-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cbowu
committed
Aug 19, 2017
1 parent
a8a0388
commit 2f9f0bf
Showing
6 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import java.util.List; | ||
|
||
/** | ||
* Created by cuibowu on 2017/8/19. | ||
*/ | ||
public class Solution139 { | ||
public boolean wordBreak(String s, List<String> 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()]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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;i++){ | ||
for(int j=0;j<grid[i].length;j++){ | ||
if(grid[i][j]=='1'){ | ||
markIsland(i,j,grid); | ||
count++; | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
private void markIsland(int i, int j, char[][] grid){ | ||
if(i<0||j<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); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by cuibowu on 2017/8/19. | ||
*/ | ||
public class Solution257 { | ||
public List<String> binaryTreePaths(TreeNode root) { | ||
List<String> result = new LinkedList<>(); | ||
if(root!=null) | ||
searchPath(root,result,""); | ||
return result; | ||
} | ||
|
||
private void searchPath(TreeNode root, List<String> 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+"->"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |