File tree Expand file tree Collapse file tree 2 files changed +117
-0
lines changed Expand file tree Collapse file tree 2 files changed +117
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < algorithm>
2
+ #include < string>
3
+ #include < vector>
4
+ #include < iostream>
5
+ using namespace std ;
6
+ // 特点: 没有括号, 使用栈来做
7
+ class Solution {
8
+ public:
9
+ int calculate (string s) {
10
+ string temp;
11
+ for (auto ch : s)
12
+ {
13
+ if (ch != ' ' )
14
+ temp.push_back (ch);
15
+ }
16
+ s = " +" + temp;
17
+
18
+ int len = s.size ();
19
+ vector<int > nums;
20
+
21
+ for (int i=0 ; i < len; i++)
22
+ {
23
+ if (s[i] == ' +' || s[i] == ' -' )
24
+ {
25
+ int j = i+1 ; // 找出"+200"这种结构中的数字
26
+ while (j < len && isdigit (s[j])) j++; // 循环结束时, j多跑了1位
27
+ int endPos = j-1 ;
28
+ int num = stoi (s.substr (i+1 , endPos-(i+1 )+1 ));
29
+ if (s[i] == ' +' ) nums.push_back (num);
30
+ else nums.push_back (-num);
31
+ i = endPos;
32
+ }
33
+ else if (s[i] == ' *' || s[i] == ' /' )
34
+ {
35
+ int j = i+1 ; // 找出"*100"这种结构中的数字
36
+ while (j < len && isdigit (s[j])) j++;
37
+ int endPos = j-1 ;
38
+ int num = stoi (s.substr (i+1 , endPos-(i+1 )+1 ));
39
+ if (s[i] == ' *' )
40
+ nums.back () = nums.back () * num;
41
+ else nums.back () = nums.back () / num;
42
+ i = endPos;
43
+ }
44
+ }
45
+ int sum = 0 ;
46
+ for (int i=0 ; i < nums.size (); i++) sum += nums[i];
47
+ return sum;
48
+ }
49
+ };
50
+
51
+ // Test
52
+ int main ()
53
+ {
54
+ Solution sol;
55
+ string s = " 3+5 / 2 " ;
56
+ auto res = sol.calculate (s);
57
+ cout << res << endl;
58
+
59
+ return 0 ;
60
+ }
Original file line number Diff line number Diff line change
1
+ #include < algorithm>
2
+ #include < string>
3
+ #include < vector>
4
+ #include < iostream>
5
+ #include < numeric> /* 含有 accumulate 函数 */
6
+ using namespace std ;
7
+
8
+ // 特点: 没有括号, 使用栈来做
9
+ class Solution {
10
+ public:
11
+ int calculate (string s) {
12
+ auto spaceEndIts = remove (s.begin (), s.end (), ' ' );
13
+ s.erase (spaceEndIts, s.end ());
14
+ s = " +" + s;
15
+
16
+ int len = s.size ();
17
+ vector<int > nums;
18
+
19
+ for (int i=0 ; i < len; i++)
20
+ {
21
+ if (s[i] == ' +' || s[i] == ' -' )
22
+ {
23
+ int j = i+1 ; // 找出"+200"这种结构中的数字
24
+ while (j < len && isdigit (s[j])) j++; // 循环结束时, j多跑了1位
25
+ int endPos = j-1 ;
26
+ int num = stoi (s.substr (i+1 , endPos-(i+1 )+1 ));
27
+ if (s[i] == ' +' ) nums.push_back (num);
28
+ else nums.push_back (-num);
29
+ i = endPos;
30
+ }
31
+ else if (s[i] == ' *' || s[i] == ' /' )
32
+ {
33
+ int j = i+1 ; // 找出"*100"这种结构中的数字
34
+ while (j < len && isdigit (s[j])) j++;
35
+ int endPos = j-1 ;
36
+ int num = stoi (s.substr (i+1 , endPos-(i+1 )+1 ));
37
+ if (s[i] == ' *' )
38
+ nums.back () = nums.back () * num;
39
+ else nums.back () = nums.back () / num;
40
+ i = endPos;
41
+ }
42
+ }
43
+ int sum = accumulate (nums.begin (), nums.end (), 0 );
44
+ return sum;
45
+ }
46
+ };
47
+
48
+ // Test
49
+ int main ()
50
+ {
51
+ Solution sol;
52
+ string s = " 3+5 / 2 " ;
53
+ auto res = sol.calculate (s);
54
+ cout << res << endl;
55
+
56
+ return 0 ;
57
+ }
You can’t perform that action at this time.
0 commit comments