摘要:題目鏈接,就是感覺條件有點多簡單點的寫法,把直接用存在里面,就存成,存成題目鏈接這題就是碰到在加減后面怎么處理的問題。用一個來表示之前的,所以碰到現(xiàn)在是乘的時候就,除類似。
224. Basic Calculator
題目鏈接:https://leetcode.com/problems...
stack,就是感覺條件有點多
public class Solution { public int calculate(String s) { Stacknums = new Stack(); Stack signs = new Stack(); int prev = 0, cur = 0; char sign = "#"; int i = 0; /* prev: number before sign, cur: number after sign, sign * push: meet "(", put prev, sign = "#" * pop: meet ")" if not empty * number: do caculate if sign != "#" */ // start of number or sign or "(" or ")" while(i < s.length()) { char c = s.charAt(i); // number if(Character.isDigit(c)) { while(i < s.length() && Character.isDigit(s.charAt(i))) cur = cur * 10 + (s.charAt(i++) - "0"); i--; if(sign == "+") prev += cur; else if(sign == "-") prev -= cur; else prev = cur; cur = 0; } else if(c == "(") { // in case of "(1 + 2)" if(sign != "#") { nums.push(prev); signs.push(sign); sign = "#"; prev = 0; } } else if(c == ")") { if(!nums.isEmpty()) { int temp = nums.pop(); sign = signs.pop(); if(sign == "+") prev += temp; else prev = temp - prev; sign = "#"; } } else if(c == "+" || c == "-") sign = c; // update i++; } return prev; } }
簡單點的寫法,把sign直接用int存在stack里面,"+"就存成1, "-"存成-1
public class Solution { public int calculate(String s) { Stack227. Basic Calculator IInums = new Stack(); Stack signs = new Stack(); int prev = 0, cur = 0; int sign = 1; int i = 0; /* prev: number before sign, cur: number after sign, sign * push: meet "(", put prev * pop: meet ")" if not empty * number: do caculate */ // start of number or sign or "(" or ")" while(i < s.length()) { char c = s.charAt(i); // number if(Character.isDigit(c)) { while(i < s.length() && Character.isDigit(s.charAt(i))) { cur = cur * 10 + (s.charAt(i++) - "0"); } i--; prev = prev + sign*cur; cur = 0; } else if(c == "(") { nums.push(prev); signs.push(sign); sign = 1; prev = 0; } else if(c == ")") { prev = nums.pop() + signs.pop() * prev; } else if(c == "+") sign = 1; else if(c == "-") sign = -1; // update i++; } return prev; } }
題目鏈接:https://leetcode.com/problems...
這題就是碰到"*", "/"在加減后面怎么處理的問題。用一個prev來表示之前的number,所以碰到現(xiàn)在是乘的時候就:res = res - prev + prev * cur, prev = prev * cur,除類似。
public class Solution { public int calculate(String s) { int res = 0; int prev = 0; s = s.replaceAll("[^0-9^+-/*]", ""); int i = 0, n = s.length(); // first number while(i < n && Character.isDigit(s.charAt(i))) { prev = prev * 10 + (s.charAt(i++) - "0"); } res = prev; // loop invariant: start from sign: -, +, *, / while(i < n) { char c = s.charAt(i++); int cur = 0; while(i < n && Character.isDigit(s.charAt(i))) { cur = cur * 10 + (s.charAt(i++) - "0"); } if(c == "+") { res += cur; prev = cur; } else if(c == "-") { res -= cur; prev = -cur; } else if(c == "*") { res = res - prev + prev * cur; prev *= cur; } else if(c == "/") { res = res - prev + prev / cur; prev /= cur; } } return res; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/66680.html
摘要:復(fù)雜度思路用兩個來分別記錄當(dāng)前的結(jié)果和操作符注意每一次統(tǒng)計當(dāng)前的的時候,要看一下下一位的操作符。有一種的方法,是表示的是匹配任意的空白符,包括空格,制表符,換行符,中文全角空格等。也可以用更簡單的方法,。 LeetCode[227] Basic Calculator II Implement a basic calculator to evaluate a simple expres...
Problem Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division sho...
摘要:復(fù)雜度思路將字符串先轉(zhuǎn)換成后綴表達式,再將其出來。 Leetcode[224] Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ),...
摘要:但是乘除就會有問題,要特殊處理。這題只有加減和括號,優(yōu)先級就是括號里的先計算,所有我們把括號里的內(nèi)容當(dāng)做操作的基本單位。遇到遇到和,遇到遇到,彈出再遇到彈出,這里只是把對數(shù)字的操作變成了對的操作,去括號的邏輯一樣。 The expression string contains only non-negative integers, +, -, *, / operators and em...
Problem Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and e...
閱讀 2360·2023-04-25 19:27
閱讀 3491·2021-11-24 09:39
閱讀 3906·2021-10-08 10:17
閱讀 3397·2019-08-30 13:48
閱讀 1930·2019-08-29 12:26
閱讀 3121·2019-08-28 17:52
閱讀 3537·2019-08-26 14:01
閱讀 3534·2019-08-26 12:19