본문 바로가기

LeetCode/Top Interview Q. - Easy47

LeetCode. Min Stack Problem: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum element in the stack. Example 1: -Summary- Use tuple to save the minimum value and the current value of the stack. class MinStack: def __ini.. 2020. 6. 26.
LeetCode. Shuffle an Array Problem: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] nums = {1,2,3}; Solution solution = new Solution(nums); // Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned. solution.shuffle(); // Resets the array back to its original configuration [1,2,3]. solution.reset(); // Returns the ra.. 2020. 6. 24.
LeetCode. Reverse Bits Problem: Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 Output: 00111001011110000010100101000000 Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000. Example 2: Input: 1111111111111111111111111111.. 2020. 6. 22.
LeetCode. Pascal's Triangle Problem: Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. -Summary- Solved by dynamic programming approach (Below code is available from Leetcode solution) 1. The first and last element of each row is always 1. 2. Calculate inner elements by using and adding previous row elements. class Solution: def generate(self, num_rows): triangle = [] for row_num in ran.. 2020. 6. 21.
LeetCode. Missing Number [Bit] Problem: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1: Input: [3,0,1] Output: 2 Example 2: Input: [9,6,4,2,3,5,7,0,1] Output: 8 Note: Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity? -Summary- 1. Bit operation on length of the nums list and .. 2020. 6. 19.
LeetCode.Hamming Distance [Bit] Problem: The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. Note: 0 ≤ x, y < 231. Example: Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different. -Summary- Used bin() method and.. 2020. 6. 18.