GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — update · hfl15/algorithm@6b8eb86

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • sword_offer
    • README.md
    • doc
      • 037.md
      • 043.md
    • src
      • 038.cpp
      • 039.cpp
      • 040.cpp
      • 041.cpp
      • 042.cpp
      • 044.cpp
      • 045.cpp
      • 046.cpp
      • 047.cpp
      • 048.cpp
      • 049.cpp
      • 050.cpp
      • 051.cpp
      • 052.cpp
      • 053.cpp
      • 054.cpp
      • 055.cpp
      • 056.cpp
      • 057.cpp
      • 058.cpp
      • 059.cpp
      • 060.cpp
      • 061.cpp
      • 062.cpp
      • 063.cpp
      • 064.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎sword_offer/README.md‎

Copy file name to clipboardExpand all lines: sword_offer/README.md
+28-28Lines changed: 28 additions & 28 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -64,63 +64,63 @@
6464
- [x] 034-最小的K个数. [code](src/034.cpp)
6565
- [x] 035-连续子数组的最大和. [code](src/035.cpp)
6666
- [x] 036-把数组排成最小的数. [code](src/036.cpp). [doc](doc/036.md)
67-
- [x] [037-整数中1出现的次数(从1到n整数中1出现的次数)](src/037.cpp)
68-
- [x] [038-数组中出现次数超过一半的数字](src/038.cpp)
67+
- [x] 037-整数中1出现的次数(从1到n整数中1出现的次数). [code](src/037.cpp). [doc](doc/037.md)
68+
- [x] 038-数组中出现次数超过一半的数字.[code](src/038.cpp)
6969

7070
## 数组:
7171

72-
- [x] [039-构建乘积数组](src/039.cpp)
73-
- [x] [040-数组中重复的数字](src/040.cpp)
74-
- [x] [041-二维数组中的查找](src/041.cpp)
72+
- [x] 039-构建乘积数组. [code](src/039.cpp).
73+
- [x] 040-数组中重复的数字. [code](src/040.cpp)
74+
- [x] 041-二维数组中的查找. [code](src/041.cpp)
7575

7676
## 抽象建模能力:
7777

78-
- [x] [042-扑克牌顺子](src/042.cpp)
79-
- [x] [043-孩子们的游戏(圆圈中最后剩下的数)](src/043.cpp)
78+
- [x] 042-扑克牌顺子. [code](src/042.cpp)
79+
- [x] 043-孩子们的游戏(圆圈中最后剩下的数). [code](src/043.cpp). [doc](doc/043.md)
8080

8181
## 字符串:
8282

83-
- [x] [044-字符流中第一个不重复的字符](src/044.cpp)
84-
- [x] [045-表示数值的字符串](src/045.cpp)
85-
- [x] [046-正则表达式匹配](src/046.cpp)
86-
- [x] [047-替换空格](src/047.cpp)
83+
- [x] 044-字符流中第一个不重复的字符. [code](src/044.cpp)
84+
- [x] 045-表示数值的字符串. [code](src/045.cpp)
85+
- [x] 046-正则表达式匹配. [code](src/046.cpp)
86+
- [x] 047-替换空格. [code](src/047.cpp)
8787

8888
## 回溯法:
8989

90-
- [x] [048-矩阵中的路径](src/048.cpp)
91-
- [x] [049-机器人的运动范围](src/049.cpp)
90+
- [x] 048-矩阵中的路径. [code](src/048.cpp)
91+
- [x] 049-机器人的运动范围. [code](src/049.cpp)
9292

9393
## 发散思维能力:
9494

95-
- [x] [050-不用加减乘除做加法](src/050.cpp)
95+
- [x] 050-不用加减乘除做加法. [code](src/050.cpp)
9696
- [ ] 求1+2+3+...+n
9797

9898
## 分解让复杂问题简单:
9999

100-
- [x] [051-二叉搜索树与双向链表](src/051.cpp)
101-
- [x] [052-复杂链表的复制](src/052.cpp)
102-
- [x] [053-字符串的排列](src/053.cpp)
100+
- [x] 051-二叉搜索树与双向链表. [code](src/051.cpp)
101+
- [x] 052-复杂链表的复制. [code](src/052.cpp)
102+
- [x] 053-字符串的排列. [code](src/053.cpp)
103103

