
Leetcode
Donald_Chen
Code for fun...
展开
-
【LeetCode】300. Longest Increasing Subsequence
题目描述Given an unsorted array of integers, find the length of longest increasing subsequence.For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], there原创 2017-04-16 14:59:41 · 372 阅读 · 0 评论 -
【LeetCode】66. Plus One
题目描述Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits ar原创 2017-03-09 22:03:58 · 196 阅读 · 0 评论 -
【LeetCode】65. Valid Number
题目描述Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambiguous. You shoul原创 2017-03-09 22:01:25 · 293 阅读 · 0 评论 -
【LeetCode】64. Minimum Path Sum
题目描述Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2017-03-09 21:50:14 · 161 阅读 · 0 评论 -
【LeetCode】63. Unique Paths II
题目描述Follow up for “Unique Paths”:Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the grid.Fo原创 2017-03-09 21:44:16 · 164 阅读 · 0 评论 -
【LeetCode】62. Unique Paths
题目描述A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the b原创 2017-03-09 18:00:57 · 182 阅读 · 0 评论 -
【LeetCode】75. Sort Colors
题目描述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原创 2017-03-19 21:23:46 · 189 阅读 · 0 评论 -
【LeetCode】74. Search a 2D Matrix
题目描述Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each ro原创 2017-03-19 21:18:02 · 232 阅读 · 0 评论 -
【LeetCode】73. Set Matrix Zeroes
题目描述Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up:Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea原创 2017-03-19 21:13:56 · 222 阅读 · 0 评论 -
【LeetCode】48. Rotate Image
题目描述You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up: Could you do this in-place?解题思路每一次以一圈为单位,在一圈中,每次寻找四个受影响的点。顺时针改变这四个点,就可以实现in-place的旋转了。A原创 2017-03-07 14:24:00 · 166 阅读 · 0 评论 -
【LeetCode】 49. Group Anagrams
题目描述Given an array of strings, group anagrams together.For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]Note: All inputs wil原创 2017-03-07 14:26:19 · 195 阅读 · 0 评论 -
【LeetCode】50. Pow(x, n)
题目描述Implement pow(x, n). 解题思路快速幂。 注意一下当n为负数的情况。 还有当n为INT_MIN时的边界情况,因为此时-n会溢出。AC代码class Solution {public: double myPow(double x, int n) { bool isMin = false; if (n < 0) {原创 2017-03-07 14:29:00 · 139 阅读 · 0 评论 -
【LeetCode】79. Word Search
题目描述Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically原创 2017-03-23 21:45:40 · 252 阅读 · 0 评论 -
【LeetCode】71. Simplify Path
题目描述Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"解题思路队列。 从index = 1开始遍历string,当遇到’/’时,计算上一个’/’到现在之间中间的内容,存在如下三种情原创 2017-03-10 22:29:16 · 222 阅读 · 0 评论 -
【LeetCode】 70. Climbing Stairs
题目描述You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Note: Given n will be a positive原创 2017-03-10 22:22:38 · 182 阅读 · 0 评论 -
【LeetCode】69. Sqrt(x)
题目描述Implement int sqrt(int x).Compute and return the square root of x.解题思路二分。 每次找中间值,判断其平方与目标值的大小关系。AC代码class Solution {public: int mySqrt(int x) { int start = 0, end = x; while (原创 2017-03-10 22:18:32 · 188 阅读 · 0 评论 -
【LeetCode】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-03-10 22:14:51 · 194 阅读 · 0 评论 -
【LeetCode】67. Add Binary
题目描述Given two binary strings, return their sum (also a binary string).For example, a = "11" b ="1" Return "100". 解题思路大数相加二进制版。 先把两个数从最小位开始相加,最后把较长那个数与carry相加,直到结束。AC代码class Solution {public: s原创 2017-03-10 22:06:37 · 188 阅读 · 0 评论 -
【LeetCode】54. Spiral Matrix
题目描述Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example, Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You shou原创 2017-03-07 14:46:02 · 206 阅读 · 0 评论 -
【LeetCode】53. Maximum Subarray
题目描述Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has原创 2017-03-07 14:37:54 · 180 阅读 · 0 评论 -
【LeetCode】61. Rotate List
题目描述Given a list, rotate the list to the right by k places, where k is non-negative.For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.解题思路先遍历一遍确定链表中元素的个数n,然后k对n求模,因为移动n的倍数还原创 2017-03-09 17:51:25 · 184 阅读 · 0 评论 -
【LeetCode】60. Permutation Sequence
题目描述The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3):"123""132""213""231""312"原创 2017-03-09 17:42:09 · 187 阅读 · 0 评论 -
【LeetCode】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 repeated number may be chosen fro原创 2017-03-03 16:10:59 · 213 阅读 · 0 评论 -
【LeetCode】36. Valid Sudoku
题目描述Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku原创 2017-03-03 16:00:58 · 162 阅读 · 0 评论 -
【LeetCode】34. Search for a Range
题目描述Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm’s runtime complexity must be in the order of O(log n).If the targe原创 2017-03-02 16:41:14 · 731 阅读 · 0 评论 -
【LeetCode】33. Search in Rotated Sorted Array
题目描述Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in th原创 2017-03-02 16:07:04 · 192 阅读 · 0 评论 -
【LeetCode】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原创 2017-03-01 17:11:02 · 226 阅读 · 0 评论 -
【LeetCode】29. Divide Two Integers
题目描述Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.解题思路二进制移位+减法。 观察我们自己求十进制数的除法的过程,模拟这个过程即可。 二进制的除法与十进制的区别,在于十进制每次要通过乘法(乘以1到9中的一个数字)来确定这原创 2017-03-01 16:59:47 · 267 阅读 · 0 评论 -
【Leetcode】21. Merge Two Sorted Lists
Leetcode 21. Merge Two Sorted Lists题目描述Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.解题思路主要考察对于指针的应用。 如果直原创 2017-02-26 16:38:16 · 250 阅读 · 0 评论 -
【LeetCode】24. Swap Nodes in Pairs
题目描述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 ma原创 2017-02-28 11:37:56 · 186 阅读 · 0 评论 -
【LeetCode】40. Combination Sum II
题目描述Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combinatio原创 2017-03-04 17:41:55 · 158 阅读 · 0 评论 -
【LeetCode】43. Multiply Strings
题目描述Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.Note:The length of both num1 and num2 is < 110.Both num1 and num2 contains only digits 0-原创 2017-03-04 17:47:45 · 235 阅读 · 0 评论 -
【LeetCode】55. Jump Game
题目描述Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you原创 2017-03-08 15:47:25 · 184 阅读 · 0 评论 -
【LeetCode】59. Spiral Matrix II
题目描述Given an integer n, generate a square matrix filled with elements from 1 to n2n^2 in spiral order.For example, Given n = 3, You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [原创 2017-03-09 17:29:28 · 163 阅读 · 0 评论 -
【LeetCode】58. Length of Last Word
题目描述Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.Note: A word is define原创 2017-03-09 17:24:23 · 161 阅读 · 0 评论 -
【LeetCode】18. 4Sum
18. 4Sum题目描述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 s原创 2017-02-27 11:55:35 · 226 阅读 · 0 评论 -
【LeetCode】16. 3Sum Closest
16. 3Sum Closest题目描述Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input原创 2017-02-27 11:46:37 · 196 阅读 · 0 评论 -
【Leetcode】15. 3Sum
【Leetcode】15. 3Sum题目描述Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set m原创 2017-02-26 16:36:22 · 238 阅读 · 0 评论 -
【LeetCode】47. Permutations II
题目描述Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example, [1,1,2] have the following unique permutations:[ [1,1,2], [1,2,1], [2,1,1]]原创 2017-03-05 14:57:09 · 264 阅读 · 0 评论 -
【LeetCode】46. Permutations
题目描述Given a collection of distinct numbers, return all possible permutations.For example, [1,2,3] have the following permutations:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]解题原创 2017-03-05 14:52:46 · 175 阅读 · 0 评论