A post to review RadixSort. The idea is to Code class RadixSort(): def sort(self, nums): max_num = max(nums) exp = 1 while max_num / exp: nums = self.count_sort(nums, exp) exp = exp * 10 return nums def count_sort(self, nums, exp): # how many numbers have 0~9 for the current position digit_counts = [0] * …
Author Archives: czxttkl
Leetcode 169: Majority Element
169. Majority Element Total Accepted: 152482 Total Submissions: 346405 Difficulty: Easy Contributors: Admin Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. …
Leetcode 277: Find the Celebrity
277. Find the Celebrity Total Accepted: 15126 Total Submissions: 42620 Difficulty: Medium Contributors: Admin Suppose you are at a party with n people (labeled from 0 to n – 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n – 1 people know him/her but …
Leetcode 325: Maximum Size Subarray Sum Equals k
325. Maximum Size Subarray Sum Equals k Total Accepted: 13137 Total Submissions: 31728 Difficulty: Medium Contributors: Admin Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn’t one, return 0 instead. Note:The sum of the entire nums array is guaranteed to fit …
Continue reading “Leetcode 325: Maximum Size Subarray Sum Equals k”
Leetcode 161: One Edit Distance
161. One Edit Distance Total Accepted: 20350 Total Submissions: 68060 Difficulty: Medium Contributors: Admin Given two strings S and T, determine if they are both one edit distance apart. Code class Solution(object): def isOneEditDistance(self, s, t): “”” :type s: str :type t: str :rtype: bool “”” for idx, (chr_s, chr_t) in enumerate(zip(s, …
Leetcode 191: Number of 1 Bits
191. Number of 1 Bits Total Accepted: 119474 Total Submissions: 312895 Difficulty: Easy Contributors: Admin Write a function that takes an unsigned integer and returns the number of ’1′ bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11′ has binary representation 00000000000000000000000000001011, so the function should return 3. …
Leetcode 285: Inorder Successor in BST
285. Inorder Successor in BST Total Accepted: 17302 Total Submissions: 47674 Difficulty: Medium Contributors: Admin Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Note: If the given node has no in-order successor in the tree, return null. Code # Definition for a …
Leetcode 253: Meeting Rooms II
253. Meeting Rooms II Total Accepted: 20906 Total Submissions: 55624 Difficulty: Medium Contributors: Admin Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],…] (si < ei), find the minimum number of conference rooms required. For example,Given [[0, 30],[5, 10],[15, 20]],return 2. Code # Definition for an interval. # class …
Leetcode 157: Read N Characters Given Read4
157. Read N Characters Given Read4 Total Accepted: 19570 Total Submissions: 66588 Difficulty: Easy Contributors: Admin The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. …
Continue reading “Leetcode 157: Read N Characters Given Read4”
Leetcode 301: Remove Invalid Parentheses
301. Remove Invalid Parentheses Total Accepted: 23550 Total Submissions: 69307 Difficulty: Hard Contributors: Admin Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: “()())()” -> [“()()()”, “(())()”] “(a)())()” -> [“(a)()()”, …