104104
## 位运算:
105105

106-
- [x] [054-二进制中1的个数](src/054.cpp)
106+
- [x] 054-二进制中1的个数. [code](src/054.cpp)
107107

108108
## 代码的鲁棒性:
109109

110-
- [x] [055-树的子结构](src/055.cpp)
111-
- [x] [056-合并两个排序的链表](src/056.cpp)
112-
- [x] [057-链表中倒数第k个结点](src/057.cpp)
113-
- [x] [058-调整数组顺序使奇数位于偶数前面](src/058.cpp)
110+
- [x] 055-树的子结构. [code](src/055.cpp)
111+
- [x] 056-合并两个排序的链表. [code](src/056.cpp)
112+
- [x] 057-链表中倒数第k个结点. [code](src/057.cpp)
113+
- [x] 058-调整数组顺序使奇数位于偶数前面. [code](src/058.cpp)
114114

115115
## 代码的完整性:
116116

117-
- [x] [059-数值的整数次方](src/059.cpp)
117+
- [x] 059-数值的整数次方. [code](src/059.cpp)
118118

119119
## 举例让抽象具体化:
120120

121-
- [x] [060-栈的压入、弹出序列](src/060.cpp)
122-
- [x] [061-二叉搜索树的后序遍历序列](src/061.cpp)
123-
- [x] [062-从上往下打印二叉树](src/062.cpp)
124-
- [x] [063-二叉树中和为某一值的路径](src/063.cpp)
125-
- [x] [064-包含min函数的栈](src/064.cpp)
121+
- [x] 060-栈的压入、弹出序列. [code](src/060.cpp)
122+
- [x] 061-二叉搜索树的后序遍历序列. [code](src/061.cpp)
123+
- [x] 062-从上往下打印二叉树. [code](src/062.cpp)
124+
- [x] 063-二叉树中和为某一值的路径. [code](src/063.cpp)
125+
- [x] 064-包含min函数的栈. [code](src/064.cpp)
126126

Collapse file

‎sword_offer/doc/037.md‎

