Skip to content

Commit 9ce5dec

Browse files
committed
update leetcode 440; reupdate leetcode 461, 462, 463, 464, 467, 468, 470
1 parent 453c41b commit 9ce5dec

10 files changed

+156
-106
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "public.h"
2+
3+
//0ms, 100%
4+
//十叉树, int数的高位为根, 低位为儿子
5+
6+
class Solution {
7+
private:
8+
int n;
9+
10+
//参数: prefix: 树前缀: 即从根部到当前节点(当前根节点)的值
11+
//返回以当前根节点为根的十叉树的节点总数
12+
//一般情况下此总数将是1/11/111...等
13+
int getNumOfNodes(int prefix)
14+
{
15+
int count = 0;
16+
for (long long cur = prefix, next = prefix + 1; cur <= this->n; cur *= 10, next *= 10)
17+
count += min(next, (long long)this->n + 1) - cur;
18+
return count;
19+
}
20+
21+
public:
22+
int findKthNumber(int n, int k) {
23+
this->n = n;
24+
int p = 1;
25+
int prefix = 1;
26+
while (p < k)
27+
{
28+
int count = getNumOfNodes(prefix);
29+
if ((p + count) > k) //字典序第k的数在当前前缀下
30+
{
31+
prefix *= 10;
32+
p++;
33+
}
34+
else //字典序第k的数不在当前前缀下, 那应该增加当前前缀
35+
{
36+
prefix++;
37+
p += count;
38+
}
39+
}
40+
return prefix;
41+
}
42+
};

cpp/leetcode/461. hamming-distance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "public.h"
22

3-
//8ms, 29.73%
3+
//0ms, 100%
44
//ÇóÒì»ò, ¿´1µÄ¸öÊý
55

