![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 71
chilakaka
这个作者很懒,什么都没留下…
展开
-
LeetCode - Search in Rotated Sorted Array II
这道题麻烦的地方就是不能像没有duplicate时候直接判断有没有bump。所以,分成三种情况,如果 A[left] A[mid],都可以直接判断左边有没有bump,然后根据左边或者右边的range来决定下一步去哪边。如果A[left]==A[mid],则无法决定左右两边是range还是bump,所以略过left,移动到下个点进行判断。另一种方法是,利用递归,去搜查mid左右两边,因为此时原创 2015-03-14 01:04:56 · 215 阅读 · 0 评论 -
LeetCode-3Sum Closest
https://oj.leetcode.com/problems/3sum-closest/和3sum非常类似,基本重用代码就行。区别在于,如果找到target,可以直接返回,并且需要维护一个目前最小差别的和值。代码如下:public class Solution { public int threeSumClosest(int[] num, int target) {原创 2015-03-03 02:16:59 · 268 阅读 · 0 评论 -
LeetCode-3Sum
https://oj.leetcode.com/problems/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原创 2015-03-03 01:10:35 · 266 阅读 · 0 评论 -
LeetCode - Rotate Array
https://leetcode.com/problems/rotate-array/Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].原创 2015-03-15 22:55:59 · 230 阅读 · 0 评论 -
LeetCode - Remove Duplicates from Sorted Array I && II
https://leetcode.com/problems/remove-duplicates-from-sorted-array/Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.D原创 2015-03-15 23:22:33 · 238 阅读 · 0 评论 -
LeetCode - Search For a Range
https://leetcode.com/problems/search-for-a-range/Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be i原创 2015-03-15 21:35:22 · 219 阅读 · 0 评论 -
LeetCode - Search a 2D Matrix
https://leetcode.com/problems/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原创 2015-03-15 21:55:03 · 224 阅读 · 0 评论 -
LeetCode - Maximum Product Subarray
https://leetcode.com/problems/maximum-product-subarray/Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the arra原创 2015-03-25 04:13:19 · 223 阅读 · 0 评论 -
LeetCode - Rotate Image
https://leetcode.com/problems/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?这道题就原创 2015-03-15 22:33:51 · 184 阅读 · 0 评论 -
LeetCode - Plus One - Frequent
https://leetcode.com/problems/plus-one/Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at原创 2015-03-15 23:41:43 · 182 阅读 · 0 评论 -
LeetCode-Word Search
https://oj.leetcode.com/problems/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原创 2015-03-03 00:27:47 · 264 阅读 · 0 评论 -
LeetCode - Merge Sorted Array
https://leetcode.com/problems/merge-sorted-array/Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is great原创 2015-03-16 00:23:22 · 251 阅读 · 0 评论 -
LeetCode - Pascal's Triangle I && II
https://leetcode.com/problems/pascals-triangle/Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1]原创 2015-03-15 23:55:52 · 253 阅读 · 0 评论 -
LeetCode - Majority Element
https://leetcode.com/problems/majority-element/这道题虽然是easy难度的,但还挺多解法,挺有意思的。我最开始想到的是hash table解法,很简单,代码就不写了。另外,因为众数一定存在,所以由于有一个candidate解法,即把当前遇到的数视为candidate,如果碰到不是candidate的数,则candidate count减1,如果原创 2015-03-25 04:50:58 · 239 阅读 · 0 评论 -
LeetCode - Jump Game
https://leetcode.com/problems/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 maximu原创 2015-03-25 22:20:16 · 245 阅读 · 0 评论 -
LeetCode - Find Missing Positive
https://leetcode.com/problems/first-missing-positive/Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.原创 2015-03-25 23:26:57 · 175 阅读 · 0 评论 -
LeetCode - Find Peak Element
https://leetcode.com/problems/find-peak-element/A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return原创 2015-03-26 00:06:43 · 191 阅读 · 0 评论 -
LeetCode - Remove Element
https://leetcode.com/problems/remove-element/Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn'原创 2015-03-15 23:07:10 · 223 阅读 · 0 评论 -
LeetCode - Find Minimum in Rotated Sorted Array
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/Suppose a sorted array 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原创 2015-03-26 00:27:04 · 190 阅读 · 0 评论 -
LeetCode - Container With Most Water
https://leetcode.com/problems/container-with-most-water/Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such原创 2015-03-26 00:51:41 · 163 阅读 · 0 评论 -
LeetCode - Combination Sum
https://leetcode.com/problems/combination-sum/Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same原创 2015-03-28 03:23:46 · 204 阅读 · 0 评论 -
LeetCode - Combination Sum I && II
https://leetcode.com/problems/combination-sum/Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same原创 2015-03-28 03:38:41 · 263 阅读 · 0 评论 -
LeetCode - Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock Ihttps://leetcode.com/problems/best-time-to-buy-and-sell-stock/这道题非常简单,在每个位置卖的话,它能获得的最大值就是它的值减去在它之前出现的最小值。如果在它之前没有比它小的值,那么就是当天买当天卖,即profit为0。所以就不断更新已出现过的数中最小的,然原创 2015-03-28 04:00:54 · 271 阅读 · 0 评论 -
LeetCode - Construct Binary Tree from Inorder and Postorder Traversal
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/这道题跟之前 Construct Binary Tree from Inorder and Preorder Traversal 思路完全一样,就是根节点和子树在数组中的位置变了。代码如下:public原创 2015-03-28 23:38:06 · 229 阅读 · 0 评论 -
LeetCode - Longest Consecutive Sequence
https://leetcode.com/problems/longest-consecutive-sequence/Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200,原创 2015-03-29 00:09:08 · 185 阅读 · 0 评论 -
LeetCode - Largest Rectangle in Histogram
https://leetcode.com/problems/largest-rectangle-in-histogram/这道题,真的好难。。。。。。其实O(n^2)的方法很简单,就是每个元素往两边扩展,找到比自己小的边界,然后再算以当前元素为高的最大矩阵面积。O(n)的方法非常巧妙,也超级难理解,先贴代码吧:public class Solution {原创 2015-03-29 01:08:25 · 150 阅读 · 0 评论 -
LeetCode - Maximal Rectangle
https://leetcode.com/problems/maximal-rectangle/Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.这道题又是一道看了无从下手的题。。。http://ww原创 2015-03-29 01:34:24 · 197 阅读 · 0 评论 -
LeetCode - Construct Binary Tree from Preorder and Inorder Traversal
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/这道题用递归就很明了了,每个子树,preorder的第一个数是根节点,然后在inorder里面找到这个根节点,inorder里左边的就是左子树,右边的是右子树,然后再递归构造左右子树。这里有画图,讲得很清楚:http原创 2015-03-28 23:14:14 · 198 阅读 · 0 评论 -
LeetCode - Sqrt(x)
这道题如果挨个算的话就超时了,所以用二分法.sqrt(x) 当x>4时,都小于x/2, 但所有都小于x/2+1.(x/2)^2有可能超过int范围,因此,要么用一个long变量,要么比较mid和x/mid代码如下:public class Solution { public int sqrt(int x) { if(x<0) return -1;原创 2015-03-29 05:03:09 · 175 阅读 · 0 评论 -
LeetCode - String to Integer (atoi) - Frequent
https://leetcode.com/problems/string-to-integer-atoi/这道题简直无语, 其实很简单,就是遍历String, 就是要求不给清楚, 导致调试好几遍......代码如下:public class Solution { public int atoi(String str) { if(str==null||str.le原创 2015-03-29 04:29:58 · 191 阅读 · 0 评论 -
LeetCode - Roman to Integer - Frequent
https://leetcode.com/problems/roman-to-integer/Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.因为罗马数字左减最多一位,因此只需要检查当前数字是被左减的还是被右加的原创 2015-03-29 05:19:57 · 175 阅读 · 0 评论 -
LeetCode - Swap Nodes in Pairs - Frequent
https://leetcode.com/problems/swap-nodes-in-pairs/这种题都可以用dummy node来解决,不过要注意其中node指针换来换去的细节问题,很容易错。代码如下:public class Solution { public ListNode swapPairs(ListNode head) { if(head原创 2015-03-29 07:33:47 · 167 阅读 · 0 评论 -
LeetCode - Rotate List
https://leetcode.com/problems/rotate-list/1. k > len时,实际rotate的次数是 k % len;2. list的倒数第k个node是rotate k次后的新头,然后从这里截断,把前后两部分倒转位置重新连接起来即可。代码如下: public ListNode rotateRight(ListNode head, in原创 2015-03-29 07:57:21 · 152 阅读 · 0 评论 -
LeetCode - Trap Rain Water
这道题思维很巧妙,不管是two pointer方法,还是stack方法,能想出来的都太厉害了!首先,two pointer两遍扫描,对于每个位置来说,找到它左右最高的bar,那么当前位置能储存的水量就是两边最高bar较小那个的值-自己的值,代码如下:public int trap(int[] A){ if(A==null ||A.length==0) return 0;原创 2015-03-13 00:15:40 · 398 阅读 · 0 评论 -
LeetCode - Insert Interval
这道题蛮有意思的,处理方法如下:遍历每个已存在的interval如果这个newinterval.end如果newinterval.start>current.end,并且current后面已经没有更大的interval(即不可能再合并),则把newinterval插到current的后面,如果current后面还有interval,则newinterval还有合并的可能,则i++,查看下一原创 2015-03-13 04:55:20 · 222 阅读 · 0 评论 -
LeetCode - Multiply Strings
https://leetcode.com/problems/multiply-strings/Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are n原创 2015-03-30 00:51:27 · 349 阅读 · 0 评论 -
LeetCode - Divide Two Integers
https://leetcode.com/problems/divide-two-integers/Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.这道题的解法一个是不断地用被除数减去除数,直到被除数比除原创 2015-03-30 03:42:16 · 183 阅读 · 0 评论 -
LeetCode - Palindrome Number - Frequent
https://leetcode.com/problems/palindrome-number/这道题可以用不同的方法做,比如转换成string再对比,或者用Reverse Integer的解求出来再比较,但是这时候如果溢出的话就说明肯定不是palindrome了。另外,题目要求不能有extra space。所以直接把首尾两位提出来,比较是否相等。当最后只剩一位或者不剩时,就说明是pali原创 2015-03-30 00:03:58 · 188 阅读 · 0 评论 -
LeetCode - Factorial Trailing Zeroes
https://leetcode.com/problems/factorial-trailing-zeroes/Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.因为 2原创 2015-03-30 04:29:17 · 243 阅读 · 0 评论 -
LeetCode - Add Binary
https://leetcode.com/problems/add-binary/Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".这道题就跟所有加法题一样,只要注意进位,和一个数加完后,处理另一个数剩原创 2015-03-30 04:17:26 · 153 阅读 · 0 评论