Copy file name to clipboard
+117Lines changed: 117 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
## 题目
2+
3+
求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数。
4+
5+
## 思考
6+
7+
### 方法1:
8+
9+
依次从 1 到 n 遍历,统计每个数中 1 出现的次数,然后累加起来。时间复杂度:遍历 n 个数,每次统计可以在 log(n) 时间内完成,因此 O(nlog(n))。
10+
11+
### 方法2:
12+
13+
新的角度:给定 N,统计“小于 N 的数在每一位上可能出现 1 的次数” 之和,可得到最终的解。使用函数 f(N) 表示。
14+
15+
**1 位数的情况**
16+
17+
如果 N=4,则所有可考虑的数为: 1,2,3,4 由此可得 f(N) = 1。不难看出对于所有 1<=N<=9, f(N) = 1,特殊的有 f(0)=0。
18+
19+
**2 位数的情况**
20+
21+
假设 N=14,可考虑的数为:1,2,3,4,5,6,7,8,9,10,11,12,13,14。
22+
个位上出现 1 的情况:1,11; 2个
23+
十位上出现 1 的情况:10, 11, 12, 13, 14;5个
24+
综合:f(N)=个位数的情况 + 十位数的情况 = 2 + 5 = 7
25+
26+
一般的,
27+
28+
- 个位上 1 的个数统计可以有两种情况:
29+
- 情况 1 : 当前数字 N 个位数是 0,则个位上 1 出现的次数为十位出现的数。(比如 N=10,个位上出现 1 的次数是 1)
30+
- 情况 2 :当前数字 N 个位数大于 0, 则个位上 1 出现的次数为十位出现的数加 1。(比如 N=14,个位上出现 1 的次数是 1+1=2,及 1 和 11)
31+
- 十位上 1 的个数统计:
32+
- 情况 1 : 当十位上的数为 1 时,则十位上出现 1 的次数为:个位上的数加 1 (比如 N=14, 十位上出现 1 的次数 = 4+1=5,及:10,11,12,13,14)
33+
- 情况 2 :当十位上的数大于 1 时,则十位上出现 1 的次数为 10 次。(比如 N=24, 十位上出现 1 的次数 = 10,及:10,11,12,13,14,15,16,17,18,19)
34+
35+
其他两位数字可以类似的方法很快计算出来
36+
37+
**3 位数的情况**
38+
39+
假设 N = 132
40+
41+
- 个位上出现 1 的次数为 14 次:1,11,21,...,91,101,111,121,131
42+
- 十位上出现 1 的次数为 20 次:10~19, 110~119
43+
- 百位上出现 1 的次数为 33 次:100~132
44+
45+
46+
**一般化**
47+
48+
同理可以计算 4,5,甚至 k 位数中 1 的个数。
49+
一般的,假设有数 N = a<sub>k</sub>... a<sub>1</sub>
50+
我们可以将数字分成 3 个部分,高位部分,当前计算的位置,低位部分;如果计算第 i 位上出现 1 的次数,则高位部分为 a<sub>k</sub>...a<sub>i+1</sub>,当前计算位置 a<sub>i</sub>,低位部分 a<sub>i-1</sub>...a<sub>1</sub>。而第 i 位上出现 1 的次数所受影响的来源也将分配到这 3 个部分当中。
51+
52+
在来一个例子,假设 N = 13014
53+
54+
- 个位出现 1 的次数,ihight=1301, icur = 4,因此 icur > 1,所以 1 的次数为 ihight+1 = 1302
55+
- 十位出现 1 的次数,ihight=130, icur=1, ilow=4,因此 1 的次数为 ihight\*10+ilow+1=130\*10+4+1=1305
56+
- 百位出现 1 的次数,ihight=13, icur=0, ilow=14
57+
- icur = 0,影响只来源于高位部分,13 个 100,每个100在百位上将会出现100个1,也就是 13*100=1300
58+
- icur = 1,假设 N=13114,影响来源于高位和低位:
59+
- 高位部分,ihight=13, 13 个 100,则 1 的个数为 13 * 100 = 1300
60+
- 低位部分,ilow=14,13100~13114,1 的个数为 ilow+1 = 14+1 = 15
61+
- icur > 1,假设 N=13214,影响仅来源于高位:
62+
- ihight = 13,13 个 100,当前尾部 13100~13199 有一个100,总攻是 14 个,及 ihight+1 个 100,1 的个数为 (13+1)*100 = 1400
63+
64+
由此规律很容易实现代码。
65+
66+
**思考总线,从简单的例子入手找规律,抽象出一般化的规律,要注意细节**
67+
68+
## code
69+
70+
``` c++
71+
72+
#include <iostream>
73+
#include <vector>
74+
#include <algorithm>
75+
76+
using namespace std;
77+
78+
class Solution{
79+
public:
80+
int NumberOf1Between1AndN_Solution(int n){
81+
int icout = 0;
82+
int ihight = 0;
83+
int ilow = 0;
84+
int icur = 0;
85+
int ifactor = 1;
86+
87+
while( n / ifactor != 0){
88+
ihight = n / (ifactor*10);
89+
icur = (n / ifactor) % 10;
90+
ilow = n - (n / ifactor) * ifactor;
91+
92+
switch(icur){
93+
case 0 :
94+
icout += ihight*ifactor;
95+
break;
96+
case 1 :
97+
icout += ihight*ifactor + ilow + 1;
98+
break;
99+
default:
100+
// icur > 1
101+
icout += (ihight+1)*ifactor;
102+
103+
}
104+
105+
ifactor *= 10; // scan from the low-order to high-order
106+
}
107+
108+
return icout;
109+
}
110+
};
111+
112+
int main()
113+
{
114+
return 0;
115+
}
116+
117+
```
Collapse file

‎sword_offer/doc/043.md‎

