class Count{ public int Count(String a){ int n = a.length(); int result = 0; int num = 0; int p = 0; int L1 = a.indexOf(‘(‘); int R1 = a.indexOf(‘)‘); String b = a.substring(L1+1,R1-1); int op = 0; if(L1 >= 0 ) return Count(b) + result; else { for(int i=0 ;i < n ;i++) { if(‘0‘<(a.charAt(i))&& (a.charAt(i))<‘9‘) num = num*10+num; switch(op) { case 0 : break; case 1 : num = num + p ;break; case 2 : num = p - num ;break; default : break; } if(a.charAt(i)==‘+‘) op = 1; else if (a.charAt(i)==‘-‘) op = 2 ; else op = 0; p = num ; } result = num; return result; } } }这个鸟玩意用了迭代,其实不太好浪费空间,数据多了还容易死,有人用stack还没研究这个是600mm的还没看明白https://leetcode.com/discuss/39454/accepted-infix-postfix-based-solution-explaination-600ms
时间: 2024-10-02 18:35:03