镜像站点
· 本页由第三方 GitHub 只读镜像提供,
非 GitHub 官方站点
,不接受任何登录或凭据输入。
前往 github.com
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
LinXin04
LeetCode-Java
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
LeetCode-Java
/
BasicCaculator.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
72 lines (67 loc) · 1.78 KB
master
Breadcrumbs
LeetCode-Java
/
BasicCaculator.java
Copy path
Top
File metadata and controls
Code
Blame
72 lines (67 loc) · 1.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* 题目:基础计算:给定一个字符串,里面可能包含空格、括号()、+、-、非负整数,求其计算的数值。
* 例如:
* "1 + 1" = 2
* " 2-1 + 2 " = 3
* "(1+(4+5+2)-3)+(6+8)" = 23
* 解题思路:
* 首先,对于读入的字符,如果是数字的话,我们就要利用一个循环将整个(多位)数字读入,计算该数字与前面数字的运算结果;
* 如果读到“+”或者读到“-”,更新符号;
* 如果读到“(”,将之前的结果,以及符号压栈,保留下来;
* 如果读到“)”,将现在的结果与栈里面的结果进行相加
*/
import java.util.Scanner;
import java.util.Stack;
public class BasicCaculator {
public static void main(String[] args) {
System.out.println("请输入一个字符串:");
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
Solution107 sl=new Solution107();
int result=sl.caculate(s);
System.out.println("结果是:"+result);
}
}
class Solution107
{
public int caculate(String s)
{
if(s==null) return 0;
int result=0;
int sign=1;
Stack<Integer> stack=new Stack<Integer>();
int Length=s.length();
for(int i=0;i<Length;i++)
{
//读入的字符
char curChar=s.charAt(i);
//如果是数字
if(Character.isDigit(curChar))
{
int digit=curChar-'0';
//读入整个数字
while(i+1<Length && Character.isDigit(s.charAt(i+1)))
digit=digit*10+s.charAt(++i)-'0';
//将前面的符号加入
result+=digit*sign;
}else if(curChar=='+')
{
sign=1;
}else if(curChar=='-')
{
sign=-1;
}else if(curChar=='(')
{
stack.push(result);
result=0;
stack.push(sign);
sign=1;
}else if(curChar==')')
{
result=stack.pop()*result+stack.pop();
sign=1;
}
}
return result;
}
}
You can’t perform that action at this time.