Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4926b74

Browse files
authored
Added tasks 150-152.
1 parent caa684d commit 4926b74

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0101_0200.s0150_evaluate_reverse_polish_notation;
2+
3+
import java.util.Stack;
4+
5+
@SuppressWarnings("java:S1149")
6+
public class Solution {
7+
public int evalRPN(String[] tokens) {
8+
Stack<Integer> st = new Stack<>();
9+
for (int i = 0; i < tokens.length; i++) {
10+
if (!Character.isDigit(tokens[i].charAt(tokens[i].length() - 1))) {
11+
st.push(eval(st.pop(), st.pop(), tokens[i]));
12+
} else {
13+
st.push(Integer.parseInt(tokens[i]));
14+
}
15+
}
16+
return st.pop();
17+
}
18+
19+
private int eval(int second, int first, String operator) {
20+
switch (operator) {
21+
case "+":
22+
return first + second;
23+
case "-":
24+
return first - second;
25+
case "*":
26+
return first * second;
27+
default:
28+
return first / second;
29+
}
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0101_0200.s0151_reverse_words_in_a_string;
2+
3+
public class Solution {
4+
public String reverseWords(String s) {
5+
StringBuilder sb = new StringBuilder();
6+
int i = s.length() - 1;
7+
while (i >= 0) {
8+
if (s.charAt(i) == ' ') {
9+
i--;
10+
continue;
11+
}
12+
int start = s.lastIndexOf(' ', i);
13+
sb.append(' ');
14+
sb.append(s.substring(start + 1, i + 1));
15+
i = start - 1;
16+
}
17+
if (sb.length() > 0) {
18+
sb.deleteCharAt(0);
19+
}
20+
21+
return sb.toString();
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0101_0200.s0152_maximum_product_subarray;
2+
3+
public class Solution {
4+
public int maxProduct(int[] arr) {
5+
int ans = Integer.MIN_VALUE;
6+
int cprod = 1;
7+
for (int i = 0; i < arr.length; i++) {
8+
cprod = cprod * arr[i];
9+
ans = Math.max(ans, cprod);
10+
if (cprod == 0) {
11+
cprod = 1;
12+
}
13+
}
14+
cprod = 1;
15+
for (int i = arr.length - 1; i >= 0; i--) {
16+
cprod = cprod * arr[i];
17+
ans = Math.max(ans, cprod);
18+
if (cprod == 0) {
19+
cprod = 1;
20+
}
21+
}
22+
return ans;
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0101_0200.s0150_evaluate_reverse_polish_notation;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void evalRPN() {
11+
assertThat(new Solution().evalRPN(new String[] {"2", "1", "+", "3", "*"}), equalTo(9));
12+
assertThat(new Solution().evalRPN(new String[] {"4", "13", "5", "/", "+"}), equalTo(6));
13+
assertThat(
14+
new Solution()
15+
.evalRPN(
16+
new String[] {
17+
"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5",
18+
"+"
19+
}),
20+
equalTo(22));
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g0101_0200.s0151_reverse_words_in_a_string;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void reverseWords() {
11+
assertThat(new Solution().reverseWords("the sky is blue"), equalTo("blue is sky the"));
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g0101_0200.s0152_maximum_product_subarray;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void maxProduct() {
11+
assertThat(new Solution().maxProduct(new int[] {2, 3, -2, 4}), equalTo(6));
12+
}
13+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /