Mock Interview: Adobe #2

JJ·2021년 4월 19일
0

MockTest

목록 보기
26/60

Reverse Integer

class Solution {
    public int reverse(int x) {
        if (x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) {
            return 0;
        }
        
        int result = 0;
        int sign = (x < 0) ? -1 : 1;
        
        int xs = x * sign;
        
        while (xs > 0) {
            if (result > Integer.MAX_VALUE / 10) {
                return 0;
            }
            
            result = result * 10;
            result += xs % 10;
            
            xs = xs / 10;
        }
        
        return result * sign;
    }
}

Runtime: 1 ms, faster than 100.00% of Java online submissions for Reverse Integer.
Memory Usage: 36.4 MB, less than 22.51% of Java online submissions for Reverse Integer.

안풀려서 이건 100% 내가 bit manipulation을 몰라서 틀리는거다라고 확신하다가
마지막에 result sign이 아닌.. xs sign을 리턴하고 있어서 안된다는것을 깨달았다는 점^^

파이선으로도 대충 풀었읍니다

class Solution:
    def reverse(self, x: int) -> int:
        
        if (x > 2**31 - 1 or x < -1 * 2**31):
            return 0
        
        result = 0
        sign = 1
        
        if x < 0:
            sign = -1
        
        xs = x * sign
        
        while xs > 0:
            if (result > 2**31 / 10):
                return 0
            result = result * 10
            result = result + xs % 10
            xs = xs / 10
        
        return result * sign

Array Partition I

class Solution {
    public int arrayPairSum(int[] nums) {
        //wtf..? sum of largest nums.length / 2 elements?
        
        
        Arrays.sort(nums);
        int result = 0;
        for (int i = 0; i < nums.length / 2; i++) {
            result += nums[i * 2];
        }
        
        return result;
    }
}

Runtime: 10 ms, faster than 97.12% of Java online submissions for Array Partition I.
Memory Usage: 41.3 MB, less than 21.39% of Java online submissions for Array Partition I.

2등인 친구들만 모아서 add~

0개의 댓글