count andy say
package com.lyc.letcode;
public class Solution0725 {
public static String countaSay(int n){
if(n<0) return null;
int start =1;
String cur = "1";
while(start<n){
StringBuilder res = new StringBuilder();
int cnt =1;
for(int j=1;j<cur.length();j++){
if(cur.charAt(j)==cur.charAt(j-1)){
cnt++;
} else{
res.append(cnt);
res.append(cur.charAt(j-1));
cnt=1;
}
}
res.append(cnt);
res.append(cur.charAt(cur.length()-1));
cur = res.toString();
start++;
}
return cur;
}
public static void main(String[] args){
System.out.println(countaSay(1));
}
}
Evaluate Reverse Polish Notation 计算逆波兰表达式
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
栈的完美应用,从前往后遍历数组,遇到数字则压入栈中,遇到符号,则把栈顶的两个数字拿出来运算,把结果再压入栈中,直到遍历完整个数组,栈顶数字即为最终答案。
网友评论