LeetCode
NickChen_0411
学生
展开
-
116. Populating Next Right Pointers in Each Node
You are given aperfect binary treewhereall leaves are on the same level, and every parent has two children. The binary tree has the following definition:struct Node { int val; Node *left; ...原创 2019-04-18 12:02:54 · 208 阅读 · 0 评论 -
647. Palindromic Substrings(回文子字符串)
Given a string, your task is to count how many palindromic substrings in this string.The substrings with different start indexes or end indexes are counted as different substrings even they consist of转载 2017-10-21 21:04:14 · 348 阅读 · 0 评论 -
621. Task Scheduler(任务行程表)
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be转载 2017-10-20 22:00:06 · 717 阅读 · 0 评论 -
543. Diameter of Binary Tree (二叉树的直径)
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longestpath between any two nodes in a tree. This path may or may not转载 2017-10-11 10:56:21 · 364 阅读 · 0 评论 -
538. Convert BST to Greater Tree (将二叉搜索树BST转为较大树)
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.Example:转载 2017-10-10 12:09:05 · 258 阅读 · 0 评论 -
463. Island Perimeter 岛屿周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely s转载 2017-10-09 21:13:02 · 467 阅读 · 0 评论 -
39/40/216/377 Combination Sum
39 Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeate原创 2017-09-21 11:22:40 · 215 阅读 · 0 评论 -
461. Hamming Distance
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.Examp原创 2017-09-29 15:07:55 · 221 阅读 · 0 评论 -
123/188/309.Best Time to Buy and Sell Stock 买股票的最佳时间
123 Best Time to Buy and Sell Stock IIISay you have an array 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 at most tw原创 2017-09-20 20:52:56 · 383 阅读 · 0 评论 -
279. Perfect Squares (完全平方数)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return原创 2017-09-12 11:02:28 · 1553 阅读 · 0 评论 -
448.Find All Numbers Disappeared in an Array 找出数组中所有消失的数字
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could you转载 2017-09-28 12:31:36 · 433 阅读 · 0 评论 -
239. Sliding Window Maximum 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window mo原创 2017-09-11 11:29:34 · 514 阅读 · 0 评论 -
438 Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.Strings consists of lowercase English letters only and the length of both strings s and p will not be larger t原创 2017-09-27 12:24:44 · 493 阅读 · 0 评论 -
238. Product of Array Except Self (除本身之外的数组之积)
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n).For e原创 2017-09-10 17:22:27 · 367 阅读 · 0 评论 -
437. Path Sum III(二叉树的路径和之三)
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it must go转载 2017-09-26 15:26:01 · 325 阅读 · 0 评论 -
235/236. Lowest Common Ancestor of a Binary Search Tree/Lowest Common Ancestor of a Binary Tree
235 Lowest Common Ancestor of a Binary Search Tree Given 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 Wikipedi转载 2017-09-09 11:39:18 · 210 阅读 · 0 评论 -
87.Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = “great”:great / \ gr eat /转载 2017-11-07 17:50:20 · 303 阅读 · 0 评论 -
572. Subtree of Another Tree (另一个树的子树)
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node’转载 2017-10-13 11:14:03 · 360 阅读 · 0 评论 -
103. Binary Tree Zigzag Level Order Traversal
Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tre...原创 2019-04-17 21:21:05 · 174 阅读 · 0 评论 -
347. Top K Frequent Elements
Given a non-empty array of integers, return thekmost frequent elements.Example 1:Input: nums = [1,1,1,2,2,3], k = 2Output: [1,2]Example 2:Input: nums = [1], k = 1Output: [1]Note:You...原创 2019-04-14 10:56:40 · 176 阅读 · 0 评论 -
739. Daily Temperatures
Given a list of daily temperaturesT, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which...原创 2019-04-16 12:43:25 · 219 阅读 · 0 评论 -
322. Coin Change
You are given coins of different denominations and a total amount of moneyamount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money ...原创 2019-04-15 22:03:31 · 171 阅读 · 0 评论 -
71. Simplify Path (简化路径)
Given an absolute path for a file (Unix-style), simplify it.For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c”这道题让简化给定的路径,光根据题目中给的那一个例子还真不太好总结出规律,应该再加上两个例子 path = “/a/./b/../c转载 2017-11-06 11:49:53 · 639 阅读 · 0 评论 -
69. Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方,然后和x比较大小,为了缩短查找时间,我们采用二分搜索法来找平方根,由于求平方的结果会很大,可能会超过int的取值范围,所以我们都用long long来定义变量,这样就不会越界,代码如下:解法一// Bina转载 2017-11-03 12:30:40 · 260 阅读 · 0 评论 -
97/100 Interleaving String/Same Tree
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, Given: s1 = “aabcc”, s2 = “dbbca”,When s3 = “aadbbcbcac”, return true. When s3 = “aadbbbaccc”, return false原创 2017-11-12 23:32:34 · 166 阅读 · 0 评论 -
68. Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is,转载 2017-11-02 21:49:16 · 214 阅读 · 0 评论 -
31. Next Permutation (下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible orde原创 2017-10-17 11:33:01 · 1038 阅读 · 0 评论 -
95. Unique Binary Search Trees II (独一无二的二叉搜索树之二)
Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n.For example, Given n = 3, your program should return all 5 unique BST’s shown below. 1 3 3转载 2017-11-10 15:09:02 · 381 阅读 · 0 评论 -
617. Merge Two Binary Trees(合并二叉树)
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree. T原创 2017-10-16 11:39:21 · 283 阅读 · 0 评论 -
91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping:‘A’ -> 1 ‘B’ -> 2 … ‘Z’ -> 26 Given an encoded message containing digits, determine the total number of转载 2017-11-09 11:31:03 · 312 阅读 · 0 评论 -
583. Delete Operation for Two Strings(两个字符串的删除操作)
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.Example 1:Input: “sea”, “ea转载 2017-10-14 12:10:05 · 686 阅读 · 0 评论 -
78/90. Subsets (子集合)
78 Subsets Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets.For example转载 2017-11-08 12:02:33 · 312 阅读 · 0 评论 -
394. Decode String (解码字符串)
Given an encoded string, return it’s decoded string.The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guara原创 2017-09-25 22:41:03 · 943 阅读 · 0 评论 -
234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space? 这道题让我们判断一个链表是否为回文链表,LeetCode中关于回文串的题共有六道,除了这道,其他的五道为 Palindrome Number 验证回文数字,原创 2017-09-08 11:20:19 · 309 阅读 · 0 评论 -
106/107/108 Convert * to Binary Search Tree 组转为二叉搜索树
108 Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树Given an array where elements are sorted in ascending order, convert it to a height balanced BST.这道题是要将有序数组转为二叉搜索树,所谓二叉搜索树,是一种始终满足左<根<右的特性,如果将原创 2017-08-02 10:32:26 · 498 阅读 · 0 评论 -
136/137/260 Single Number
136 Single Number Given an 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 wi原创 2017-08-01 17:50:15 · 285 阅读 · 0 评论 -
198/213/337 House Robber
198 House Robber You 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 th原创 2017-08-08 11:21:07 · 296 阅读 · 0 评论 -
72 Edit Distance (编辑距离)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) In原创 2017-07-23 16:48:14 · 750 阅读 · 0 评论 -
169/229 Majority Element(寻找众数)
169 Majority Element 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原创 2017-08-07 18:09:51 · 327 阅读 · 0 评论 -
51/52 N-Queens(N皇后问题)
51 N-Queens dfs经典问题,采用回溯法求解。 经典的N皇后问题,基本所有的算法书中都会包含的问题,经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组,其中pos[i]表示第i行皇后的位置,初始化为-1,然后从第0开始递归,每一行都一次遍历各列,判断如果在该位置放置皇后会不会有冲突,以此类推,当到最后一行的皇后放好后,一种解法就生成了,将其存入结果res中,然后再还会继原创 2017-07-19 09:46:41 · 458 阅读 · 0 评论