├── README.md ├── Git.java ├── Basic.java ├── Lint8.java ├── Test.java ├── Leet101.java ├── Leet111.java ├── Leet116.java ├── Leet202.java ├── Leet231.java ├── Leet241.java ├── Leet257.java ├── Leet264.java ├── Leet313.java ├── Leet326.java ├── Leet342.java ├── Leet445.java ├── Leet455.java ├── Leet474.java ├── Leet494.java ├── Leet54.java ├── Lint532.java ├── Lint69.java ├── Review.java ├── Leet498.1.java ├── Leet51+52+39+40+216.java ├── Stock.java ├── Leet464.java ├── Leet434.java ├── Leet495.java ├── .gitignore ├── Leet59.java ├── Leet77.java ├── Leet129.java ├── CompletePackage.java ├── Leet112.java ├── Leet236.java ├── Lint68.java ├── ZeroOne.java ├── Package.java └── Leet113.java /README.md: -------------------------------------------------------------------------------- 1 | "# Leetcode" 2 | -------------------------------------------------------------------------------- /Git.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Git.java -------------------------------------------------------------------------------- /Basic.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Basic.java -------------------------------------------------------------------------------- /Lint8.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Lint8.java -------------------------------------------------------------------------------- /Test.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Test.java -------------------------------------------------------------------------------- /Leet101.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet101.java -------------------------------------------------------------------------------- /Leet111.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet111.java -------------------------------------------------------------------------------- /Leet116.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet116.java -------------------------------------------------------------------------------- /Leet202.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet202.java -------------------------------------------------------------------------------- /Leet231.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet231.java -------------------------------------------------------------------------------- /Leet241.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet241.java -------------------------------------------------------------------------------- /Leet257.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet257.java -------------------------------------------------------------------------------- /Leet264.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet264.java -------------------------------------------------------------------------------- /Leet313.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet313.java -------------------------------------------------------------------------------- /Leet326.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet326.java -------------------------------------------------------------------------------- /Leet342.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet342.java -------------------------------------------------------------------------------- /Leet445.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet445.java -------------------------------------------------------------------------------- /Leet455.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet455.java -------------------------------------------------------------------------------- /Leet474.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet474.java -------------------------------------------------------------------------------- /Leet494.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet494.java -------------------------------------------------------------------------------- /Leet54.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet54.java -------------------------------------------------------------------------------- /Lint532.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Lint532.java -------------------------------------------------------------------------------- /Lint69.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Lint69.java -------------------------------------------------------------------------------- /Review.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Review.java -------------------------------------------------------------------------------- /Leet498.1.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet498.1.java -------------------------------------------------------------------------------- /Leet51+52+39+40+216.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/situjun/LeetCode/HEAD/Leet51+52+39+40+216.java -------------------------------------------------------------------------------- /Stock.java: -------------------------------------------------------------------------------- 1 | public class Stock{ 2 | public static void main(String[] args){ 3 | int[] stock = {13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7}; 4 | int[] F = new int[stock.length]; 5 | int max = F[0] = stock[0]; 6 | for(int i=1;i>1; 9 | y = y>>1; 10 | //System.out.println("x"+x+"y"+y); 11 | } 12 | return count; 13 | } 14 | } -------------------------------------------------------------------------------- /Leet434.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int countSegments(String s) { 3 | if(s == null || s.equals("")) return 0; 4 | s = s.trim(); 5 | int count = 0; 6 | boolean tag = false; 7 | for(int i =0;i<=s.length()-1;i++){ 8 | if(!tag && s.charAt(i) != ' '){ 9 | count++; 10 | tag =true; 11 | } else if(s.charAt(i) == ' '){ 12 | tag = false; 13 | } 14 | } 15 | return count; 16 | } 17 | } -------------------------------------------------------------------------------- /Leet495.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findPoisonedDuration(int[] timeSeries, int duration) { 3 | if(timeSeries.length == 0) return 0; 4 | if(timeSeries.length == 1) return duration; 5 | int result = 0; 6 | for(int i = 1;i<=timeSeries.length-1;i++){ 7 | if(timeSeries[i] - timeSeries[i-1] <= duration) result += timeSeries[i] - timeSeries[i-1]; 8 | else result += duration; 9 | } 10 | result += duration; 11 | return result; 12 | } 13 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | # Android Studio Navigation editor temp files 29 | .navigation/ 30 | 31 | # Android Studio captures folder 32 | captures/ 33 | -------------------------------------------------------------------------------- /Leet59.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[][] generateMatrix(int n) { 3 | 4 | int[][] matrix = new int[n][n]; 5 | int x1 = 0,x2 = n-1,y1 = 0,y2 = n-1; 6 | int count = 1,sum = n*n; 7 | while(x1<=x2 && y1 <= y2){ 8 | for(int i = y1;i<=y2;i++) matrix[x1][i] = count++; 9 | x1++; 10 | for(int i = x1;i<=x2;i++) matrix[i][y2] = count++; 11 | y2--; 12 | if(y1<=y2) for(int i = y2;i>=y1;i--) matrix[x2][i] = count++; 13 | x2--; 14 | if(x1<=x2) for(int i = x2;i>=x1;i--) matrix[i][y1] = count++; 15 | y1++; 16 | } 17 | return matrix; 18 | } 19 | } -------------------------------------------------------------------------------- /Leet77.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List> combine(int n, int k) { 3 | List> list = new ArrayList<>(); 4 | List item = new ArrayList<>(); 5 | helper(list,item,n,k,1); 6 | return list; 7 | } 8 | public void helper(List> list,List item,int n,int k,int start){ 9 | if(item.size() == k){ 10 | list.add(new ArrayList<>(item)); 11 | } else { 12 | for(int i = start;i<=n;i++){ 13 | item.add(i); 14 | helper(list,item,n,k,i+1); 15 | item.remove(item.size()-1); 16 | } 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Leet129.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | int sum = 0; 12 | public int sumNumbers(TreeNode root) { 13 | helper(root,0); 14 | return sum; 15 | } 16 | public void helper(TreeNode node,int num){ 17 | if(node != null){ 18 | if(node.left == null && node.right == null){ 19 | sum = sum+num*10+node.val; 20 | } else { 21 | helper(node.left,num*10+node.val); 22 | helper(node.right,num*10+node.val); 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /CompletePackage.java: -------------------------------------------------------------------------------- 1 | class CompletePackage{ 2 | public static void main(String[] args){ 3 | int[] val = {5,1,5,4,2}; 4 | int[] weight = {4,2,4,5,3}; 5 | 6 | int length= weight.length; 7 | int limit = 20; 8 | int F[] = new int[limit]; 9 | 10 | for(int i= 0;i<=limit-1;i++){ 11 | F[i] = 0; 12 | for(int j =0;j<=length-1;j++){ 13 | if(i-weight[j]>=0){ 14 | F[i] = Math.max(F[i],F[i-weight[j]]+val[j]); 15 | } 16 | } 17 | } 18 | System.out.println("The max value is "+F[limit-1]); 19 | System.out.println("Entire maxtrix is:"); 20 | for(int i =0;i<=limit-1;i++){ 21 | System.out.print(i+"\t"); 22 | } 23 | System.out.print("\n"); 24 | for(int i:F){ 25 | System.out.print(i+"\t"); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Leet112.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public boolean hasPathSum(TreeNode root, int sum) { 12 | return helper(root,sum,0); 13 | } 14 | public boolean helper(TreeNode node,int target,int sum){ 15 | if(node!=null){ 16 | if(node.left == null && node.right == null){ 17 | if(sum+node.val == target) return true; 18 | } else { 19 | return helper(node.left,target,sum+node.val)||helper(node.right,target,sum+node.val)?true:false; 20 | } 21 | } 22 | return false; 23 | } 24 | } -------------------------------------------------------------------------------- /Leet236.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 12 | return helper(root,p,q); 13 | } 14 | public TreeNode helper(TreeNode node,TreeNode p,TreeNode q){ 15 | if(node != null){ 16 | if(node == p || node == q){ 17 | return node; 18 | } else { 19 | TreeNode left = helper(node.left,p,q); 20 | TreeNode right = helper(node.right,p,q); 21 | return left != null && right != null?node:left==null?right:left; 22 | } 23 | } 24 | return null; 25 | } 26 | } -------------------------------------------------------------------------------- /Lint68.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition of TreeNode: 3 | * public class TreeNode { 4 | * public int val; 5 | * public TreeNode left, right; 6 | * public TreeNode(int val) { 7 | * this.val = val; 8 | * this.left = this.right = null; 9 | * } 10 | * } 11 | */ 12 | public class Solution { 13 | /** 14 | * @param root: The root of binary tree. 15 | * @return: Postorder in ArrayList which contains node values. 16 | */ 17 | public ArrayList postorderTraversal(TreeNode root) { 18 | // write your code here 19 | ArrayList list = new ArrayList<>(); 20 | helper(list,root); 21 | return list; 22 | } 23 | public void helper(ArrayList list,TreeNode node){ 24 | if(node != null){ 25 | helper(list,node.left); 26 | helper(list,node.right); 27 | list.add(node.val); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /ZeroOne.java: -------------------------------------------------------------------------------- 1 | class ZeroOne{ 2 | public static void main(String[] args){ 3 | int[] V = {6,3,5,4,6}; 4 | int[] N = {2,2,6,5,4}; 5 | int limit = 10; 6 | int max = 0; 7 | int[][] arr = new int[N.length+1][limit+1]; 8 | //matrix size is N*(Package volume) 9 | for(int i =0;i<=N.length;i++){ 10 | arr[i][0] = 0 ; 11 | } 12 | for(int i=0;i<=limit;i++){ 13 | arr[0][i] = 0; 14 | } 15 | for(int i =1;i<=N.length;i++){ 16 | for(int j = 1;j<=limit;j++){ 17 | if(j-N[i-1] >=0){ 18 | arr[i][j] = Math.max(arr[i-1][j],arr[i-1][j-N[i-1]]+V[i-1]); 19 | } else { 20 | arr[i][j] = arr[i-1][j]; 21 | } 22 | } 23 | } 24 | System.out.println("The max value is"+arr[N.length][limit]); 25 | System.out.println("Entire maxtrix is:"); 26 | for(int i=1;i<=N.length;i++){ 27 | for(int j =1;j<=limit;j++){ 28 | System.out.print(arr[i][j]+"\t"); 29 | } 30 | System.out.print("\n"); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /Package.java: -------------------------------------------------------------------------------- 1 | //01 package 2 | // ********* Item weight and value ************* 3 | // Order: 1 2 3 4 5 4 | // Value: 6 3 5 4 6 5 | // Weight: 2 2 6 5 4 6 | //F[i][j] 是前i个物品放入容积j中,而不是第i个物品放入j中。(如F[3][5]是前三个物品放入(Order:1&2&3),而不是仅仅第3个物品。) 7 | public class Package{ 8 | public static void main(String []args){ 9 | int[] val = {6,3,5,4,6}; 10 | int[] weight = {2,2,6,5,4}; 11 | int N,V; 12 | N = val.length; 13 | V = 10; 14 | int[][] F = new int[N+1][V+1]; 15 | for(int i=0;i<=V;i++){ 16 | F[0][i] = 0; 17 | } 18 | for(int i=0;i<=N;i++){ 19 | F[i][0] = 0; 20 | } 21 | for(int i=1;i<=N;i++){ 22 | for(int j=1;j<=V;j++){ 23 | if(weight[i-1]>j){ 24 | F[i][j] = F[i-1][j]; 25 | } else { 26 | F[i][j] = Math.max(F[i-1][j],F[i-1][j-weight[i-1]]+val[i-1]); 27 | } 28 | } 29 | } 30 | for(int i=0;i<=N;i++){ 31 | for(int j=0;j<=V;j++){ 32 | System.out.print(F[i][j]+","); 33 | } 34 | System.out.print("\n"); 35 | } 36 | } 37 | public void Package(){ 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /Leet113.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public List> pathSum(TreeNode root, int sum) { 12 | List> list = new ArrayList<>(); 13 | helper(root,list,sum,0,""); 14 | return list; 15 | } 16 | public void helper(TreeNode node,List> list,int target,int sum,String s){ 17 | if(node != null){ 18 | if(node.left == null && node.right == null){ 19 | if(sum+node.val == target){ 20 | s = s+node.val; 21 | String[] arr = s.split(","); 22 | List item = new ArrayList<>(); 23 | for(String num:arr) item.add(Integer.parseInt(num)); 24 | list.add(new ArrayList<>(item)); 25 | } 26 | } else { 27 | helper(node.left,list,target,sum+node.val,s+node.val+","); 28 | helper(node.right,list,target,sum+node.val,s+node.val+","); 29 | } 30 | } 31 | } 32 | } --------------------------------------------------------------------------------