Basic Calculator Base Calculator-leetcode

Source: Internet
Author: User

1. Title:

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-negati ve integers and empty spaces .

Assume that the given expression was always valid.

Some Examples:

"1 + 1" = 2 "2-1 + 2" = 3 "(1+ (4+5+2)-3) + (6+8)" = 23

The title requirement is to implement a basic calculator that takes a string as input, implementing addition subtraction and parentheses. There are 0-9,+,-, (,) and white space symbols in the string.

You can assume that all calculations are non-negative. It can be considered that the input of the topic is a valid formula. There may be spaces in the string that you need to dispose of yourself.

2. Ideas:

Recursively calculates the value of each sub-sum minus.

What is a sub-formula? Think the whole equation is the first subtype. The first expression enclosed in parentheses is the second subtype. In turn.

For example

(1+ (4+5+2)-3) + (6+8)

, the first subtype is (1+ (4+5+2)-3) + (6+8). The second subtype is (1+ (4+5+2)-3). The third one is (4+5+2). The fourth one is (6+8).

So

A value that requires the first subtype is equal to the second subtype + fourth subtype.

A value of the second subtype is required, which is equal to the 1+ of the third subtype-3.

    • So, first there is a function 1, which takes a pointer to the beginning of a certain subtype, and returns the value of that subtype.
    • Second, a function 2 is used to convert a string of 1, 23 Ah, 84 AH and other strings into the corresponding numeric 1,23,84. If the character to be converted is ' (', it means that the content to be converted is a subtype, then it is treated with function 1.)
3. Code

Give a Java implementation:

public class solution{public int calculate (String s) {//This is the function 1 int ans=0;        int sign=1;            while (Location<s.length ()) {ans+= (helper (s) *sign);            if (Location>=s.length ()) break;                if (S.charat (location) = = ') ') {location++;            return ans;            } sign= (S.charat (location) = = '-')? -1:1;        Location+=1;    } return ans;        } private int location=0;//This is the pointer to the sub-position of the private int helper (String s) {//This is the function 2 int op=0; while (Location<s.length () && (Character.isdigit (s.charat) | |                 Character.isspacechar (S.charat (location))) {if (Character.isdigit (S.charat)) {op*=10;            op+= (S.charat (location)-' 0 ');        } location++;            } if (Location<s.length () &&s.charat (location) = = ' (') {location++;        return calculate (s);    } return op; }}

Basic Calculator Base Calculator-leetcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.