Copy file name to clipboard
+140Lines changed: 140 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
## 题目
2+
3+
孩子们的游戏,圆圈中最后剩下的数字。n 个小朋友,从 0 开始编号到 n-1,每次都删除第 m 个小朋友。其中删除以后从被删除结点的下一个结点开始从 0 开始编号。最后只剩下一个小朋友时游戏结束,这个小朋友的编号是多少?
4+
5+
## 思考
6+
7+
### 方法1:
8+
9+
循环链表模拟游戏过程。可以自己实现循环链表结构,也可以直接是用 C++ 的 list 来模拟循环链表。其中使用链表结构而不是数组结构是因为要保证删除/移动的时间复杂度为O(1)。空间复杂度:O(n);时间复杂度:每次删除 1 个结点,需要删除 n-1 个结点,每删除一个结点需要循环 m 次,则复杂度为 O(n*m)。
10+
11+
**注意:分析时间复杂度时要结合使用的语言和数据结构的特性**
12+
13+
c++ 中的 list 结构虽然能保证增加和删除节点的操作在常数时间内完成,但获取链表长度的操作确是线性的复杂度。为了避免这样的复杂度,在代码实现时候应该避免使用这个耗时的操作。list 中剩下最后一个结点,也就意味着我们要删除 n-1 个结点,使用 for 循环控制 n-1 次删除操作,而不是检查当前 list 的 size,以保证高效型。
14+
15+
### 方法2:
16+
17+
从数学的角度可以让求解更高效,下面将是比较复杂的分析推理过程。最优解,时间复杂度O(n),空间复杂度O(1)。
18+
19+
假设有关于 n 和 m 的函数 f(n, m),表示从 n 个数字,seq=(0,1,..,n-1),开始,每次删除第 m 个数字后**最后剩下的数字**。
20+
21+
第一次删除操作:
22+
删除的数字为 k = (m-1)%n
23+
剩下的序列为 ,共 n-1 个数字,根据游戏规则重新调整得到序列 seq' = (k+1,...,n-1,0,1,..,k-1),即从被删除的下一个结点开始编号,将 k+1 放到了序列的第一个位置。
24+
25+
值得注意的是 seq' 与 seq 的形式不同,假设有新的关于 n 和 m 的函数 f'(n-1, m),表示从 seq' 开始,每次删除第 m 个数字后**最后剩下的数字**
26+
27+
综上必然有:f(n,m) = f'(n-1,m)
28+
29+
f' 函数是否与 f 函数有什么关系呢???
30+
31+
对 seq' 重新编号:
32+
33+
| | | | | | | | | |
34+
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
35+
| seq' |k+1 |k+2 |... |n-1 |0 |1 |... |k-1 |
36+
|y=seq'-k-1|0 |1 |... |n-k-2|-k-1 |-k |... |-2 |
37+
|seq''=y%n |0 |1 |... |n-k-2|n-k-1|n-k |... |n-2 |
38+
39+
细节:
40+
seq' 有 n-1 个数,对应编号为 0,..,n-2。k+1 编号为 0,也就是以 k+1 为基准对序列重新编号,那么对于序列中的任意一个数 x,编号为 y = x-(k+1) = x-k-1。
41+
当 x 取 [0,..,k-1] 时,y 为负数,对 n 取模,可以将数映射到 [0,n] 的范围内,并保证新序列依旧有序。
42+
43+
综上:从 f' 函数中的序列 seq' 进行映射 g(x) = (x-k-1)%n,可以得到类似 seq 的序列 seq''=(0,1,..,n-2),是 f(n,m) 的子问题 f(n-1,m)。
44+
45+
g(x) = (x-k-1)%n 的逆映射为 g'(x) = (x+k+1)%n。(可以将这一过程想象成循环移位的过程也就好理解了,原的映射是在 x 的基础上向左边循环移 (k+1)位,那么逆过程就是在原来的基础上向右循环移 (k+1),左移为“减”,右移为“加”)。
46+
47+
根据 g(x) 和 g'(x) 我们可以将 f 和 f' 联系起来
48+
49+
```
50+
f(n,m) = f'(n-1,m)
51+
= g'(f(n-1, m)) ..... f'(n-1,m) = f(n-1,m),参考上文分析
52+
= [f(n-1, m)+k+1]%n ...... f(n-1,m) 带入 g'
53+
= [f(n-1, m)+(m-1)%n+1]%n ...... k = (m-1)%n 带入
54+
= [f(n-1, m)+m]%n
55+
56+
```
57+
58+
细节,[f(n-1, m)+(m-1)%n+1]%n 的展开:
59+
由题目肯定有:0<=f(n-1, m)<=n-1,可以写成 f(n-1, m)%n,显然有 1 = 1%n
60+
则有:[f(n-1, m)+(m-1)%n+1]%n = [f(n-1, m)%n+(m-1)%n+1%n]%n = [f(n-1, m)+(m-1)+1]%n = [f(n-1, m)+m]%n
61+
62+
综上可以得到递归式:
63+
64+
当 n > 1 时: f(n,m) = [f(n-1, m)+m]%n
65+
当 n = 1 时: f(n,m)=0。
66+
67+
接下来的代码就好实现了。
68+
69+
**总体思路:将问题抽象成函数表达式,问题拆解,能否找到重叠子问题,递归求解?**
70+
71+
72+
## code
73+
74+
**注意不合法情况,即边界条件的检查**
75+
76+
``` c++
77+
78+
#include <iostream>
79+
#include <vector>
80+
#include <algorithm>
81+
#include <list>
82+
83+
using namespace std;
84+
85+
class Solution{
86+
public:
87+
int LastRemaining_Solution(int n, int m){
88+
//return LastRemainingList(n, m);
89+
return LastRemainingDynamic(n, m);
90+
}
91+
92+
// time-O(m*n), spcae-O(n)
93+
int LastRemainingList(int n, int m){
94+
if(n <= 0 || m <= 0)
95+
return -1;
96+
list<int> children;
97+
for(int i=0; i<n; i++){// no. from 0 to (n-1)
98+
children.push_back(i);
99+
}
100+
101+
auto it = children.begin(); // no. 0
102+
for(int k=0; k<n-1; k++){
103+
// Note: while(children.size() > 1) is not appropriate, because the complexity of children.size() is up to linear
104+
// until now, it point to the 0-th child
105+
for(int i=1; i<m; i++){ // loop from th 1-th child to m-1
106+
it++;
107+
if(it == children.end())
108+
it = children.begin();
109+
}
110+
111+
// until now, it point to the (m-1)-th child
112+
it = children.erase(it); // erase will return the following iterator
113+
if(it == children.end())
114+
it = children.begin();
115+
}
116+
117+
return children.front();
118+
}
119+
120+
// time-O(n), space-O(1)
121+
int LastRemainingDynamic(int n, int m){
122+
if(n<=0 || m<=0)
123+
return -1;
124+
125+
int f = 0; // f[1,m] = 0
126+
for(int i=2; i<=n; i++){
127+
// calculate f[i,m]
128+
f = (f+m)%i;
129+
}
130+
131+
return f;
132+
}
133+
};
134+
135+
int main()
136+
{
137+
return 0;
138+
}
139+
140+
```
Collapse file

