24. Swap Nodes in Pairs Total Accepted: 130373 Total Submissions: 354429 Difficulty: Easy Contributors: Admin Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only …
Author Archives: czxttkl
Leetcode 18: 4Sum
18. 4Sum Total Accepted: 92730 Total Submissions: 367805 Difficulty: Medium Contributors: Admin Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The …
Leetcode 19: Remove Nth Node From End of List
19. Remove Nth Node From End of List Total Accepted: 140954 Total Submissions: 445848 Difficulty: Easy Contributors: Admin Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked …
Continue reading “Leetcode 19: Remove Nth Node From End of List”
Leetcode 200: Number of Islands
200. Number of Islands Total Accepted: 71131 Total Submissions: 228449 Difficulty: Medium Contributors: Admin Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid …
Leetcode 75: Sort Colors
75. Sort Colors Total Accepted: 125210 Total Submissions: 345796 Difficulty: Medium Contributors: Admin Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 …
Radix Sort
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] * …
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, …