20. Valid Parentheses
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- Every close bracket has a corresponding open bracket of the same type.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Constraints:
1 <= s.length <= 104sconsists of parentheses only'()[]{}'.
很简单的用栈实现的问题。第一次用java写,对系统api还要熟练一下。
class Solution {
public boolean isValid(String s) {
char[] stk = new char[s.length()];
int pos = 0;
for(char c : s.toCharArray()) {
if(c == '(' || c == '[' || c == '{') {
stk[pos++] = c;
}
else if (c == ')' ) {
if(pos > 0 && stk[pos-1] == '(') {
stk[--pos] = 0;
}
else {
return false;
}
}
else if (c == ']'){
if(pos > 0 && stk[pos-1] == '[') {
stk[--pos] = 0;
}
else {
return false;
}
}
else if (c == '}') {
if(pos > 0 && stk[pos-1] == '{') {
stk[--pos] = 0;
}
else {
return false;
}
}
}
if(pos <= 0) {
return true;
}
else {
return false;
}
}
}
1047. Remove All Adjacent Duplicates In String
You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.
We repeatedly make duplicate removals on s until we no longer can.
Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
Example 1:
Input: s = "abbaca"
Output: "ca"
Explanation:
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
Example 2:
Input: s = "azxxzy"
Output: "ay"
Constraints:
1 <= s.length <= 105sconsists of lowercase English letters.
用栈做。注意peek和pop的条件。
class Solution {
public String removeDuplicates(String s) {
StringBuilder stk = new StringBuilder();
int posi = 0;
int poso = -1;
while(posi < s.length()) {
if(poso == -1) {
stk.append(s.charAt(posi++));
poso++;
continue;
}
char ci = s.charAt(posi);
char co = stk.charAt(poso);
if(ci == co) {
stk.deleteCharAt(poso);
poso--;
posi++;
}
else {
stk.append(ci);
poso++;
posi++;
}
}
return stk.toString();
}
}
150. Evaluate Reverse Polish Notation
You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.
Evaluate the expression. Return an integer that represents the value of the expression.
Note that:
- The valid operators are
'+','-','*', and'/'. - Each operand may be an integer or another expression.
- The division between two integers always truncates toward zero.
- There will not be any division by zero.
- The input represents a valid arithmetic expression in a reverse polish notation.
- The answer and all the intermediate calculations can be represented in a 32-bit integer.
Example 1:
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
Constraints:
1 <= tokens.length <= 104tokens[i]is either an operator:"+","-","*", or"/", or an integer in the range[-200, 200].
还是要熟悉java的各种写法。
class Solution {
public int evalRPN(String[] tokens) {
Deque<Integer> stk = new LinkedList<Integer>();
for(String s : tokens) {
if("+".equals(s)) {
Integer i1 = stk.pollLast();
Integer i2 = stk.pollLast();
if(i1 != null && i2 != null) {
stk.addLast(i1 + i2);
}
}
else if ("-".equals(s)) {
Integer i1 = stk.pollLast();
Integer i2 = stk.pollLast();
if(i1 != null && i2 != null) {
stk.addLast(i2 - i1);
}
}
else if("*".equals(s)) {
Integer i1 = stk.pollLast();
Integer i2 = stk.pollLast();
if(i1 != null && i2 != null) {
stk.addLast(i2 * i1);
}
}
else if("/".equals(s)) {
Integer i1 = stk.pollLast();
Integer i2 = stk.pollLast();
if(i1 != null && i2 != null) {
stk.addLast(Integer.valueOf(i2 / i1));
}
}
else {
stk.addLast(Integer.valueOf(s));
}
}
return stk.pop();
}
}