Skip to content

Commit e12d2f8

Browse files
committed
151
1 parent be4b4d5 commit e12d2f8

File tree

2 files changed

+131
-7
lines changed

2 files changed

+131
-7
lines changed

src/main/java/leetcode/stack/EvaluateReversePolishNotation.java renamed to src/main/java/leetcode/stack/_150_EvaluateReversePolishNotation.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,40 @@
22

33
import java.util.Stack;
44

5-
/*Evaluate the value of an arithmetic expression in Reverse Polish Notation.
6-
Valid operators are+,-,*,/. Each operand may be an integer or another expression.
7-
Some examples:
8-
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
9-
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6*/
10-
public class EvaluateReversePolishNotation {
5+
/**
6+
* 根据逆波兰表示法,求表达式的值。
7+
*
8+
* 有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
9+
*
10+
* 说明:
11+
*
12+
* 整数除法只保留整数部分。
13+
* 给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。
14+
* 示例 1:
15+
*
16+
* 输入: ["2", "1", "+", "3", "*"]
17+
* 输出: 9
18+
* 解释: ((2 + 1) * 3) = 9
19+
* 示例 2:
20+
*
21+
* 输入: ["4", "13", "5", "/", "+"]
22+
* 输出: 6
23+
* 解释: (4 + (13 / 5)) = 6
24+
* 示例 3:
25+
*
26+
* 输入: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
27+
* 输出: 22
28+
* 解释:
29+
* ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
30+
* = ((10 * (6 / (12 * -11))) + 17) + 5
31+
* = ((10 * (6 / -132)) + 17) + 5
32+
* = ((10 * 0) + 17) + 5
33+
* = (0 + 17) + 5
34+
* = 17 + 5
35+
* = 22
36+
*/
37+
public class _150_EvaluateReversePolishNotation {
38+
1139
public int evalRPN(String[] tokens) {
1240

1341
Stack<Integer> stack=new Stack<>();
@@ -45,7 +73,7 @@ public int operation(String token,int a,int b){
4573
}
4674
public static void main(String[] args){
4775
String[] arr={"2", "1", "+", "3", "*"};
48-
EvaluateReversePolishNotation test=new EvaluateReversePolishNotation();
76+
_150_EvaluateReversePolishNotation test=new _150_EvaluateReversePolishNotation();
4977
System.out.println(test.evalRPN(arr));
5078

5179
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package leetcode.string;
2+
3+
/**
4+
*
5+
*/
6+
public class _151_ReverseWords {
7+
8+
public String reverseWords(String s) {
9+
if (s == null )
10+
return s;
11+
if(s.trim().length()==0){
12+
return s.trim();
13+
}
14+
char[] arr = s.toCharArray();
15+
int left = 0, right = s.length() - 1;
16+
// 完全翻转
17+
reverse(arr,left,right);
18+
//翻转单个单词
19+
reverseWord(arr);
20+
//空格处理
21+
return cleanSpaces(arr);
22+
}
23+
24+
private String cleanSpaces(char[] arr) {
25+
int n=arr.length;
26+
int i=0,j=0;
27+
while(j<n){
28+
while(j<n&&arr[j]==' ')
29+
j++;
30+
while(j<n&&arr[j]!=' '){
31+
arr[i++]=arr[j++];
32+
}
33+
while(j<n&&arr[j]==' ')
34+
j++;
35+
if(j<n)
36+
arr[i++]=' ';
37+
}
38+
return new String(arr).substring(0,i);
39+
}
40+
41+
private void reverseWord(char[] arr) {
42+
int n=arr.length,i=0,j=0;
43+
while(j<n){
44+
while(i<j||i<n&&arr[i]==' ')
45+
i++;
46+
while(j<i||j<n&&arr[j]!=' ')
47+
j++;
48+
reverse(arr,i,j-1);
49+
}
50+
}
51+
//翻转left到right间的字母
52+
private void reverse(char[] arr, int left, int right) {
53+
while(left<right){
54+
char temp=arr[left];
55+
arr[left]=arr[right];
56+
arr[right]=temp;
57+
left++;
58+
right--;
59+
}
60+
}
61+
62+
63+
public String reverseWords1(String s) {
64+
65+
char[] array = s.toCharArray();
66+
if (array.length == 0)
67+
return s;
68+
char[] res = new char[array.length];
69+
int len = helper(array, array.length - 1, res, 0, 0);
70+
return new String(res, 0, len);
71+
72+
}
73+
74+
private int helper(char[] array, int r, char[] res, int l, int len) {
75+
while (r >= 0 && array[r] == ' ')
76+
r--;
77+
if (r < 0)
78+
return Math.max(0, len - 1);
79+
int right = r;
80+
while (r >= 0 && array[r] != ' ')
81+
r--;
82+
len += right - r + 1;
83+
for (int left = r + 1; left <= right; left++, l++)
84+
res[l] = array[left];
85+
if (l < res.length)
86+
res[l++] = ' ';
87+
return helper(array, r, res, l, len);
88+
}
89+
90+
public static void main(String[] args) {
91+
String s=" 1";
92+
_151_ReverseWords test=new _151_ReverseWords();
93+
System.out.println(test.reverseWords(s));
94+
95+
}
96+
}

0 commit comments

Comments
 (0)