首页 > 其他分享 >LeetCode:224.Basic Calculator

LeetCode:224.Basic Calculator

时间:2022-12-05 18:06:24浏览次数:62  
标签:oper lc curNum Calculator else ++ num 224 LeetCode


LeetCode:224.Basic Calculator

题目描述

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 empty spaces .

Example 1:

Input: "1 + 1"
Output: 2

Example 2:

Input: " 2-1 + 2 "
Output: 3

Example 3:

Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23

Note:

You may assume that the given expression is always valid.
Do not use the eval built-in library function.

解题思路 —— 递归求解

从左向右依次计算,当出现括号时,递归地优先计算括号内的算式。

AC 代码

class Solution {
public:
int calculate(string s) {
long long num = 0;
char oper = '+';

for(size_t i = 0; i < s.size(); ++i)
{
if(s[i] >= '0' && s[i] <= '9')
{
long long curNum = 0;
while(i < s.size() && s[i] >= '0' && s[i] <= '9')
{
curNum = curNum*10 + s[i] - '0';
++i;
}
--i;

if(oper == '+') num += curNum;
else if(oper == '-') num -= curNum;
}
else if(s[i] == '(')
{
int j = i;
int lc = 1;
++i;
while(i < s.size() && lc != 0)
{
if(s[i] == '(') ++lc;
else if(s[i] == ')') --lc;
++i;
}

if(oper == '+') num += calculate(s.substr(j+1, i-j-2));
else if(oper == '-') num -= calculate(s.substr(j+1, i-j-2));
--i;
}
else if(s[i] == ' ')
{
continue;
}
else
{
oper = s[i];
}
}

return num;
}
};


标签:oper,lc,curNum,Calculator,else,++,num,224,LeetCode
From: https://blog.51cto.com/u_15903085/5913199

相关文章

  • LeetCode: 221. Maximal Square
    LeetCode:221.MaximalSquare题目描述Givena2Dbinarymatrixfilledwith​​0​​​'sand​​1​​​'s,findthelargestsquarecontainingonly​​1​​'s......
  • LeetCode: 223. Rectangle Area
    LeetCode:223.RectangleArea题目描述Findthetotalareacoveredbytworectilinearrectanglesina2Dplane.Eachrectangleisdefinedbyitsbottomleftcorne......
  • LeetCode: 225. Implement Stack using Queues
    LeetCode:225.ImplementStackusingQueues题目描述Implementthefollowingoperationsofastackusingqueues.​​push(x)​​–Pushelementxontostack.​......
  • LeetCode: 232. Implement Queue using Stacks
    LeetCode:232.ImplementQueueusingStacks题目描述Implementthefollowingoperationsofaqueueusingstacks.​​push(x)​​​–Pushelementxtothebacko......
  • LeetCode: 227. Basic Calculator II
    LeetCode:227.BasicCalculatorII题目描述Implementabasiccalculatortoevaluateasimpleexpressionstring.Theexpressionstringcontainsonlynon-negative......
  • LeetCode: 239. Sliding Window Maximum
    LeetCode:239.SlidingWindowMaximum题目描述Givenanarraynums,thereisaslidingwindowofsizekwhichismovingfromtheveryleftofthearraytotheve......
  • LeetCode: 228. Summary Ranges
    LeetCode:228.SummaryRanges题目描述Givenasortedintegerarraywithoutduplicates,returnthesummaryofitsranges.Example1:Input:[0,1,2,4,5,7]Output:[......
  • LeetCode: 234. Palindrome Linked List
    LeetCode:234.PalindromeLinkedList题目描述Givenasinglylinkedlist,determineifitisapalindrome.Example1:Input:1->2Output:falseExample2:Input:1->......
  • LeetCode: 241. Different Ways to Add Parentheses
    LeetCode:241.DifferentWaystoAddParentheses题目描述Givenastringofnumbersandoperators,returnallpossibleresultsfromcomputingallthedifferentp......
  • LeetCode: 238. Product of Array Except Self
    LeetCode:238.ProductofArrayExceptSelf题目描述Givenanarraynumsofnintegerswhere​​n>1​​​,returnanarrayoutputsuchthatoutput[i]isequal......