LeetCode
LeetCode
Bungehurst
极限尤可突破,至臻亦不可止
展开
-
美团春招编程第一场第三题
【代码】美团春招笔试第一场第三题。原创 2024-03-09 14:17:22 · 195 阅读 · 0 评论 -
[Easy] 268. Missing Number
268. Missing NumberGiven 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: 2Example 2:Input: [9,6,4,2,3,5,7,0,1]Output: 8SolutionBit OperationRuntime: 36原创 2020-05-22 09:09:28 · 162 阅读 · 0 评论 -
[Easy] 263. Ugly Number
263. Ugly NumberWrite a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.Example 1:Input: 6Output: trueExplanation: 6 = 2 × 3Example 2:Input: 8Output: trueExplan原创 2020-05-22 08:22:27 · 238 阅读 · 0 评论 -
[Easy] 258. Add Digits
258. Add DigitsGiven a non-negative integer num, repeatedly add all its digits until the result has only one digit.Example:Input: 38Output: 2 Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.原创 2020-05-22 07:21:23 · 190 阅读 · 0 评论 -
[Easy] 257. Binary Tree Paths
257. Binary Tree PathsGiven a binary tree, return all root-to-leaf paths.Note: A leaf is a node with no children.Example:Input: 1 / \2 3 \ 5Output: ["1->2->5", "1->3"]SolutionDFSRuntime: 4 ms, faster than 79.62% of C++ onl原创 2020-05-21 07:23:01 · 170 阅读 · 0 评论 -
[Easy] 242. Valid Anagram
242. Valid AnagramGiven two strings s and t , write a function to determine if t is an anagram of s.Example 1:Input: s = "anagram", t = "nagaram"Output: trueExample 2:Input: s = "rat", t = "car"Output: falseNote:You may assume the string contain原创 2020-05-21 06:51:51 · 201 阅读 · 0 评论 -
[Easy] 237. Delete Node in a Linked List
237. Delete Node in a Linked ListWrite a function to delete a node (except the tail) in a singly linked list, given only access to that node.Given linked list – head = [4,5,1,9], which looks like following:Example 1:Input: head = [4,5,1,9], node = 5O原创 2020-05-21 06:39:15 · 152 阅读 · 0 评论 -
[Easy] 235. Lowest Common Ancestor of a Binary Search Tree
235. Lowest Common Ancestor of a Binary Search TreeGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two n原创 2020-05-20 10:50:59 · 131 阅读 · 0 评论 -
[Easy] 234. Palindrome Linked List
234. Palindrome Linked ListGiven a singly linked list, determine if it is a palindrome.Example 1:Input: 1->2Output: falseExample 2:Input: 1->2->2->1Output: trueSolutionRecursiveRuntime: 36 ms, faster than 13.84% of C++ online submis原创 2020-05-20 08:34:20 · 208 阅读 · 0 评论 -
[Easy] 232. Implement Queue using Stacks
232. Implement Queue using StacksImplement the following operations of a queue using stacks.push(x) – Push element x to the back of queue.pop() – Removes the element from in front of queue.peek() – Get the front element.empty() – Return whether the qu原创 2020-05-19 09:39:42 · 160 阅读 · 0 评论 -
[Easy] 231. Power of Two
231. Power of TwoGiven an integer, write a function to determine if it is a power of two.Example 1:Input: 1Output: true Explanation: 20 = 1Example 2:Input: 16Output: trueExplanation: 24 = 16Example 3:Input: 218Output: falseSolutionBrute Fo原创 2020-05-19 09:02:26 · 175 阅读 · 0 评论 -
[Easy] 226. Invert Binary Tree
226. Invert Binary TreeInvert a binary tree.Example:Input: 4 / \ 2 7 / \ / \1 3 6 9Output: 4 / \ 7 2 / \ / \9 6 3 1SolutionRuntime: 8 ms, faster than 38.86% of C++ online submissions for Invert Binar原创 2020-05-19 08:40:01 · 159 阅读 · 0 评论 -
[Easy] 225. Implement Stack using Queues
225. Implement Stack using QueuesImplement the following operations of a stack using queues.push(x) – Push element x onto stack.pop() – Removes the element on top of the stack.top() – Get the top element.empty() – Return whether the stack is empty.Ex原创 2020-05-19 08:11:08 · 204 阅读 · 0 评论 -
[Easy] 219. Contains Duplicate II
219. Contains Duplicate IIGiven an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.Example 1:Input: nums = [1,2原创 2020-05-18 09:42:37 · 152 阅读 · 0 评论 -
[Easy] 217. Contains Duplicate
217. Contains DuplicateGiven an array of integers, find if the array contains any duplicates.Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.Example 1:Input: [1,原创 2020-05-17 09:44:23 · 147 阅读 · 0 评论 -
[Easy] 206. Reverse Linked List
206. Reverse Linked ListReverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLSolutionStackRuntime: 8 ms, faster than 79.52% of C++ online submissions for Reverse Linked List.Memory原创 2020-05-17 09:39:15 · 156 阅读 · 0 评论 -
[Easy] 205. Isomorphic Strings
205. Isomorphic StringsGiven two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another character while preserving the ord原创 2020-05-17 09:20:21 · 137 阅读 · 0 评论 -
[Easy] 204. Count Primes
204. Count PrimesCount the number of prime numbers less than a non-negative number, n.Example:Input: 10Output: 4Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.SolutionSieve of Eratosthenes 埃氏筛法寻找质数Runtime: 16 ms, faster t原创 2020-05-17 08:39:52 · 186 阅读 · 0 评论 -
[Easy] 203. Remove Linked List Elements
203. Remove Linked List ElementsRemove all elements from a linked list of integers that have value val.Example:Input: 1->2->6->3->4->5->6, val = 6Output: 1->2->3->4->5SolutionRuntime: 28 ms, faster than 84.91% of C++ o原创 2020-05-16 10:10:24 · 162 阅读 · 0 评论 -
[Easy] 202. Happy Number
202. Happy NumberWrite an algorithm to determine if a number n is “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process原创 2020-05-16 09:49:37 · 171 阅读 · 0 评论 -
[Easy] 198. House Robber
198. House RobberYou are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and i原创 2020-05-16 09:10:43 · 156 阅读 · 0 评论 -
[Easy] 191. Number of 1 Bits
191. Number of 1 BitsWrite a function that takes an unsigned integer and return the number of ‘1’ bits it has (also known as the Hamming weight).Example 1:Input: 00000000000000000000000000001011Output: 3Explanation: The input binary string 00000000000原创 2020-05-15 17:01:29 · 152 阅读 · 0 评论 -
[Easy] 190. Reverse Bits
190. Reverse BitsReverse bits of a given 32 bits unsigned integer.Example 1:Input: 00000010100101000001111010011100Output: 00111001011110000010100101000000Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned i原创 2020-05-15 16:55:01 · 212 阅读 · 0 评论 -
[Easy] 172. Factorial Trailing Zeroes
172. Factorial Trailing ZeroesGiven an integer n, return the number of trailing zeroes in n!.Example 1:Input: 3Output: 0Explanation: 3! = 6, no trailing zero.Example 2:Input: 5Output: 1Explanation: 5! = 120, one trailing zero.Note: Your solutio原创 2020-05-15 16:37:57 · 103 阅读 · 0 评论 -
[Easy] 171. Excel Sheet Column Number
171. Excel Sheet Column NumberGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...Example 1:Inpu原创 2020-05-15 16:10:13 · 140 阅读 · 0 评论 -
[Easy] 169. Majority Element
169. Majority ElementGiven 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.Example 1:I原创 2020-05-15 16:00:18 · 192 阅读 · 0 评论 -
[Easy] 189. Rotate Array
189. Rotate ArrayGiven an array, rotate the array to the right by k steps, where k is non-negative.Follow up:Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.Could you do it in-place with O(1) extra原创 2020-05-14 11:05:25 · 142 阅读 · 0 评论 -
[Easy] 168. Excel Sheet Column Title
168. Excel Sheet Column TitleGiven a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ...Example 1:In原创 2020-05-14 10:30:49 · 196 阅读 · 0 评论 -
[Easy] 167. Two Sum II - Input array is sorted
167. Two Sum II - Input array is sortedGiven an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they ad原创 2020-05-14 09:16:34 · 142 阅读 · 0 评论 -
[Easy] 160. Intersection of Two Linked Lists
160. Intersection of Two Linked ListsWrite a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:begin to intersect at node c1.Example 1:Input: intersectVal = 8, listA = [4原创 2020-05-13 09:17:24 · 156 阅读 · 0 评论 -
[Easy] 155. Min Stack
155. Min StackDesign 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 mini原创 2020-05-13 08:14:41 · 148 阅读 · 0 评论 -
[Easy] 141. Linked List Cycle
141. Linked List CycleGiven a linked list, determine if it has a cycle in it.To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then ther原创 2020-05-12 10:32:14 · 175 阅读 · 0 评论 -
[Easy] 136. Single Number
136. Single NumberGiven a non-empty array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?Example 1:Input: [2原创 2020-05-12 09:29:47 · 274 阅读 · 0 评论 -
[Easy] 125. Valid Palindrome
125. Valid PalindromeGiven a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.Note: For the purpose of this problem, we define empty string as valid palindrome.Example 1:Input: "A man, a plan, a canal原创 2020-05-11 10:38:35 · 102 阅读 · 0 评论 -
[Easy] 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock IISay you have an array prices for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell原创 2020-05-11 09:27:03 · 541 阅读 · 0 评论 -
[Easy] 121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell StockSay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm原创 2020-05-10 21:38:44 · 183 阅读 · 0 评论 -
[Easy] 119. Pascal's Triangle II
119. Pascal’s Triangle IIGiven a non-negative index k where k ≤ 33, return the kth index row of the Pascal’s triangle.Note that the row index starts from 0.Example:Input: 3Output: [1,3,3,1]SolutionDPRuntime: 0 ms, faster than 100.00% of C++ online原创 2020-05-09 10:30:41 · 86 阅读 · 0 评论 -
[Easy] 118. Pascal's Triangle
118. Pascal’s TriangleGiven a non-negative integer numRows, generate the first numRows of Pascal’s triangle.In Pascal’s triangle, each number is the sum of the two numbers directly above it.Example:Input: 5Output:[ [1], [1,1], [1,2,1],原创 2020-05-09 10:24:40 · 142 阅读 · 0 评论 -
[Easy] 112. Path Sum
112. Path SumGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Note: A leaf is a node with no children.Example:Given the below binary tree and sum = 22,原创 2020-05-09 09:48:24 · 172 阅读 · 0 评论 -
[Easy] 111. Minimum Depth of Binary Tree
111. Minimum Depth of Binary TreeGiven a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Note: A leaf is a node with no children.Example:Given binary原创 2020-05-09 09:11:02 · 148 阅读 · 0 评论