Skip to content

Commit 60bd090

Browse files
committed
update
1 parent 2175227 commit 60bd090

File tree

5 files changed

+54
-46
lines changed

5 files changed

+54
-46
lines changed

MaxPointsOnaLine/MaxPointsOnaLine.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,16 @@ class Solution {
3131
int maxPoints(vector<Point> &points) {
3232
int N = points.size();
3333
if (N < 3) return N;
34-
int res = 1;
34+
unordered_map<int, int> tb;
35+
int res = 0;
3536
for (int i = 0; i < N; i++) {
36-
int o = 1, u = 0, v = 0;
37-
unordered_map<int, int> ks;
37+
tb.clear();
38+
int o = 0, u = 0, v = 0;
3839
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;
42-
if (xx == 0 && yy == 0) o++;
43-
else if (xx == 0) v++;
44-
else u = max(u, ++ks[1e6 * yy / xx]);
40+
int x = points[j].x - points[i].x, y = points[j].y - points[i].y;
41+
if (x == 0 && y == 0) o++;
42+
else if (x == 0) v++;
43+
else u = max(u, ++tb[1e6*y / x]);
4544
}
4645
res = max(res, o + max(u, v));
4746
}

MultiplyStrings/MultiplyStrings.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ using namespace std;
1414
class Solution {
1515
public:
1616
string multiply(string num1, string num2) {
17+
if (num1.empty() || num2.empty()) return "";
1718
int n1 = num1.size(), n2 = num2.size();
18-
int n3 = n1 + n2;
19-
string num3(n3, '0');
19+
string num3(n1 + n2, '0');
2020
for (int i = n1 - 1; i >= 0; i--) {
21-
int sum = 0;
21+
int sum = 0, carry = 0;
2222
for (int j = n2 - 1; j >= 0; j--) {
23-
sum += (num1[i] - '0') * (num2[j] - '0') + (num3[i + j + 1] - '0');
23+
sum = carry + (num3[i + j + 1] - '0') + (num1[i] - '0') * (num2[j] - '0');
2424
num3[i + j + 1] = '0' + sum % 10;
25-
sum /= 10;
25+
carry = sum / 10;
2626
}
27-
if (sum > 0) num3[i] = sum + '0';
27+
num3[i] = '0' + carry;
2828
}
29-
auto it = num3.begin();
30-
while (it < num3.end() - 1 && *it == '0')
31-
it = num3.erase(it);
29+
while (num3.size() > 1 && (*num3.begin()) == '0') num3.erase(num3.begin());
3230
return num3;
3331
}
3432
};

Searcha2DMatrix/Searcha2DMatrix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
// Given target = 3, return true.
1818
//
1919
// Complexity:
20-
// 2D Binary Search O(m*nlog(m*n)) time
21-
// Upper Right Linear Search O(m*n) time
20+
// 2D Binary Search O(max(m,n)*log(max(m,n))) time
21+
// Upper Right Linear Search O(m+n) time
2222
// 1D Binary Search O(log(m*n)) time
2323
//============================================================================
2424

SurroundedRegions/SurroundedRegions.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,44 @@
2727

2828
using namespace std;
2929

30+
int di[] = {-1, 1, 0, 0};
31+
int dj[] = {0, 0, -1, 1};
32+
3033
class Solution {
3134
public:
3235
void solve(vector<vector<char>> &board) {
3336
if (board.empty() || board[0].empty()) return;
3437
int M = board.size(), N = board[0].size();
35-
for (int j = 0; j < N; j++) {
36-
if (board[0][j] == 'O') bfs(board, M, N, 0, j);
37-
if (board[M - 1][j] == 'O') bfs(board, M, N, M - 1, j);
38-
}
3938
for (int i = 0; i < M; i++) {
4039
if (board[i][0] == 'O') bfs(board, M, N, i, 0);
41-
if (board[i][N - 1] == 'O') bfs(board, M, N, i, N - 1);
40+
if (board[i][N-1] == 'O') bfs(board, M, N, i, N-1);
4241
}
42+
for (int j = 0; j < N; j++) {
43+
if (board[0][j] == 'O') bfs(board, M, N, 0, j);
44+
if (board[M-1][j] == 'O') bfs(board, M, N, M-1, j);
45+
}
46+
4347
for (int i = 0; i < M; i++) {
4448
for (int j = 0; j < N; j++) {
45-
if (board[i][j] == 'O') board[i][j] = 'X';
46-
else if (board[i][j] == 'D') board[i][j] = 'O';
49+
if (board[i][j] == 'D') board[i][j] = 'O';
50+
else if (board[i][j] == 'O') board[i][j] = 'X';
4751
}
4852
}
4953
}
5054

5155
void bfs(vector<vector<char>> &board, int M, int N, int i, int j) {
56+
board[i][j] = 'D';
5257
queue<int> qs;
53-
board[i][j] = 'D', qs.push(i*N + j);
58+
qs.push(i*N+j);
5459
while (!qs.empty()) {
55-
i = qs.front() / N, j = qs.front() % N, qs.pop();
56-
if (i - 1 >= 0 && board[i - 1][j] == 'O') board[i - 1][j] = 'D', qs.push((i - 1)*N + j);
57-
if (i + 1 < M && board[i + 1][j] == 'O') board[i + 1][j] = 'D', qs.push((i + 1)*N + j);
58-
if (j - 1 >= 0 && board[i][j - 1] == 'O') board[i][j - 1] = 'D', qs.push(i*N + j - 1);
59-
if (j + 1 < N && board[i][j + 1] == 'O') board[i][j + 1] = 'D', qs.push(i*N + j + 1);
60+
i = qs.front()/N, j = qs.front()%N;
61+
qs.pop();
62+
for (int k = 0; k < 4; k++) {
63+
int ni = i+di[k], nj = j+dj[k];
64+
if (ni == -1 || ni == M || nj == -1 || nj == N || board[ni][nj] != 'O') continue;
65+
board[ni][nj] = 'D';
66+
qs.push(ni*N+nj);
67+
}
6068
}
6169
}
6270
};
@@ -91,4 +99,4 @@ int main() {
9199
}
92100

93101
return 0;
94-
}
102+
}

