Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
class Solution:
def reverse(self, x: int) -> int:
# 부호 설정
sign = 1
if x < 0:
sign = -1
x = abs(x)
# x 를 reverse 로 result 에 저장
result = 0
while x:
result += x%10
result *= 10
x //= 10
# 10 이 한번 더 곱해졌으므로 나누기 한번~
result //= 10
# 부호 곱해주기
result = sign * result
# 범위 체크
if result < -2 ** 31 or result > 2 ** 31 - 1:
return 0
return result
부호부터 체크해주고 음수면 절댓값으로 바꿔줌
x 를 10 으로 나눠가면서 result 에 더해주고 10 곱해준다 => reverse 가 됨
부호 설정해주고 범위 체크하면 끗~
bit 어쩌구 해서 쫄았는데 나름 괜찮았읍니다
제대로 푼건 맞겠지..;
Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum.
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
result = 0
for i in range(0, len(nums), 2):
result += nums[i]
return result
자세히 보니까...
sort 하고 홀수들만 더해주면 되는거더라구요~
도비야 고마워~