|
3 | 3 | #include <limits.h>
|
4 | 4 | #include <stack>
|
5 | 5 | #include <string>
|
| 6 | +#include <vector> |
| 7 | + |
| 8 | +using namespace std; |
6 | 9 |
|
7 | 10 | class StringSolution {
|
8 | 11 | public:
|
9 |
| - |
10 |
| - bool isValid(std::string const &s) { |
11 |
| - std::stack<char> st; |
12 |
| - for (auto c : s) { |
13 |
| - if (c == '[' || c == '(' || c == '{') { |
14 |
| - st.push(c); |
15 |
| - } |
16 |
| - if (c == ']' || c == ')' || c == '}') { |
17 |
| - if (st.empty()) { |
18 |
| - return false; |
| 12 | + bool isValid(std::string const &s) { |
| 13 | + std::stack<char> st; |
| 14 | + for (auto c : s) { |
| 15 | + if (c == '[' || c == '(' || c == '{') { |
| 16 | + st.push(c); |
| 17 | + } |
| 18 | + if (c == ']' || c == ')' || c == '}') { |
| 19 | + if (st.empty()) { |
| 20 | + return false; |
| 21 | + } |
| 22 | + char tmp = st.top(); |
| 23 | + if ((tmp == '[' && c == ']') || (tmp == '(' && c == ')') || |
| 24 | + (tmp == '{' && c == '}')) { |
| 25 | + st.pop(); |
| 26 | + } else { |
| 27 | + return false; |
| 28 | + } |
| 29 | + } |
19 | 30 | }
|
20 |
| - char tmp = st.top(); |
21 |
| - if ((tmp == '[' && c == ']') || (tmp == '(' && c == ')') || |
22 |
| - (tmp == '{' && c == '}')) { |
23 |
| - st.pop(); |
24 |
| - } else { |
25 |
| - return false; |
| 31 | + if (!st.empty()) { |
| 32 | + return false; |
26 | 33 | }
|
27 |
| - } |
28 |
| - } |
29 |
| - if (!st.empty()) { |
30 |
| - return false; |
| 34 | + return true; |
31 | 35 | }
|
32 |
| - return true; |
33 |
| - } |
34 |
| - std::string addBinary(std::string a, std::string b) { |
35 |
| - std::string result; |
36 |
| - int carry = 0; |
37 |
| - reverse(a.begin(), a.end()); |
38 |
| - reverse(b.begin(), b.end()); |
39 |
| - unsigned int alen = a.size(); |
40 |
| - unsigned int blen = b.size(); |
41 |
| - std::string short_str = b; |
42 |
| - std::string long_str = a; |
43 |
| - if (alen < blen) { |
44 |
| - short_str = a; |
45 |
| - long_str = b; |
46 |
| - } |
47 |
| - for (size_t i = 0; i < short_str.size(); i++) { |
48 |
| - unsigned int acc, val; |
49 | 36 |
|
50 |
| - acc = short_str[i] + long_str[i] - 2 * '0' + carry; |
51 |
| - carry = acc / 2; |
52 |
| - val = acc % 2; |
53 |
| - result.insert(result.begin(), val + '0'); |
54 |
| - } |
55 |
| - size_t idx = short_str.size(); |
56 |
| - while (idx < long_str.size()) { |
57 |
| - unsigned int acc, val; |
58 |
| - acc = long_str[idx] - '0' + carry; |
59 |
| - carry = acc / 2; |
60 |
| - val = acc % 2; |
61 |
| - idx++; |
62 |
| - result.insert(result.begin(), val + '0'); |
63 |
| - } |
64 |
| - if (carry) { |
65 |
| - result.insert(result.begin(), '1'); |
66 |
| - } |
67 |
| - return result; |
68 |
| - } |
69 |
| - int myAtoi(const std::string &str) { |
70 |
| - auto start = str.begin(); |
71 |
| - int result = 0; |
72 |
| - int sign = 1; |
73 |
| - while (*start == ' ' && start != str.end()) { |
74 |
| - start++; |
| 37 | + std::string addBinary(std::string a, std::string b) { |
| 38 | + std::string result; |
| 39 | + int carry = 0; |
| 40 | + reverse(a.begin(), a.end()); |
| 41 | + reverse(b.begin(), b.end()); |
| 42 | + unsigned int alen = a.size(); |
| 43 | + unsigned int blen = b.size(); |
| 44 | + std::string short_str = b; |
| 45 | + std::string long_str = a; |
| 46 | + if (alen < blen) { |
| 47 | + short_str = a; |
| 48 | + long_str = b; |
| 49 | + } |
| 50 | + for (size_t i = 0; i < short_str.size(); i++) { |
| 51 | + unsigned int acc, val; |
| 52 | + |
| 53 | + acc = short_str[i] + long_str[i] - 2 * '0' + carry; |
| 54 | + carry = acc / 2; |
| 55 | + val = acc % 2; |
| 56 | + result.insert(result.begin(), val + '0'); |
| 57 | + } |
| 58 | + size_t idx = short_str.size(); |
| 59 | + while (idx < long_str.size()) { |
| 60 | + unsigned int acc, val; |
| 61 | + acc = long_str[idx] - '0' + carry; |
| 62 | + carry = acc / 2; |
| 63 | + val = acc % 2; |
| 64 | + idx++; |
| 65 | + result.insert(result.begin(), val + '0'); |
| 66 | + } |
| 67 | + if (carry) { |
| 68 | + result.insert(result.begin(), '1'); |
| 69 | + } |
| 70 | + return result; |
75 | 71 | }
|
76 |
| - if (*start == '+') { |
77 |
| - sign = 1; |
78 |
| - start++; |
79 |
| - } else if (*start == '-') { |
80 |
| - sign = -1; |
81 |
| - start++; |
| 72 | + |
| 73 | + int myAtoi(const std::string &str) { |
| 74 | + auto start = str.begin(); |
| 75 | + int result = 0; |
| 76 | + int sign = 1; |
| 77 | + while (*start == ' ' && start != str.end()) { |
| 78 | + start++; |
| 79 | + } |
| 80 | + if (*start == '+') { |
| 81 | + sign = 1; |
| 82 | + start++; |
| 83 | + } else if (*start == '-') { |
| 84 | + sign = -1; |
| 85 | + start++; |
| 86 | + } |
| 87 | + while (start != str.end()) { |
| 88 | + if (*start > '9' || *start < '0') { |
| 89 | + break; |
| 90 | + } |
| 91 | + if (result > INT_MAX / 10 || |
| 92 | + (result == INT_MAX && (*start - '0') > INT_MAX % 10)) { |
| 93 | + return (sign == -1) ? INT_MIN : INT_MAX; |
| 94 | + } |
| 95 | + result *= 10; |
| 96 | + result += *start - '0'; |
| 97 | + start++; |
| 98 | + } |
| 99 | + return result * sign; |
82 | 100 | }
|
83 |
| - while (start != str.end()) { |
84 |
| - if (*start > '9' || *start < '0') { |
85 |
| - break; |
86 |
| - } |
87 |
| - if (result > INT_MAX / 10 || |
88 |
| - (result == INT_MAX && (*start - '0') > INT_MAX % 10)) { |
89 |
| - return (sign == -1) ? INT_MIN : INT_MAX; |
90 |
| - } |
91 |
| - result *= 10; |
92 |
| - result += *start - '0'; |
93 |
| - start++; |
| 101 | + |
| 102 | + bool isPalindrome(std::string s) { |
| 103 | + transform(s.begin(), s.end(), s.begin(), ::tolower); |
| 104 | + auto left = s.begin(), right = prev(s.end()); |
| 105 | + while (left < right) { |
| 106 | + if (!::isalnum(*left)) |
| 107 | + left++; |
| 108 | + else if (!::isalnum(*right)) |
| 109 | + right--; |
| 110 | + else if (*left != *right) |
| 111 | + return false; |
| 112 | + else { |
| 113 | + left++; |
| 114 | + right--; |
| 115 | + } |
| 116 | + } |
| 117 | + return true; |
94 | 118 | }
|
95 |
| - return result * sign; |
96 |
| - } |
97 |
| - bool isPalindrome(std::string s) { |
98 |
| - transform(s.begin(), s.end(), s.begin(), ::tolower); |
99 |
| - auto left = s.begin(), right = prev(s.end()); |
100 |
| - while (left < right) { |
101 |
| - if (!::isalnum(*left)) |
102 |
| - left++; |
103 |
| - else if (!::isalnum(*right)) |
104 |
| - right--; |
105 |
| - else if (*left != *right) |
106 |
| - return false; |
107 |
| - else { |
108 |
| - left++; |
109 |
| - right--; |
110 |
| - } |
| 119 | + |
| 120 | + bool isMatch(const std::string &s, const std::string &p) { |
| 121 | + return isMatch(s.c_str(), p.c_str()); |
111 | 122 | }
|
112 |
| - return true; |
113 |
| - } |
114 |
| - bool isMatch(const std::string &s, const std::string &p) { |
115 |
| - return isMatch(s.c_str(), p.c_str()); |
116 |
| - } |
117 | 123 |
|
118 | 124 | private:
|
119 |
| - bool isMatch(const char *s, const char *p) { |
120 |
| - if (*p == '\0') |
121 |
| - return *s == '\0'; |
122 |
| - // next char is not '*', then must match current character |
123 |
| - if (*(p + 1) != '*') { |
124 |
| - if (*p == *s || (*p == '.' && *s != '\0')) |
125 |
| - return isMatch(s + 1, p + 1); |
126 |
| - else |
127 |
| - return false; |
128 |
| - } else { // next char is '*' |
129 |
| - while (*p == *s || (*p == '.' && *s != '\0')) { |
130 |
| - if (isMatch(s, p + 2)) |
131 |
| - return true; |
132 |
| - s++; |
133 |
| - } |
134 |
| - return isMatch(s, p + 2); |
| 125 | + bool isMatch(const char *s, const char *p) { |
| 126 | + if (*p == '\0') |
| 127 | + return *s == '\0'; |
| 128 | + // next char is not '*', then must match current character |
| 129 | + if (*(p + 1) != '*') { |
| 130 | + if (*p == *s || (*p == '.' && *s != '\0')) |
| 131 | + return isMatch(s + 1, p + 1); |
| 132 | + else |
| 133 | + return false; |
| 134 | + } else { // next char is '*' |
| 135 | + while (*p == *s || (*p == '.' && *s != '\0')) { |
| 136 | + if (isMatch(s, p + 2)) |
| 137 | + return true; |
| 138 | + s++; |
| 139 | + } |
| 140 | + return isMatch(s, p + 2); |
| 141 | + } |
135 | 142 | }
|
136 |
| - } |
137 | 143 | };
|
138 | 144 |
|
139 | 145 | int main(int argc, char const *argv[]) {
|
140 |
| - StringSolution sol; |
141 |
| - std::string s = "AASS,DD.****SSAA"; |
142 |
| - std::string p = "ac*a"; |
143 |
| - std::cout << sol.isMatch(s, p) << std::endl; |
144 |
| - std::cout << "is Palindrome" << std::endl; |
145 |
| - std::cout << sol.isPalindrome(".,") << std::endl; |
146 |
| - std::cout << "atoi\t" << sol.myAtoi("125") << std::endl; |
147 |
| - std::cout << " add Binary\t" << sol.addBinary("1010", "1011") << std::endl; |
148 |
| - printf("const std::string &)))"); |
149 |
| - std::cout << "is valid closure " << sol.isValid("[(11111]") << '\n'; |
150 |
| - return 0; |
| 146 | + StringSolution sol; |
| 147 | + std::string s = "AASS,DD.****SSAA"; |
| 148 | + std::string p = "ac*a"; |
| 149 | + std::cout << sol.isMatch(s, p) << std::endl; |
| 150 | + std::cout << "is Palindrome" << std::endl; |
| 151 | + std::cout << sol.isPalindrome(".,") << std::endl; |
| 152 | + std::cout << "atoi\t" << sol.myAtoi("125") << std::endl; |
| 153 | + std::cout << " add Binary\t" << sol.addBinary("1010", "1011") << std::endl; |
| 154 | + printf("const std::string &)))"); |
| 155 | + std::cout << "is valid closure " << sol.isValid("[(11111]") << '\n'; |
| 156 | + return 0; |
151 | 157 | }
|
0 commit comments