File tree Expand file tree Collapse file tree 3 files changed +96
-0
lines changed
0224-Basic-Calculator/cpp-0224 Expand file tree Collapse file tree 3 files changed +96
-0
lines changed Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.5 )
2
+ project (cpp_0224 )
3
+
4
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
5
+
6
+ set (SOURCE_FILES main.cpp )
7
+ add_executable (cpp_0224 ${SOURCE_FILES} )
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/basic-calculator/description/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2018-09-03
4
+
5
+ #include < iostream>
6
+ #include < vector>
7
+ #include < cassert>
8
+
9
+ using namespace std ;
10
+
11
+
12
+ // / Two Stacks
13
+ // / Shunting-Yard Algorithms: https://en.wikipedia.org/wiki/Shunting-yard_algorithm
14
+ // /
15
+ // / Time Complexity: O(n)
16
+ // / Space Complexity: O(n)
17
+ class Solution {
18
+ public:
19
+ int calculate (string s) {
20
+
21
+ vector<int > nums;
22
+ vector<char > ops;
23
+
24
+ for (int i = 0 ; i < s.size (); ){
25
+ if (s[i] == ' +' || s[i] == ' -' || s[i] == ' (' )
26
+ ops.push_back (s[i++]);
27
+ else if (s[i] == ' )' ){
28
+ assert (!ops.empty () && ops.back () == ' (' );
29
+ ops.pop_back ();
30
+ i ++;
31
+
32
+ cal (nums, ops);
33
+ }
34
+ else if (isdigit (s[i])){
35
+
36
+ int num = s[i] - ' 0' ;
37
+ int j;
38
+ for (j = i + 1 ; j < s.size () && isdigit (s[j]); j ++)
39
+ num = num * 10 + (s[j] - ' 0' );
40
+ i = j;
41
+
42
+ nums.push_back (num);
43
+ cal (nums, ops);
44
+ }
45
+ else
46
+ i ++;
47
+
48
+ // Solution::print_vec(nums);
49
+ // Solution::print_vec(ops);
50
+ // cout << endl;
51
+ }
52
+
53
+ assert (nums.size () == 1 );
54
+ return nums.back ();
55
+ }
56
+
57
+ private:
58
+ void cal (vector<int >& nums, vector<char >& ops){
59
+
60
+ if (!ops.empty () && (ops.back () == ' +' || ops.back () == ' -' )){
61
+ int second = nums.back ();
62
+ nums.pop_back ();
63
+ assert (!nums.empty ());
64
+ int first = nums.back ();
65
+ nums.pop_back ();
66
+ nums.push_back (ops.back () == ' +' ? (first + second) : (first - second));
67
+ ops.pop_back ();
68
+ }
69
+ }
70
+
71
+ template <typename T>
72
+ static void print_vec (const vector<T>& vec){
73
+ for (int i = 0 ; i < vec.size (); i ++)
74
+ cout << vec[i] << " " ;
75
+ cout << endl;
76
+ }
77
+ };
78
+
79
+
80
+ int main () {
81
+
82
+ cout << Solution ().calculate (" 1 + 1" ) << endl; // 2
83
+ cout << Solution ().calculate (" 2-1 + 2 " ) << endl; // 3
84
+ cout << Solution ().calculate (" (1+(4+5+2)-3)+(6+8)" ) << endl; // 23
85
+
86
+ return 0 ;
87
+ }
Original file line number Diff line number Diff line change @@ -205,6 +205,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
205
205
| | | | | | |
206
206
| 222 | [ Count Complete Tree Nodes] ( https://leetcode.com/problems/count-complete-tree-nodes/description/ ) | [ 无] | [ C++] ( 0222-Count-Complete-Tree-Nodes/cpp-0222/ ) | | |
207
207
| | | | | | |
208
+ | 224 | [ Basic Calculator] ( https://leetcode.com/problems/basic-calculator/description/ ) | [ 无] | [ C++] ( 0224-Basic-Calculator/cpp-0224/ ) | | |
209
+ | | | | | | |
208
210
| 226 | [ Invert Binary Tree] ( https://leetcode.com/problems/invert-binary-tree/description/ ) | [ solution] ( https://leetcode.com/problems/invert-binary-tree/solution/ ) | [ C++] ( 0226-Invert-Binary-Tree/cpp-0226/ ) | [ Java] ( 0226-Invert-Binary-Tree/java-0226/src/ ) | |
209
211
| | | | | | |
210
212
| 230 | [ Kth Smallest Element in a BST] ( https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ ) | [ 无] | [ C++] ( 0230-Kth-Smallest-Element-in-a-BST/cpp-0230/ ) | | |
You can’t perform that action at this time.
0 commit comments