Skip to content

Commit 74083c3

Browse files
committed
update
1 parent 1797305 commit 74083c3

File tree

11 files changed

+126
-185
lines changed

11 files changed

+126
-185
lines changed

AddBinary/AddBinary.cpp

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,20 @@ using namespace std;
1818
class Solution {
1919
public:
2020
string addBinary(string a, string b) {
21-
return addBinary1(a, b);
22-
}
23-
24-
string addBinary1(string & a, string & b) {
25-
reverse(begin(a), end(a));
26-
reverse(begin(b), end(b));
27-
if (a.size() < b.size()) swap(a, b);
28-
int na = a.size(), nb = b.size();
29-
int c = 0;
30-
string res(na, '0');
31-
for (int i = 0; i < na; i++) {
32-
int s = a[i] - '0';
33-
s += (i < nb) ? b[i] - '0' : 0;
34-
s += c;
35-
res[i] = '0' + s % 2;
36-
c = s / 2;
21+
string res;
22+
int i = a.size() - 1, j = b.size() - 1, sum = 0;
23+
while (i >= 0 || j >= 0) {
24+
sum += (i == -1 ? 0 : a[i--] - '0');
25+
sum += (j == -1 ? 0 : b[j--] - '0');
26+
res.insert(res.begin(), '0' + sum % 2);
27+
sum /= 2;
3728
}
38-
if (c == 1) res.push_back('1');
39-
reverse(begin(res), end(res));
40-
return res;
41-
}
42-
43-
string addBinary2(string & a, string & b) {
44-
if (a.size() < b.size()) swap(a, b);
45-
int na = a.size(), nb = b.size();
46-
int c = 0;
47-
string res(na, '0');
48-
for (int i = na - 1; i >= 0; i--) {
49-
int j = nb - na + i;
50-
int s = a[i] - '0';
51-
s += (j >= 0) ? b[j] - '0' : 0;
52-
s += c;
53-
res[i] = '0' + s % 2;
54-
c = s / 2;
55-
}
56-
if (c == 1) res.insert(res.begin(), '1');
29+
if (sum == 1) res.insert(res.begin(), '1');
5730
return res;
5831
}
5932
};
6033

34+
6135
int main() {
6236
Solution sol;
6337
string a, b;

EvaluateReversePolishNotation/EvaluateReversePolishNotation.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,32 @@ using namespace std;
2525

2626
class Solution {
2727
public:
28-
int evalRPN(vector<string> tokens) {
28+
int evalRPN(vector<string> &tokens) {
2929
stack<int> stk;
30-
for (size_t i = 0; i < tokens.size(); i++) {
31-
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
32-
int op2 = stk.top();
33-
stk.pop();
34-
int op1 = stk.top();
35-
stk.pop();
36-
if (tokens[i] == "+")
37-
stk.push(op1 + op2);
38-
else if (tokens[i] == "-")
39-
stk.push(op1 - op2);
40-
else if (tokens[i] == "*")
41-
stk.push(op1 * op2);
42-
else if (tokens[i] == "/")
43-
stk.push(op1 / op2);
30+
for (string str : tokens) {
31+
if (str == "+") {
32+
int y = stk.top(); stk.pop();
33+
int x = stk.top(); stk.pop();
34+
stk.push(x + y);
4435
}
45-
else {
46-
stk.push(stoi(tokens[i]));
36+
else if (str == "-") {
37+
int y = stk.top(); stk.pop();
38+
int x = stk.top(); stk.pop();
39+
stk.push(x - y);
4740
}
41+
else if (str == "*") {
42+
int y = stk.top(); stk.pop();
43+
int x = stk.top(); stk.pop();
44+
stk.push(x * y);
45+
}
46+
else if (str == "/") {
47+
int y = stk.top(); stk.pop();
48+
int x = stk.top(); stk.pop();
49+
stk.push(x / y);
50+
}
51+
else stk.push(stoi(str));
4852
}
49-
return stk.top();
53+
return stk.empty() ? 0 : stk.top();
5054
}
5155
};
5256

LRUCache/LRUCache.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ using namespace std;
2121
class LRUCache{
2222
public:
2323
LRUCache(int capacity) {
24-
_capacity = capacity;
25-
_size = 0;
24+
_cap = capacity;
2625
}
2726

2827
int get(int key) {
29-
if (_map.find(key) == _map.end()) return -1;
28+
if (!_map.count(key)) return -1;
3029
auto it = _map[key];
3130
_list.push_front(*it);
3231
_list.erase(it);
@@ -36,26 +35,16 @@ class LRUCache{
3635
}
3736

3837
void set(int key, int value) {
39-
auto it = _map.find(key);
40-
if (it != _map.end()) {
41-
_map.erase(key);
42-
_list.erase(it->second);
43-
_size--;
44-
}
45-
else if (_size == _capacity) {
46-
_map.erase(_list.back().first);
47-
_list.pop_back();
48-
_size--;
49-
}
38+
if (_map.count(key)) _list.erase(_map[key]), _map.erase(key);
39+
else if (_map.size() == _cap) _map.erase(_list.back().first), _list.pop_back();
5040
_list.push_front(make_pair(key, value));
5141
_map[key] = _list.begin();
52-
_size++;
5342
}
5443

44+
private:
45+
int _cap;
5546
list<pair<int, int> > _list;
5647
unordered_map<int, list<pair<int, int> >::iterator> _map;
57-
int _size;
58-
int _capacity;
5948
};
6049

6150
int main() {

LengthofLastWord/LengthofLastWord.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class Solution {
2525
int lengthOfLastWord(const char *s) {
2626
if (s == NULL || *s == '\0') return 0;
2727
int N = strlen(s), i = N - 1;
28-
while (i >= 0 && s[i] == ' ') i--;
28+
while (i >= 0 && isspace(s[i])) i--;
2929
int res = 0;
30-
while (i >= 0 && s[i] != ' ') i--, res++;
30+
while (i >= 0 && !isspace(s[i])) i--, res++;
3131
return res;
3232
}
3333
};

MaxPointsOnaLine/MaxPointsOnaLine.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,28 @@ struct Point {
2525
Point(int x, int y) : x(x), y(y) {}
2626
};
2727

28+
2829
class Solution {
2930
public:
30-
int maxPoints(vector<Point> & ps) {
31-
int N = ps.size();
32-
if (N <= 2) return N;
33-
31+
int maxPoints(vector<Point> &points) {
32+
int N = points.size();
33+
if (N < 3) return N;
3434
int res = 1;
35-
for (int i = 0; i < N - 1; i++) {
36-
int o = 1, v = 0;
35+
for (int i = 0; i < N; i++) {
36+
int o = 1, u = 0, v = 0;
3737
unordered_map<int, int> ks;
38-
for (int j = i + 1; j < N; j++) {
39-
int yy = ps[j].y - ps[i].y;
40-
int xx = ps[j].x - ps[i].x;
38+
for (int j = 0; j < N; j++) {
39+
if (i == j) continue;
40+
int xx = points[j].x - points[i].x;
41+
int yy = points[j].y - points[i].y;
4142
if (xx == 0 && yy == 0) o++;
4243
else if (xx == 0) v++;
43-
else ks[1e6 * yy / xx]++;
44+
else u = max(u, ++ks[1e6 * yy / xx]);
4445
}
45-
res = max(res, o + v);
46-
for (auto k : ks) res = max(res, o + k.second);
46+
res = max(res, o + max(u, v));
4747
}
48-
4948
return res;
5049
}
51-
5250
};
5351

5452
int main() {

MultiplyStrings/MultiplyStrings.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ class Solution {
1515
public:
1616
string multiply(string num1, string num2) {
1717
int n1 = num1.size(), n2 = num2.size();
18-
int n3 = n1+n2;
18+
int n3 = n1 + n2;
1919
string num3(n3, '0');
20-
for (int i = n1-1; i >= 0; i--) {
21-
int c = 0, j;
22-
for (j = n2-1; j >= 0; j--) {
23-
int s = (num1[i]-'0')*(num2[j]-'0')+(num3[i+j+1]-'0')+c;
24-
num3[i+j+1] = s%10+'0';
25-
c = s/10;
20+
for (int i = n1 - 1; i >= 0; i--) {
21+
int sum = 0;
22+
for (int j = n2 - 1; j >= 0; j--) {
23+
sum += (num1[i] - '0') * (num2[j] - '0') + (num3[i + j + 1] - '0');
24+
num3[i + j + 1] = '0' + sum % 10;
25+
sum /= 10;
2626
}
27-
num3[i+j+1] = c+'0';
27+
if (sum > 0) num3[i] = sum + '0';
2828
}
2929
auto it = num3.begin();
30-
while (it < num3.end()-1 && *it == '0')
30+
while (it < num3.end() - 1 && *it == '0')
3131
it = num3.erase(it);
3232
return num3;
3333
}

PlusOne/PlusOne.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,22 @@
77
//============================================================================
88

99
#include <vector>
10-
#include <algorithm>
10+
1111
using namespace std;
1212

1313
class Solution {
1414
public:
1515
vector<int> plusOne(vector<int> &digits) {
16-
int N = digits.size();
17-
for (int i = N - 1; i >= 0; i--) {
18-
if (digits[i] < 9) {
19-
digits[i]++;
20-
return;
21-
}
22-
else {
23-
digits[i] = 0;
16+
vector<int> res = digits;
17+
int i = res.size() - 1;
18+
for (; i >= 0; i--) {
19+
if (res[i] < 9) {
20+
res[i]++;
21+
break;
2422
}
23+
else res[i] = 0;
2524
}
26-
vector<int> res(N + 1, 0);
27-
res[0] = 1;
25+
if (i == -1) res.insert(res.begin(), 1);
2826
return res;
2927
}
3028
};

SimplifyPath/SimplifyPath.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,38 @@ class Solution {
2727
return simplifyPath2(path);
2828
}
2929

30-
string simplifyPath1(string & path) {
30+
string simplifyPath(string path) {
31+
if (path.empty()) return "/";
3132
replace(begin(path), end(path), '/', ' ');
3233
vector<string> vs;
3334
istringstream is(path);
3435
string str;
3536
while (is >> str) {
36-
if (str == "..") {
37-
if (!vs.empty()) vs.pop_back();
38-
continue;
39-
}
40-
if (str != ".") vs.push_back(str);
37+
if (str == "..") { if (!vs.empty()) vs.pop_back(); }
38+
else { if (str != ".") vs.push_back(str); }
4139
}
4240
if (vs.empty()) return "/";
43-
string res;
44-
for (auto s : vs) res += ('/' + s);
45-
return res;
41+
ostringstream os;
42+
for (auto v : vs) os << "/" << v;
43+
return os.str();
4644
}
4745

4846
string simplifyPath2(string & path) {
49-
if (path.empty()) return path;
47+
if (path.empty()) return "/";
5048
if (path.back() != '/') path.push_back('/');
51-
int last = 0;
52-
for (int start = 0, end = 0; end < path.size(); end++) {
49+
int len = 0;
50+
for (int begin = 0, end = 0; end < path.size(); end++) {
5351
if (path[end] != '/') continue;
54-
if (end - start == 3 && path[end - 1] == '.' && path[end - 2] == '.') {
55-
while (last > 0 && path[--last] != '/');
52+
if (end - begin == 3 && path[end - 2] == '.' && path[end - 1] == '.') {
53+
if (len > 0) len--;
54+
while (len > 0 && path[len] != '/') len--;
5655
}
57-
else if (end - start > 2 || (end - start == 2 && path[end - 1] != '.')) {
58-
while (start < end) path[last++] = path[start++];
56+
else if (end - begin > 2 || (end - begin == 2 && path[end - 1] != '.')) {
57+
while (begin < end) path[len++] = path[begin++];
5958
}
60-
start = end;
59+
begin = end;
6160
}
62-
63-
if (last == 0) return "/";
64-
return path.substr(0, last);
61+
return len == 0 ? "/" : path.substr(0, len);
6562
}
6663
};
6764

Sqrtx/Sqrtx.cpp

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,23 @@
66
//
77
// Complexity:
88
// Binary Search, log(x)
9-
// Newton's Method: log(x)
109
//============================================================================
1110

1211
#include <iostream>
12+
1313
using namespace std;
1414

1515
class Solution {
1616
public:
17-
class Solution {
18-
public:
19-
int sqrt(int x) {
20-
return sqrt2(x);
21-
}
22-
23-
int sqrt1(int x) {
24-
long long l = 0ll, u = x;
25-
while (l < u) {
26-
long long m = l + (u - l + 1) / 2;
27-
long long p = m*m;
28-
if (p == x) return (int)m;
29-
if (p > x) u = m - 1;
30-
else l = m;
31-
}
32-
return (int)l;
33-
}
34-
35-
int sqrt2(int x) {
36-
double r0 = 0, r1 = x / 2.0;
37-
while (abs(r1 - r0) > 0.01) {
38-
r0 = r1;
39-
r1 = (r0 + x / r0) / 2.0;
40-
}
41-
return (int)r1;
17+
int sqrt(int x) {
18+
long long l = 0, u = x;
19+
while (l < u) {
20+
long long m = l + (u - l + 1) / 2;
21+
if (m*m > x) u = m - 1;
22+
else l = m;
4223
}
43-
};
24+
return l;
25+
}
4426
};
4527

4628
int main() {

0 commit comments

Comments
 (0)