自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(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 265

转载 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 307

转载 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 278

转载 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 255

转载 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 298

转载 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 222

转载 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 227

转载 Container with most water

two pointers, 两头往中间走

2014-11-10 12:19:53 208

转载 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 240

转载 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 305

转载 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 283 1

转载 remove duplicates from sorted list, remove nth node

Linkedlist中的remove,记得要构造fakehead!

2014-11-10 03:40:55 270

转载 remove element

Remove系列:two pointers类型

2014-11-10 03:28:55 295

转载 longest substring without repeating characters

Sliding window类的题类似maximum subarray, maximum product subarray

2014-11-10 00:43:25 254

转载 Permutation I,II

还要有多典型的DFS +  backtracking,之前写的乱七八糟凑出来的,后来用了这个si

2014-10-30 23:52:15 257

转载 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 246

转载 Interleaving String

先保存一下 某个大神的DP专辑:http://www.cnblogs.com/remlostime/tag/DP/

2014-10-19 21:26:55 283

转载 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 345

转载 Subsets I, II

DFS + BacktrackingII是考虑原set中有重复元素的情况Subset ISubset I

2014-10-19 21:02:17 314

转载 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 283

转载 Maximum Product Subarray

最值问题的第二种思路,保存局部最优,更新全局最优求P

2014-10-19 20:53:57 223

转载 Sudoku Solver

找到存在的解:DFS + Backtracking因为ji

2014-10-13 00:12:31 248

转载 Valid Sudoku

1.标记 Visited:a. 直接在array中改 (bi'r

2014-10-12 23:20:10 251

转载 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 267

转载 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 367

转载 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 472

转载 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 297

转载 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 244

转载 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 298

转载 Divide two integers

注意dividend是0的情况,这个时候sum总是比被除数大的!尼玛考虑负数的情况!而且还有溢出的情况。。。。 -(-2147483648)溢出啦 亲。。。要从O(n) -> O(lgn) -> O(1)

2014-10-09 12:07:58 239

转载 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 261

转载 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 274

转载 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 215

转载 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 232

转载 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 232

转载 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 215

转载 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 267

原创 资料汇总篇

Leetcode一些好的网站:1,九章算法da

2014-10-04 12:13:53 269

转载 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 285

转载 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 370

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除