- 博客(120)
- 收藏
- 关注
原创 28. Implement strStr()
public class Solution { public int strStr(String haystack, String needle) { if(haystack == null || needle == null || needle.length() > haystack.length()) { return -1; }e
2017-03-26 19:28:41
240
原创 139. Word Break
public class Solution { boolean find = false; public boolean wordBreak(String s, List<String> wordDict) { boolean[] f = new boolean[s.length()+1]; f[0] = true; for(int i =
2017-03-18 11:41:51
250
原创 47. Permutations II
public class Solution { public List<List<Integer>> permuteUnique(int[] nums) { Set<List<Integer>> set = new HashSet<>(); perm(set,new ArrayList<>(),0,nums); List<List<Intege
2017-03-17 23:51:39
248
原创 130. Surrounded Regions
public class Solution { public void solve(char[][] board) { if(board == null || board.length <=2 || board[0].length <= 2) { return; } int m = board.length, n = b
2017-03-15 22:28:12
241
原创 32. Longest Valid Parentheses
public class Solution { public int longestValidParentheses(String s) { if(s == null || s.length() <=1) { return 0; } int[] f = new int[s.length()]; int m
2017-03-15 21:33:06
183
原创 230. Kth Smallest Element in a BST
public class Solution { int count = 0; int value = 0; public int kthSmallest(TreeNode root, int k) { dfs(root,k); return value; } public void dfs(TreeNode root,int k)
2017-03-11 15:24:44
201
原创 22. Generate Parentheses
public class Solution { public List<String> generateParenthesis(int n) { List<String> list = new ArrayList<>(); if(n<=0) { return list; } dfs(list,0,2*n,
2017-03-11 15:14:04
217
原创 401. Binary Watch
public class Solution { public List<String> readBinaryWatch(int num) { List<String> list = new ArrayList<>(); if(num==0) { list.add("0:00"); return list;
2017-03-11 13:34:45
201
原创 242. Valid Anagram
public class Solution { public boolean isAnagram(String s, String t) { int ls = s.length(); int lt = t.length(); if(ls!= lt) { return false; } cha
2017-03-11 12:48:53
214
原创 public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p== null
public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p== null && q== null) { return true; }else if(p != null && q==null || p==null && q!= null)
2017-03-11 12:43:21
654
原创 404. Sum of Left Leaves
public class Solution { int sum = 0; public int sumOfLeftLeaves(TreeNode root) { dfs(root); return sum; } public void dfs(TreeNode root) { if(root == null) {
2017-03-11 11:12:04
207
原创 504. Base 7
public class Solution { public String convertToBase7(int num) { int nums = num; if(nums >=-6 && nums <= 6) { return nums+""; } boolean isne = num>=0?fals
2017-03-11 10:56:47
212
原创 453. Minimum Moves to Equal Array Elements
public class Solution { /* public int minMoves(int[] nums) { if(nums == null || nums.length == 1) { return 0; } int min = nums[0]; for(int i = 1; i< nums.l
2017-03-11 10:46:20
168
原创 455. Assign Cookies
public class Solution { public int findContentChildren(int[] g, int[] s) { if(g == null || s == null || g.length == 0 || s.length == 0) { return 0; } Arrays.sort
2017-03-11 09:14:54
167
原创 506. Relative Ranks
public class Solution { class Rank { int pos; int score; Rank(int pos, int score) { this.pos = pos; this.score = score; } } public St
2017-03-11 08:35:39
215
原创 258. Add Digits
public class Solution { public int addDigits(int num) { int sum = 0; while(num>=10) { sum = 0; while(num!=0) { sum+=(num%10);
2017-03-10 23:55:20
175
原创 104. Maximum Depth of Binary Tree
public class Solution { public int maxDepth(TreeNode root) { if(root == null) { return 0; }else { int left = maxDepth(root.left)+1; int right = m
2017-03-10 23:46:18
177
原创 495. Teemo Attacking
public class Solution { public int findPoisonedDuration(int[] timeSeries, int duration) { if(timeSeries == null || timeSeries.length == 0) { return 0; }else if(timeSerie
2017-03-10 23:41:14
206
原创 508. Most Frequent Subtree Sum
public class Solution { int max = 1; public int[] findFrequentTreeSum(TreeNode root) { Map<Integer,Integer> map = new HashMap<>(); dfs(map,root); int maxi = 0; for(I
2017-03-10 23:28:02
168
原创 520. Detect Capital
public class Solution { public boolean detectCapitalUse(String word) { int count = 0; String str = word.toLowerCase(); for(int i = 0; i < word.length();i++) { if(
2017-03-10 22:38:18
161
原创 496. Next Greater Element I
public class Solution { public int[] nextGreaterElement(int[] findNums, int[] nums) { int lf = findNums.length; int[] res = new int[lf]; int i=0,j=0; for(i=0;i<lf;i+
2017-03-10 22:13:23
155
原创 338. Counting Bits
public class Solution { public int[] countBits(int num) { List<Integer> list = new ArrayList<>(); for(int i=0;i<=num;i++) { list.add(cacuate(i)); } int[]
2017-03-10 21:54:45
133
原创 500. Keyboard Row
public class Solution { public String[] findWords(String[] words) { String[] keys={"qwertyuiopQWERTYUIOP","asdfghjklASDFGHJKL","zxcvbnmZXCVBNM"}; List<String> list = new ArrayLis
2017-03-10 21:42:37
170
原创 70. Climbing Stairs
public class Solution { public int climbStairs(int n) { if(n<=0) { return 0; } if(n==1 || n == 2) { return n; } int[] nums = new int[
2017-03-10 16:48:34
147
原创 87. Scramble String
public class Solution { public boolean isScramble(String s1, String s2) { if (s1.length() != s2.length()) return false; if (s1.equals(s2)) return true; char[] c1 = s1.t
2017-03-10 14:39:28
153
原创 91. Decode Ways
public class Solution { public int numDecodings(String s) { if(s == null || s.length() == 0) { return 0; } if(s.length()==1) { return s.charAt(0) == '
2017-03-10 14:14:54
132
原创 131. Palindrome Partitioning
public class Solution { public void help(boolean f[][],String s,List<List<String>> result, List<String> path, int pos) { if(s.length() == pos) { result.add(new ArrayList<>(path)
2017-03-10 10:48:15
111
原创 115. Distinct Subsequences
public class Solution { public int numDistinct(String s, String t) { int[][] nums = new int[t.length()+1][s.length()+1]; for(int i = 0; i< s.length()+1;i++) { nums[0][i]
2017-03-10 09:56:36
133
原创 96. Unique Binary Search Trees
public class Solution { public int numTrees(int n) { int[] res = new int[n+1]; res[0]=1; res[1]=1; for(int i = 2; i<= n; i++) { for(int j = 0; j < i; j++
2017-03-09 22:23:03
122
原创 97. Interleaving String
public class Solution { public boolean isInterleave(String s1, String s2, String s3) { if(s1.length()+s2.length() !=s3.length()) { return false; }else { bool
2017-03-09 21:56:37
139
原创 516. Longest Palindromic Subsequence
public class Solution { public int longestPalindromeSubseq(String s) { int[][] num = new int[s.length()][s.length()]; for(int i = s.length()-1; i>=0;i--) { num[i][i] = 1
2017-03-09 20:48:07
183
原创 463. Island Perimeter
public static int islandPerimeter(int[][] grid) { int count = 0; if ( grid == null || grid[0].length == 0 ) { return count; } for ( int i = 0; i < grid.lengt
2016-12-26 22:38:28
196
原创 202. Happy Number
public static boolean isHappy(int n) { if(n <= 0) { return false; } Set<Integer> set = new HashSet<>(); boolean res = true; int x = n; while(
2016-12-25 10:27:54
252
原创 136. Single Number
public static int singleNumber(int[] nums) { Arrays.sort(nums); int count = 0; int i = 0; while(i < nums.length){ if(i == 0 || nums[i] == nums[i-1]) {
2016-12-19 17:14:23
192
原创 36. Valid Sudoku
public static boolean isValidSudoku(char[][] board) { if(board == null ) { return true; } int len = board.length; for(int i = 0; i < len; i++) {
2016-12-15 20:25:59
181
原创 79. Word Search
public static boolean exist(char[][] board, String word) { if(board == null || board.length == 0) { return false; } boolean res = false; for(int i = 0; i < b
2016-12-15 15:11:34
194
原创 73. Set Matrix Zeroes
public static void setZeroes(int[][] matrix) { if(matrix == null || matrix.length == 0 ) { return ; } int[] path = new int[matrix.length*matrix[0].length];
2016-12-15 11:11:51
171
原创 75. Sort Colors
public static void sortColors(int[] nums) { int count0 = 0,count1 = 0; for(int i = 0; i < nums.length; i++) { if(nums[i] == 0) { count0++; }else if(nu
2016-12-15 10:40:22
165
原创 90. Subsets II
public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> list = new ArrayList<>(); list.add(new ArrayList<>()); for(int i = 1; i <= nums.length; i++) {
2016-12-14 16:34:05
146
原创 78. Subsets
public static List<List<Integer>> subsets(int[] nums) { List<List<Integer>> list = new ArrayList<>(); for(int i = 1; i <= nums.length; i++) { List<List<Integer>> tlist = rec
2016-12-14 15:25:38
183
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人