66
class Solution {
@@ -10,7 +10,7 @@ class Solution {
1010
int res = 0;
1111
while (x_or > 0)
1212
{
13-
res += x_or % 2;
13+
if (x_or % 2) res++;
1414
x_or /= 2;
1515
}
1616
return res;

cpp/leetcode/462. minimum-moves-to-equal-array-elements-ii.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "public.h"
22

3-
//12ms, 100%
3+
//16ms, 93.81%
44
//ÕÒµ½ÖÐλÊý
55

66
class Solution {

cpp/leetcode/463. island-perimeter.cpp

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,29 @@
11
#include "public.h"
22

3-
//208ms, 5.36%
4-
//DFS
3+
//68ms, 97.55%
54
//注意只有一个岛屿而且没有湖
6-
//如果岛屿某个方向是边界或者水, 那么将被计算到周长里面
5+
//对于岛屿: 每当周围多一个岛它的周长贡献减1, 每个岛的最大周长贡献是4
6+
//直接双重循环
77

88
class Solution {
9-
private:
10-
int res = 0;
11-
void DFS(const vector<vector<int>>& grid, vector<vector<bool>>& issearched,
12-
int r, int c)
13-
{
14-
issearched[r][c] = true;
15-
if (r == 0) res++;
16-
else
17-
{
18-
if (grid[r - 1][c] == 0) res++;
19-
else if (!issearched[r - 1][c]) DFS(grid, issearched, r - 1, c);
20-
}
21-
22-
if (c == 0) res++;
23-
else
24-
{
25-
if (grid[r][c - 1] == 0) res++;
26-
else if (!issearched[r][c - 1]) DFS(grid, issearched, r, c - 1);
27-
}
28-
29-
if (r == grid.size() - 1) res++;
30-
else
31-
{
32-
if (grid[r + 1][c] == 0) res++;
33-
else if (!issearched[r + 1][c]) DFS(grid, issearched, r + 1, c);
34-
}
35-
36-
if (c == grid[0].size() - 1) res++;
37-
else
38-
{
39-
if (grid[r][c + 1] == 0) res++;
40-
else if (!issearched[r][c + 1]) DFS(grid, issearched, r, c + 1);
41-
}
42-
}
43-
449
public:
4510
int islandPerimeter(vector<vector<int>>& grid) {
46-
vector<vector<bool>> issearched(grid.size(), vector<bool>(grid[0].size(), false));
47-
//找到一块陆地, DFS
48-
for (int r = 0; r < grid.size(); ++r)
49-
for (int c = 0; c < grid[0].size(); ++c)
11+
int res = 0;
12+
int gSize = grid.size();
13+
int g0Size = grid[0].size();
14+
for (int r = 0; r < gSize; ++r)
15+
for (int c = 0; c < g0Size; ++c)
16+
{
5017
if (grid[r][c] == 1)
5118
{
52-
DFS(grid, issearched, r, c);
53-
goto bigbreak;
19+
int contribute = 4;
20+
if (r > 0 && grid[r - 1][c] == 1) contribute--;
21+
if (c > 0 && grid[r][c - 1] == 1) contribute--;
22+
if (r < (gSize - 1)&& grid[r + 1][c] == 1) contribute--;
23+
if (c < (g0Size - 1) && grid[r][c + 1] == 1) contribute--;
24+
res += contribute;
5425
}
55-
bigbreak:
26+
}
5627
return res;
5728
}
5829
};

cpp/leetcode/464. can-i-win.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "public.h"
22

3-
//28ms, 83.56%
3+
//24ms, 87.61%
44
//用20位二进制表示是否当前组合被选取过
5+
//用DFS(也就是递归)穷举当前可能的情况并确定胜负问题
56

67
class Solution {
78
private:
@@ -37,9 +38,11 @@ class Solution {
3738
}
3839
};
3940

41+
/*
4042
int main()
4143
{
4244
Solution* s = new Solution();
4345
cout << s->canIWin(10, 11);
4446
return 0;
4547
}
48+
*/

cpp/leetcode/467. unique-substrings-in-wraparound-string.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "public.h"
22

3-
//12ms, 74.19%
3+
//12ms, 73.01%
44
//dp, 用vector存储, 记录以char结尾的子字符串的长度
55
//最后res用各个长度来更新, 等于sum长度们
66

@@ -9,33 +9,33 @@ class Solution {
99
int findSubstringInWraproundString(string p) {
1010
if (p.empty()) return 0;
1111

12-
vector<int> length(26, 0);
12+
vector<int> length(256, 0); //其实只有中间a-z有效
1313
//int res = 0;
1414
int oldlen = 1;
15-
length[p[0] - 'a']++;
15+
length[p[0]]++;
1616

1717
for (int loc = 1; loc < p.size(); ++loc)
1818
{
1919
if (p[loc] - p[loc - 1] == 1 || p[loc] - p[loc - 1] == -25)
2020
{
21-
if (length[p[loc] - 'a'] <= oldlen)
21+
if (length[p[loc]] <= oldlen)
2222
{
2323
oldlen++;
24-
length[p[loc] - 'a'] = oldlen;
24+
length[p[loc]] = oldlen;
2525
}
2626
else oldlen++;
2727
}
2828
else
2929
{
30-
if (length[p[loc] - 'a'] == 0)
30+
if (length[p[loc]] == 0)
3131
{
32-
length[p[loc] - 'a'] = 1;
32+
length[p[loc]] = 1;
3333
oldlen = 1;
3434
}
3535
else oldlen = 1;
3636
}
3737
}
38-
return accumulate(length.begin(), length.end(), 0);
38+
return accumulate(length.begin() + 'a', length.begin() + 'z' + 1, 0);
3939
}
4040
};
4141

cpp/leetcode/468. validate-ip-address.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "public.h"
22

3-
//4ms, 81.45%
3+
//0ms, 100%
44
//模拟
55

66
class Solution {
@@ -84,6 +84,7 @@ class Solution {
8484
}
8585
};
8686

87+
/*
8788
int main()
8889
{
8990
Solution* s = new Solution();
@@ -93,3 +94,4 @@ int main()
9394
//cout << s->validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334");
9495
return 0;
9596
}
97+
*/

cpp/leetcode/470. implement-rand10-using-rand7.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "public.h"
22

3-
//108ms, 67.78%
3+
//88ms, 95.38%
44
//先获取1-5随机数, 再获取1-6随机数以是否大于3决定是否再来一次1-5随机数
55
// The rand7() API is already defined for you.
66
/*这是假的rand7()函数*/

cpp/leetcode/leetcode.vcxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@
382382
<ClCompile Include="436. find-right-interval.cpp" />
383383
<ClCompile Include="437. path-sum-iii.cpp" />
384384
<ClCompile Include="438. find-all-anagrams-in-a-string.cpp" />
385+
<ClCompile Include="440. k-th-smallest-in-lexicographical-order.cpp" />
385386
<ClCompile Include="441. arranging-coins.cpp" />
386387
<ClCompile Include="442. find-all-duplicates-in-an-array.cpp" />
387388
<ClCompile Include="443. string-compression.cpp" />
@@ -399,6 +400,13 @@
399400
<ClCompile Include="457. circular-array-loop.cpp" />
400401
<ClCompile Include="459. repeated-substring-pattern.cpp" />
401402
<ClCompile Include="461. hamming-distance.cpp" />
403+
<ClCompile Include="462. minimum-moves-to-equal-array-elements-ii.cpp" />
404+
<ClCompile Include="463. island-perimeter.cpp" />
405+
<ClCompile Include="464. can-i-win.cpp" />
406+
<ClCompile Include="467. unique-substrings-in-wraparound-string.cpp" />
407+
<ClCompile Include="468. validate-ip-address.cpp" />
408+
<ClCompile Include="470. implement-rand10-using-rand7.cpp" />
409+
<ClCompile Include="473. Matchsticks to Square.cpp" />
402410
<ClCompile Include="摩尔投票法升级229. majority-element-ii.cpp" />
403411
<ClCompile Include="230. kth-smallest-element-in-a-bst.cpp" />
404412
<ClCompile Include="231. power-of-two.cpp" />

0 commit comments

Comments
 (0)