‎sword_offer/src/038.cpp‎

Copy file name to clipboardExpand all lines: sword_offer/src/038.cpp
+14-1Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
/*
2-
[数组中出现次数超过一半的数字]
32
3+
[数组中出现次数超过一半的数字]
4+
5+
[题目]
46
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。
57
例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。
68
由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
9+
10+
[解析]
11+
方法0:
12+
双重循环统计每个数的次数,最笨的方法,时间复杂度-O(n^2)
13+
方法1:
14+
使用 hash 表对数组中的每个数进行统计,由此找出次数超过一半的数;时间复杂度 O(n),空间复杂度-O(n)
15+
方法2:
16+
实际上超过数组长度一半的数,如果这个数存在,当数组有序时,第 n/2,即中位数一定是答案。
17+
这就可以将问题转换成 top k 的问题,k = n/2,最好的情况下以 partition 的思想可以在平均时间复杂度 O(n) 的情况下完成。
18+
方法3:
19+
如下面代码实现。使用trick,我们成对的删除数字,最后剩下的数字肯定是超过半数的数字。
720
*/
821

922
#include <iostream>
Collapse file

‎sword_offer/src/039.cpp‎

Copy file name to clipboardExpand all lines: sword_offer/src/039.cpp
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
/*
22
[构建乘积数组]
3-
3+
4+
[题目]
45
给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],
56
其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。
7+
8+
[解析]
9+
参看代码
10+
时间复杂度-O(n),空间复杂度-O(n)。
611
*/
712

813
#include <iostream>

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.