- 博客(42)
- 收藏
- 关注
原创 深入一下hashcode
1. java String中hashcode计算公式:h(s)=s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]为什么取31?31是odd prime number,不取偶数,因为一个数乘以2相当于移位操作,如有溢出会导致information lost31*i = (32 - 1)*i = i -> http://stacko
2016-06-29 15:11:24
295
转载 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
330
转载 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
293
转载 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
279
转载 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
325
转载 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
250
转载 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
246
转载 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
264
转载 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
转载 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
302
转载 remove duplicates from sorted list, remove nth node
Linkedlist中的remove,记得要构造fakehead!
2014-11-10 03:40:55
293
转载 longest substring without repeating characters
Sliding window类的题类似maximum subarray, maximum product subarray
2014-11-10 00:43:25
278
转载 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
转载 Interleaving String
先保存一下 某个大神的DP专辑:http://www.cnblogs.com/remlostime/tag/DP/
2014-10-19 21:26:55
302
转载 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
365
转载 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
309
转载 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
286
转载 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
387
转载 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
转载 Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]
2014-10-09 12:26:47
317
转载 Word Search
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
2014-10-09 12:20:31
264
转载 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
314
转载 Divide two integers
注意dividend是0的情况,这个时候sum总是比被除数大的!尼玛考虑负数的情况!而且还有溢出的情况。。。。 -(-2147483648)溢出啦 亲。。。要从O(n) -> O(lgn) -> O(1)
2014-10-09 12:07:58
255
转载 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
转载 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
293
转载 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
转载 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
246
转载 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
转载 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
转载 DP啊DP
1, Given a list of N coins, their values (V1, V2, ... , VN), and the total sum S. Find the minimum number of coins the sum of which is S (we can use as many coins of one type as we want), or r
2014-10-05 00:38:37
289
转载 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
302
转载 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
390
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人