Skip to content

Commit

Permalink
139,200,257,297,374
Browse files Browse the repository at this point in the history
  • Loading branch information
cbowu committed Aug 19, 2017
1 parent a8a0388 commit 2f9f0bf
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@

public class Main {
public static void main(String[] args) {
Solution347 solution347 = new Solution347();
String s = "(a)())()";
List<String> list = solution347.removeInvalidParentheses(s);
Solution139 solution139 = new Solution139();
String s ="leetcode";
List<String> list = new ArrayList<>();
list.add("leet");
list.add("code");
if(solution139.wordBreak(s,list)){
System.out.println(1);
}
}
}
26 changes: 26 additions & 0 deletions src/Solution139.java
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()];
}

}
29 changes: 29 additions & 0 deletions src/Solution200.java
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);

}
}
24 changes: 24 additions & 0 deletions src/Solution257.java
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+"->");
}
}
57 changes: 57 additions & 0 deletions src/Solution297.java
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));
23 changes: 23 additions & 0 deletions src/Solution374.java
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;
}
}

0 comments on commit 2f9f0bf

Please sign in to comment.