TextJustification/TextJustification.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,34 @@ class Solution {
4444
public:
4545
vector<string> fullJustify(vector<string> &words, int L) {
4646
vector<string> res;
47+
int N = words.size();
4748
int begin = 0, end = 0;
48-
while (end < words.size()) {
49+
while (end < N) {
4950
int len = L;
50-
for (; end < words.size(); end++) {
51-
int tmp = len - (len == L ? words[end].size() : words[end].size() + 1);
51+
for (; end < N; end++) {
52+
int tmp = len - words[end].size() - (len == L ? 0 : 1);
5253
if (tmp < 0) break;
5354
len = tmp;
5455
}
55-
ostringstream os;
5656
int num = end - begin - 1;
57-
if (num != 0 && end != words.size()) {
58-
int avg = len / num;
57+
ostringstream os;
58+
if (num != 0 && end != N) {
59+
int avg = len / num + 1;
5960
len %= num;
6061
for (; begin < end; begin++) {
61-
if (begin == end - 1) os << words[begin];
62-
else os << words[begin] << string(avg + 1, ' ');
63-
if (len > 0) os << ' ', len--;
62+
os << words[begin];
63+
if (begin != end - 1) {
64+
os << string(avg, ' ');
65+
if (len > 0) os << " ", len--;
66+
}
6467
}
6568
}
6669
else {
6770
for (; begin < end; begin++) {
68-
if (begin == end - 1) os << words[begin];
69-
else os << words[begin] << ' ';
71+
os << words[begin];
72+
if (begin != end - 1) os << " ";
7073
}
71-
if (len > 0) os << string(len, ' ');
74+
os << string(len, ' ');
7275
}
7376
res.push_back(os.str());
7477
}

0 commit comments

Comments
 (0)