Skip to content

Commit

Permalink
add some new file
Browse files Browse the repository at this point in the history
  • Loading branch information
cbowu committed Aug 30, 2017
1 parent 3b70981 commit 214dc58
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Solution227.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.Stack;

/**
* Created by cuibowu on 2017/8/30.
*/
public class Solution227 {
public int calculate(String s) {
if(s.length()==0)
return 0;
Stack<Integer> stack = new Stack<>();
int k=0;
char operator = '+';
for(int i=0;i<s.length();i++){
if(Character.isDigit(s.charAt(i))){
k = k*10+s.charAt(i)-'0';
}
if(!Character.isDigit(s.charAt(i))&&s.charAt(i)!=' '||i==s.length()-1){
switch (operator){
case '-':
stack.push(-k);
break;
case '+':
stack.push(k);
break;
case '*':
stack.push(stack.pop()*k);
break;
case '/':
stack.push(stack.pop()/k);
break;
}
operator=s.charAt(i);
k=0;
}
}
int res=0;
while(!stack.isEmpty()){
res+=stack.pop();
}
return res;
}
}
19 changes: 19 additions & 0 deletions src/Solution230.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Created by cuibowu on 2017/8/30.
*/
public class Solution230 {
public int kthSmallest(TreeNode root, int k) {
int count = count(root.left);
if(k<=count)
return kthSmallest(root.left,k);
else if(k>count+1)
return kthSmallest(root.right,k-1-count);
return root.val;
}

private int count(TreeNode root) {
if (root == null)
return 0;
return count(root.left)+count(root.right)+1;
}
}
18 changes: 18 additions & 0 deletions src/Solution344.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Arrays;

/**
* Created by cuibowu on 2017/8/30.
*/
public class Solution344 {
public String reverseString(String s) {
char[] word = s.toCharArray();
for (int i = 0, j = s.length() - 1; i < j; ) {
char temp = word[i];
word[i] = word[j];
word[j] = temp;
i++;
j--;
}
return Arrays.toString(word);
}
}
20 changes: 20 additions & 0 deletions src/Solution53.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Created by cuibowu on 2017/8/30.
*/
public class Solution53 {
public int maxSubArray(int[] nums) {
int[] dp = new int[nums.length];
dp[0]=nums[0];
int max = nums[0];
for(int i=1;i<nums.length;i++){
if(dp[i-1]>0){
dp[i]=nums[i]+dp[i-1];
}
else
dp[i]=nums[i];
max=Math.max(max,dp[i]);
}
return max;
}

}

0 comments on commit 214dc58

Please sign in to comment.