From 2f9f0bfc9dc7d931d702915bed4ccb5ced3ff3c7 Mon Sep 17 00:00:00 2001 From: cbowu Date: Sat, 19 Aug 2017 20:49:39 +0800 Subject: [PATCH] 139,200,257,297,374 --- src/Main.java | 11 ++++++--- src/Solution139.java | 26 ++++++++++++++++++++ src/Solution200.java | 29 ++++++++++++++++++++++ src/Solution257.java | 24 +++++++++++++++++++ src/Solution297.java | 57 ++++++++++++++++++++++++++++++++++++++++++++ src/Solution374.java | 23 ++++++++++++++++++ 6 files changed, 167 insertions(+), 3 deletions(-) create mode 100644 src/Solution139.java create mode 100644 src/Solution200.java create mode 100644 src/Solution257.java create mode 100644 src/Solution297.java create mode 100644 src/Solution374.java diff --git a/src/Main.java b/src/Main.java index 0579cf9..1a5eac6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -8,8 +8,13 @@ public class Main { public static void main(String[] args) { - Solution347 solution347 = new Solution347(); - String s = "(a)())()"; - List list = solution347.removeInvalidParentheses(s); + Solution139 solution139 = new Solution139(); + String s ="leetcode"; + List list = new ArrayList<>(); + list.add("leet"); + list.add("code"); + if(solution139.wordBreak(s,list)){ + System.out.println(1); + } } } diff --git a/src/Solution139.java b/src/Solution139.java new file mode 100644 index 0000000..55c7370 --- /dev/null +++ b/src/Solution139.java @@ -0,0 +1,26 @@ +import java.util.List; + +/** + * Created by cuibowu on 2017/8/19. + */ +public class Solution139 { + public boolean wordBreak(String s, List wordDict) { + boolean[] wordBreaker = new boolean[s.length() + 1]; + wordBreaker[0] = true; + for (int i = 1; i <= s.length(); i++) { + for (String ss: + wordDict) { + if(ss.length()<=i){ + if(wordBreaker[i-ss.length()]==true){ + if(s.substring(i-ss.length(),i).equals(ss)){ + wordBreaker[i]=true; + break; + } + } + } + } + } + return wordBreaker[s.length()]; + } + +} diff --git a/src/Solution200.java b/src/Solution200.java new file mode 100644 index 0000000..0eaf1ad --- /dev/null +++ b/src/Solution200.java @@ -0,0 +1,29 @@ + +/** + * Created by cuibowu on 2017/8/19. + */ +public class Solution200 { + public int numIslands(char[][] grid) { + int count =0; + if(grid.length==0) return 0; + for(int i = 0;i=grid.length||j>=grid[0].length||grid[i][j]!='1') + return; + grid[i][j]='0'; + markIsland(i-1,j,grid); + markIsland(i+1,j,grid); + markIsland(i,j-1,grid); + markIsland(i,j+1,grid); + + } +} diff --git a/src/Solution257.java b/src/Solution257.java new file mode 100644 index 0000000..909a90d --- /dev/null +++ b/src/Solution257.java @@ -0,0 +1,24 @@ +import java.util.LinkedList; +import java.util.List; + +/** + * Created by cuibowu on 2017/8/19. + */ +public class Solution257 { + public List binaryTreePaths(TreeNode root) { + List result = new LinkedList<>(); + if(root!=null) + searchPath(root,result,""); + return result; + } + + private void searchPath(TreeNode root, List result, String currentPath){ + currentPath += root.val; + if(root.left==null&&root.right==null) + result.add(currentPath); + if(root.left!=null) + searchPath(root.left,result,currentPath+"->"); + if(root.right!=null) + searchPath(root.right,result,currentPath+"->"); + } +} diff --git a/src/Solution297.java b/src/Solution297.java new file mode 100644 index 0000000..dbe6fbb --- /dev/null +++ b/src/Solution297.java @@ -0,0 +1,57 @@ +/** + * Created by cuibowu on 2017/8/19. + */ + +import java.util.StringTokenizer; + +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution297 { + + // Encodes a tree to a single string. + public String serialize(TreeNode root) { + StringBuilder serialized = new StringBuilder(); + serializer(root,serialized); + return serialized.toString(); + + } + + // Decodes your encoded data to tree. + public TreeNode deserialize(String data) { + StringTokenizer st = new StringTokenizer(data); + return deserializer(st); + + } + + private void serializer(TreeNode root, StringBuilder serialized){ + if(root!=null){ + serialized.append(root.val); + serialized.append(" "); + serializer(root.left,serialized); + serializer(root.right,serialized); + } + else + serialized.append("$ "); + } + + private TreeNode deserializer(StringTokenizer st){ + String val = st.nextToken(); + if(val=="$") + return null; + TreeNode root = new TreeNode(Integer.parseInt(val)); + root.left=deserializer(st); + root.right=deserializer(st); + return root; + } +} + +// Your Codec object will be instantiated and called as such: +// Codec codec = new Codec(); +// codec.deserialize(codec.serialize(root)); diff --git a/src/Solution374.java b/src/Solution374.java new file mode 100644 index 0000000..3f43e84 --- /dev/null +++ b/src/Solution374.java @@ -0,0 +1,23 @@ +/** + * Created by cuibowu on 2017/8/18. + */ +public class Solution374 { + public int guessNumber(int n) { + int low = 1, high = n; + while (low <= high) { + int mid = low + (high - low) / 2; + if (guess(mid) == 0) + return mid; + else if (guess(mid) == -1) { + high = mid - 1; + } else if (guess(mid) == 1) { + low = mid + 1; + } + } + return -1; + } + + int guess(int n){ + return 0; + } +}