-
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 30, 2017
1 parent
3b70981
commit 214dc58
Showing
4 changed files
with
99 additions
and
0 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
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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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; | ||
} | ||
|
||
} |