Microsoft Mock Interview #1

JJ·2021년 3월 10일
0

MockTest

목록 보기
4/60

이렇게만 나오면 좋겠다...2^^

Delete Node in a Linked List

class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
        
    }
}

Runtime: 0 ms, faster than 100.00% of Java online submissions for Delete Node in a Linked List.
Memory Usage: 38.3 MB, less than 66.17% of Java online submissions for Delete Node in a Linked List.

예예~~

Number of 1 Bits

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        String sn = Integer.toBinaryString(n);
        int total = 0;
        for (int i = 0; i < sn.length(); i++) {
            if (sn.charAt(i) == '1') {
                total++;
            }
        }
        
        return total;
    }
}

보자마자 toBinaryString 생각했는데 디버깅 해놓고 안바꺼서 안되는줄 알고 당황탔네요...^^;;;;;;

루션이

public int hammingWeight(int n) {
    int sum = 0;
    while (n != 0) {
        sum++;
        n &= (n - 1);
    }
    return sum;
}

100%

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
    int bits = 0;
    int mask = 1;
    for (int i = 0; i < 32; i++) {
        if ((n & mask) != 0) {
            bits++;
        }
        mask <<= 1;
    }
    return bits;
}
}

100%

0개의 댓글