
Leetcode
文章平均质量分 68
不刷脸皮要刷题
这个作者很懒,什么都没留下…
展开
-
longest substring without repeating characters
Sliding window类的题类似maximum subarray, maximum product subarray转载 2014-11-10 00:43:25 · 278 阅读 · 0 评论 -
Remove duplicates from sorted array I
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with转载 2014-10-09 11:28:02 · 247 阅读 · 0 评论 -
sqrt(x)
//二分搜索//考虑溢出的情况,所以要用long!要通过编译的话,还要强制类型转换回来!public class Solution { public int sqrt(int x) { if (x long beg = 1; long end = x/2; while (beg转载 2014-10-09 12:00:01 · 277 阅读 · 0 评论 -
Word Break I
1, recursion2, recursion + memoization3, DP转载 2014-10-03 23:31:03 · 367 阅读 · 0 评论 -
Flatten Binary Tree to Linked List
1. Inorder flatten:public class Solution { TreeNode prev = null; public void flatten(TreeNode root) { if (root == null) { return; } TreeNode rightnode =转载 2014-11-13 04:36:09 · 294 阅读 · 0 评论 -
Palindrome Number溢出怎么处理?
1. x == reverse(x)?2. public class Solution { // 1. could negative number be Palindrome? // 2. reverse Integer! public boolean isPalindrome(int x) { if (x < 0) return false转载 2014-10-09 21:10:11 · 287 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree
Inorder逆向思维//inorderpublic class Solution { ListNode list; public TreeNode sortedListToBST(ListNode head) { int len = 0; ListNode node = head; while (node != null) {转载 2014-11-13 04:56:49 · 331 阅读 · 0 评论 -
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:转载 2014-10-09 20:51:26 · 388 阅读 · 0 评论 -
Add two numbers & Add binary
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link转载 2014-10-09 11:58:34 · 294 阅读 · 0 评论 -
Valid Sudoku
1.标记 Visited:a. 直接在array中改 (bi'r转载 2014-10-12 23:20:10 · 271 阅读 · 0 评论 -
资料汇总篇
Leetcode一些好的网站:1,九章算法da原创 2014-10-04 12:13:53 · 291 阅读 · 0 评论 -
Subsets I, II
DFS + BacktrackingII是考虑原set中有重复元素的情况Subset ISubset I转载 2014-10-19 21:02:17 · 346 阅读 · 0 评论 -
Maximum Product Subarray
最值问题的第二种思路,保存局部最优,更新全局最优求P转载 2014-10-19 20:53:57 · 240 阅读 · 0 评论 -
Multiply String
public class Solution { public String multiply(String num1, String num2) { if (num1 == null || num2 == null) { return null; } int len1 = num1.length(); int l转载 2014-10-19 20:57:37 · 313 阅读 · 0 评论 -
String to Integer(atoi)
1, 考虑溢出 -》用long。 If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.2,转载 2014-10-19 21:08:03 · 366 阅读 · 0 评论 -
Interleaving String
先保存一下 某个大神的DP专辑:http://www.cnblogs.com/remlostime/tag/DP/转载 2014-10-19 21:26:55 · 304 阅读 · 0 评论 -
Sudoku Solver
找到存在的解:DFS + Backtracking因为ji转载 2014-10-13 00:12:31 · 268 阅读 · 0 评论 -
Remove duplicates from sorted array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,转载 2014-10-09 11:49:50 · 232 阅读 · 0 评论 -
Largest Rectangle in Histogram待求
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width o转载 2014-10-06 04:40:58 · 250 阅读 · 0 评论 -
Trapping rain water
array的找规律的题public class Solution { public int trap(int[] A) { int sum = 0; int maxInd = -1; int max = -1; //find the highest bars; for (int i = 0; i < A.leng转载 2014-10-04 04:30:22 · 303 阅读 · 0 评论 -
3 sum
注意跳过重复的元素!//时间复杂度: O(n^2)public class Solution { public List> threeSum(int[] num) { List> res = new ArrayList<>(); if(num == null || num.length < 3) return res; Arra转载 2014-11-10 11:27:21 · 303 阅读 · 1 评论 -
3 sum closest
two pointers O(n^2)public class Solution { public int threeSumClosest(int[] num, int target) { if (num == null || num.length <= 2) return 0; Arrays.sort(num); int closest转载 2014-11-10 11:43:02 · 337 阅读 · 0 评论 -
Container with most water
two pointers, 两头往中间走转载 2014-11-10 12:19:53 · 231 阅读 · 0 评论 -
remove element
Remove系列:two pointers类型转载 2014-11-10 03:28:55 · 332 阅读 · 0 评论 -
remove duplicates from sorted list, remove nth node
Linkedlist中的remove,记得要构造fakehead!转载 2014-11-10 03:40:55 · 293 阅读 · 0 评论 -
4 sum
public class Solution { public List> fourSum(int[] num, int target) { List> res = new ArrayList<>(); if (num == null || num.length < 4) { return res; }转载 2014-11-10 11:54:29 · 266 阅读 · 0 评论 -
Sort Colors
//法一: counting sortpublic class Solution { public void sortColors(int[] A) { int[] count = new int[3]; for (int i = 0; i < A.length; i++) { count[A[i]]++; }转载 2014-11-10 13:22:59 · 247 阅读 · 0 评论 -
Combination Sum I, II
public class Solution { public List> combinationSum(int[] candidates, int target) { if (candidates == null || candidates.length == 0) { return null; } Arrays.so转载 2014-11-11 23:48:00 · 252 阅读 · 0 评论 -
Letter Combinations of a Phone number
先把digit-letter建个Given a digit string, return all possible letter combinations that the number could represent.转载 2014-11-12 00:06:30 · 326 阅读 · 0 评论 -
Gray Code
public class Solution { public List grayCode(int n) { List res = new ArrayList<>(); res.add(0); if(n == 0) return res; res = grayCode(n-1); for(int转载 2014-11-12 00:43:49 · 280 阅读 · 0 评论 -
Divide two integers
注意dividend是0的情况,这个时候sum总是比被除数大的!尼玛考虑负数的情况!而且还有溢出的情况。。。。 -(-2147483648)溢出啦 亲。。。要从O(n) -> O(lgn) -> O(1)转载 2014-10-09 12:07:58 · 255 阅读 · 0 评论 -
Search for a range
两次二分public class Solution { public int[] searchRange(int[] A, int target) { int[] res = new int[2]; res[0] = -1; res[1] = -1; int beg = 0; in转载 2014-10-09 12:14:27 · 316 阅读 · 0 评论 -
Sum root to leaf numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum转载 2014-10-09 12:31:17 · 491 阅读 · 0 评论 -
Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3转载 2014-10-05 02:36:30 · 241 阅读 · 0 评论 -
Longest Palindromic Substring
public class Solution { public String longestPalindrome(String s) { if (s == null) return null; int max = 0; String maxSub = ""; for (int beg = 0; beg < s.length();转载 2014-10-03 09:13:00 · 274 阅读 · 0 评论 -
Word Break II
枚举的题,一般用DFS+Backtracking做,此题会超时,那么要辅助memoization, 保存之前得到的结果.怎么mem?1,借鉴word break I, 保存能否该index之后能否break: boolean[s.length() + 1]2, 保存详细的分法结果: HashMap>public class Solution { List res =转载 2014-10-04 03:19:24 · 391 阅读 · 0 评论 -
Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to转载 2014-10-21 20:58:48 · 270 阅读 · 0 评论