09 2017 档案
摘要:class Solution { public int minimumTotal(List> triangle) { int[] dp=new int[triangle.size()]; for(int i=triangle.size()-1;i>=0;i--) for(int j=0;j<=i;j++) ...
阅读全文
摘要:public class Solution { public void connect(TreeLinkNode root) { TreeLinkNode lstart=new TreeLinkNode(0); while(root!=null) { TreeLinkNode cur=lstart; ...
阅读全文
摘要:public class Solution { public void connect(TreeLinkNode root) { TreeLinkNode startNode=root; while(startNode!=null) { TreeLinkNode node=startNode; ...
阅读全文
摘要:class Solution { public int numDistinct(String s, String t) { int[][] dp=new int[t.length()+1][s.length()+1]; for(int i=0;i<=t.length();i++) for(int j=i;j<=s.length();...
阅读全文
摘要:class Solution { public void flatten(TreeNode root) { TreeNode cur=root, pre=null; while(cur!=null) { if(cur.left!=null) { pre=cur....
阅读全文
摘要:class Solution { public List> pathSum(TreeNode root, int sum) { List> res=new ArrayList>(); pathSum(root, sum, new ArrayList(), res); return res; } private void pa...
阅读全文
摘要:class Solution { ListNode lnode=null; public TreeNode sortedListToBST(ListNode head) { int size=0; ListNode p=head; while(p!=null) { p=p.next; ...
阅读全文
摘要:public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { return buildTree(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1); } private TreeNo...
阅读全文
摘要:public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { return buildTree(preorder, 0, preorder.length-1, inorder, 0, inorder.length); } private TreeNode bu...
阅读全文
摘要:class Solution { public List> zigzagLevelOrder(TreeNode root) { List> res=new ArrayList>(); if(root==null) return res; Queue queue=new LinkedList(); Li...
阅读全文
摘要:class Solution { public List> levelOrder(TreeNode root) { List> res=new ArrayList>(); if(root==null) return res; Queue queue=new LinkedList(); queue.ad...
阅读全文
摘要:class Solution { public void recoverTree(TreeNode root) { TreeNode cur=root,pre=null,node1=null,node2=null, p=null; while(cur!=null) { if(cur.left!=null) ...
阅读全文
摘要:public class Solution { public boolean isValidBST(TreeNode root) { Stack stack=new Stack(); TreeNode cur=null; while(root!=null||!stack.isEmpty()) { ...
阅读全文
摘要:class Solution { public boolean isInterleave(String s1, String s2, String s3) { if(s1.length()+s2.length()!=s3.length()) return false; boolean [][] dp=new boolean[s1.l...
阅读全文
摘要:public class Solution { public int numTrees(int n) { int[] dp=new int[n+1]; dp[0]=1; dp[1]=1; for(int i=2;i<=n;i++) for(int j=1;j<=i;j++) ...
阅读全文
摘要:class Solution { public List generateTrees(int n) { if(n==0) return new ArrayList(); return generateTrees(1, n); } private List generateTrees(int i, int j) { ...
阅读全文
摘要:public class Solution { public List inorderTraversal(TreeNode root) { List res=new ArrayList(); Stack stack=new Stack(); while(root!=null||!stack.isEmpty()) { ...
阅读全文
摘要:class Solution { public List restoreIpAddresses(String s) { List res=new ArrayList(); restoreIpAddresses("", 0, 0, s, res); return res; } private void restoreIpAdd...
阅读全文
摘要:class Solution { public ListNode reverseBetween(ListNode head, int m, int n) { ListNode pre=new ListNode(0); pre.next=head; ListNode p=pre; for(int i=0;i0) ...
阅读全文
摘要:public class Solution { public int numDecodings(String s) { if(s.length()==0) return 0; int[] dp=new int[s.length()+1]; dp[0]=1; for(int i=0;i'0'&&s.ch...
阅读全文
摘要:public class Solution { public List> subsetsWithDup(int[] nums) { List> res=new ArrayList>(); Arrays.sort(nums); subsetsWithDup(0, new ArrayList(), res, nums); ret...
阅读全文
摘要:1st bit: 0->1->1->0->0->1->1->0 2nd bit: 0->0->1->1->1->1->0->0 3rd bit: 0->0->0->0->1->1->1->1
阅读全文
摘要:class Solution { public boolean isScramble(String s1, String s2) { if(s1.length()==0||s1.equals(s2)) return true; int[] cnt=new int[128]; for(int i=0;i<s1.leng...
阅读全文
摘要:class Solution { public ListNode partition(ListNode head, int x) { ListNode l1=new ListNode(0); ListNode l2=new ListNode(0); ListNode pre=new ListNode(0); pre.next...
阅读全文
摘要:class Solution { public int maximalRectangle(char[][] matrix) { if(matrix.length==0||matrix[0].length==0) return 0; int[] heights=new int[matrix[0].length]; in...
阅读全文
摘要:class Solution { public int largestRectangleArea(int[] heights) { Stack stack=new Stack(); int maxArea=0; for(int i=0;i<=heights.length;i++) { int h=i<...
阅读全文
摘要:class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode pre=new ListNode(0); pre.next=head; ListNode p=pre; while(p!=null&&p.next!=null) ...
阅读全文
摘要:class Solution { public boolean search(int[] nums, int target) { if(nums.length==0) return false; int lo=0; int hi=nums.length-1; while(lonums[hi]) ...
阅读全文
摘要:public class Solution { public int removeDuplicates(int[] nums) { if(nums.length nums[i-2]) nums[i++] = n; return i; }
阅读全文
摘要:class Solution { public boolean exist(char[][] board, String word) { if(board.length==0||board[0].length==0) return false; boolean[][] used=new boolean[board.length][b...
阅读全文
摘要:class Solution { public List> subsets(int[] nums) { List> res=new ArrayList>(); subsets(0, new ArrayList(), res, nums); return res; } private void subsets(int idx,...
阅读全文
摘要:class Solution { public List> combine(int n, int k) { List> res=new ArrayList>(); combine(n, k, new ArrayList(), res); return res; } private void combine(int num, ...
阅读全文
摘要:class Solution { public String minWindow(String s, String t) { boolean[] chs=new boolean[128]; int[] cnt=new int[128]; for(int i=0;i0&&r0) count--; ...
阅读全文
摘要:public class Solution { public void sortColors(int[] nums) { int lo=0; int hi=nums.length-1; int idx=0; while(idx<=hi) { if(nums[idx]==0) ...
阅读全文
摘要:public class Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix.length==0||matrix[0].length==0) return false; int m=matrix.length; ...
阅读全文
摘要:public class Solution { public void setZeroes(int[][] matrix) { if(matrix.length==0||matrix[0].length==0) return; boolean row0=false; for(int i=0;i=0;i--) ...
阅读全文
摘要:class Solution { public int minDistance(String word1, String word2) { int[][] dp=new int[word1.length()+1][word2.length()+1]; for(int i=0;i<=word1.length();i++) dp[i][...
阅读全文
摘要:class Solution { public String simplifyPath(String path) { Stack stack=new Stack(); String[] strs=path.split("\\/"); for(int i=0;i<strs.length;i++) { i...
阅读全文
摘要:class Solution { public List fullJustify(String[] words, int maxWidth) { List res=new ArrayList(); int idx=0; while(idx0) { sb....
阅读全文
摘要:class Solution { public boolean isNumber(String s) { s=s.trim(); int idx=s.indexOf('e'); if(idx>0) return isNum(s.substring(0,idx), false)&&isNum(s.substring(i...
阅读全文
摘要:class Solution { public int minPathSum(int[][] grid) { for(int i=0;i0&&j>0) grid[i][j]+=Math.min(grid[i-1][j],grid[i][j-1]); else if(j>0) ...
阅读全文
摘要:class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { for(int i=0;i=0?1:0; for(int i=0;i=0) obstacleGrid[i][j]+=(i>0&&obstacleGrid[i-1][...
阅读全文
摘要:class Solution { public int uniquePaths(int m, int n) { int[][] dp=new int[m][n]; dp[0][0]=1; for(int i=0;i0?dp[i-1][j]:0)+(j>0?dp[i][j-1]:0); return dp[m-1][n-1];...
阅读全文
摘要:class Solution { public ListNode rotateRight(ListNode head, int k) { ListNode preNode=new ListNode(0); preNode.next=head; ListNode p=preNode; int len=0; wh...
阅读全文
摘要:Base on next permutation:
阅读全文
摘要:class Solution { public int[][] generateMatrix(int n) { int[][] matrix=new int[n][n]; generateMatrix(0, 0, 0, n-1, n-1, matrix); return matrix; } private void gene...
阅读全文
摘要:class Solution { public List insert(List intervals, Interval newInterval) { int idx=0; while(idx res=new ArrayList(); int start=intervals.get(0).start; int end=int...
阅读全文
摘要:class Solution { public List merge(List intervals) { List res=new ArrayList(); if(intervals.size()==0) return res; Collections.sort(intervals, (a,b)->a.start-b...
阅读全文
摘要:public class Solution { public boolean canJump(int[] nums) { int maxDistance=0; for(int i=0;i=nums.length-1) return true; } return false; ...
阅读全文
摘要:public class Solution { public List spiralOrder(int[][] matrix) { List res=new ArrayList(); if(matrix.length==0||matrix[0].length==0) return res; spiralOrder(0...
阅读全文
摘要:class Solution { public int totalNQueens(int n) { int[][] board=new int[n][n]; return totalNQueens(0, board); } private int totalNQueens(int i, int[][] board) { ...
阅读全文
摘要:class Solution { public List> solveNQueens(int n) { int[][] board=new int[n][n]; List> res=new ArrayList>(); nQueens(0,board,res); return res; } private vo...
阅读全文
摘要:public class Solution { public double myPow(double x, int n) { if(n==0) return 1; if(n < 0) return 1/(x*myPow(x, -(n+1))); double t = myPow(x,n/2)...
阅读全文
摘要:class Solution { public List> groupAnagrams(String[] strs) { Map> map=new HashMap>(); for(String str: strs) { char[] arr=str.toCharArray(); Arrays....
阅读全文
摘要:class Solution { public void rotate(int[][] matrix) { int n=matrix.length; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) swap(matrix,i,j,j,i); fo...
阅读全文
摘要:public class Solution { public List> permuteUnique(int[] nums) { List> ret=new ArrayList>(); Arrays.sort(nums); boolean[] used=new boolean[nums.length]; permute(ne...
阅读全文
摘要:public class Solution { public List> permute(int[] nums) { List> ret=new ArrayList>(); permute(new ArrayList(),ret,nums); return ret; } private void permute(List l...
阅读全文
摘要:class Solution { public int jump(int[] nums) { int steps=0; int max=0; int cur=0; for(int i=0;i<nums.length-1;i++) { cur=Math.max(cur, i+nums[i...
阅读全文
摘要:class Solution { public boolean isMatch(String s, String p) { boolean[][] dp=new boolean[p.length()+1][s.length()+1]; dp[0][0]=true; for(int i=1;i0&&(p.charAt(i-1)==s.char...
阅读全文
摘要:public class Solution { public String multiply(String num1, String num2) { int[] digits=new int[num1.length()+num2.length()]; for(int i=0;i=0;i--) { if(i>0) ...
阅读全文
摘要:class Solution { public int trap(int[] height) { int sum=0; int l=0; int r=height.length-1; int left=0; int right=0; while(lleft) ...
阅读全文
摘要:class Solution { public int firstMissingPositive(int[] nums) { int i=0; while(i0&&nums[i]<=nums.length&&nums[i]!=nums[nums[i]-1]) { int tmp=nums[i]; ...
阅读全文
摘要:class Solution { public List> combinationSum2(int[] candidates, int target) { List> ret=new ArrayList>(); Arrays.sort(candidates); boolean[] used=new boolean[candidates.le...
阅读全文
摘要:public class Solution { public List> combinationSum(int[] candidates, int target) { List> ret=new ArrayList>(); Arrays.sort(candidates); generateCombination(new ArrayList(...
阅读全文
摘要:class Solution { public void solveSudoku(char[][] board) { boolean[][][] used=new boolean[3][9][9]; for(int i=0;i<9;i++) for(int j=0;j<9;j++) if(board[...
阅读全文
摘要:public class Solution { public boolean isValidSudoku(char[][] board) { boolean[][][] used=new boolean[3][9][9]; for(int i=0;i<9;i++) for(int j=0;j<9;j++) ...
阅读全文
摘要:public class Solution { public int[] searchRange(int[] nums, int target) { return new int[]{binarySearch(nums,target,true),binarySearch(nums,target,false)}; } private int binarySe...
阅读全文
摘要:public class Solution { public int search(int[] nums, int target) { int l=0; int r=nums.length-1; while(l<r) { int m=l+(r-l)/2; if(nums[m]=...
阅读全文
摘要:class Solution { public int longestValidParentheses(String s) { int left=-1; Stack stack=new Stack(); int ret=0; for(int i=0;i<s.length();i++) { ...
阅读全文
摘要:class Solution { public void nextPermutation(int[] nums) { int i=nums.length-1; while(i>0&&nums[i-1]>=nums[i])i--; if(i==0) { Arrays.sort(nums); ...
阅读全文
摘要:class Solution { public List findSubstring(String s, String[] words) { List ret=new ArrayList(); if(words.length==0||words[0].length()==0||s.length()==0) return ret; ...
阅读全文
摘要:public class Solution { public int divide(int dividend, int divisor) { boolean isNegtive=dividend0||dividend>0&&divisor=ldivisor) { if(ldividend>=l) { ...
阅读全文
摘要:http://blog.csdn.net/v_july_v/article/details/7041827
阅读全文
摘要:class Solution { public ListNode reverseKGroup(ListNode head, int k) { if(head==null||k==1) return head; ListNode preNode=new ListNode(0); preNode.next=head; ...
阅读全文
摘要:class Solution { public ListNode mergeKLists(ListNode[] lists) { PriorityQueue que=new PriorityQueue((a,b)->a.val-b.val); for(ListNode list:lists) if(list!=null) ...
阅读全文
摘要:public class Solution { public List generateParenthesis(int n) { List ret=new ArrayList(); generateParenthesis("", 0, 2*n, ret); return ret; } void generateParenth...
阅读全文
摘要:public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode pre=new ListNode(0); pre.next=head; ListNode p=pre,q=pre; for(int i=0;i<n...
阅读全文
摘要:public class Solution { public List> fourSum(int[] nums, int target) { List> ret=new ArrayList>(); Arrays.sort(nums); for(int i=0;i<nums.length;i++) i...
阅读全文
摘要:public class Solution { public List letterCombinations(String digits) { LinkedList que=new LinkedList(); if(digits.length()==0) return que; String[] btns=new S...
阅读全文
摘要:class Solution { public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int ret=nums[0]+nums[1]+nums[2]; for(int i=0;i<nums.length;i++) { ...
阅读全文
摘要:class Solution { public List> threeSum(int[] nums) { List> ret=new ArrayList>(); Arrays.sort(nums); for(int i=0;i0) r--; else ...
阅读全文
摘要:class Solution { public String intToRoman(int num) { String[][] r=new String[][]{{"","M","MM","MMM"}, {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}, ...
阅读全文
摘要:class Solution { public int maxArea(int[] height) { int maxArea=0; int i=0; int j=height.length-1; while(i<j) { maxArea=Math.max(maxArea,Math.m...
阅读全文
摘要:Recursive DP
阅读全文
摘要:class Solution { public int myAtoi(String str) { str=str.trim(); if(str.length()==0) return 0; int flag=1; int i=0; if(str.charAt(i)=='+') ...
阅读全文
摘要:class Solution { public String convert(String s, int numRows) { if(numRows==1) return s; int divisor=(numRows-1)*2; StringBuilder sb=new StringBuilder(); ...
阅读全文
摘要:Brutal Force: O(n^2) Manacher: O(n)
阅读全文
摘要:public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int l=(nums1.length+nums2.length+1)>>1; int r=(nums1.length+nums2.length+2)>>1; re...
阅读全文
摘要:class Solution { public int lengthOfLongestSubstring(String s) { Map map=new HashMap(); int maxlen=0; for(int j=0,i=0;i<s.length();i++) { char c=s.char...
阅读全文
摘要:class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode ret=new ListNode(0); ListNode l3=ret; int carry=0; while(l1!=null||l2!=null||ca...
阅读全文

浙公网安备 33010602011771号