[Mock] Microsoft 1

shsh·2021년 3월 10일
0

Mock

목록 보기
4/93

237. Delete Node in a Linked List

Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly.

It is guaranteed that the node to be deleted is not a tail node in the list.

My Answer 1: Accepted (Runtime: 32 ms - 96.36% / Memory Usage: 14.9 MB - 36.26%)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        node.val = node.next.val
        node.next = node.next.next

node 의 값 (node.val, node.next) 을 node.next 의 값으로 변경


191. Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).

Note:

  • Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
  • In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3, the input represents the signed integer. -3.

My Answer 1: Accepted (Runtime: 24 ms - 97.26% / Memory Usage: 14.3 MB - 7.95%)

class Solution:
    def hammingWeight(self, n: int) -> int:
        result = 0
        
        while n:
            result += n % 2
            n = n // 2
        
        return result

2 로 나눈 나머지의 개수 세기

profile
Hello, World!

0개의 댓글

Powered by GraphCDN, the GraphQL CDN