Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_dailycode
提交
31246570
S
skill_tree_dailycode
项目概览
CSDN 技术社区
/
skill_tree_dailycode
通知
11
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
2
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_dailycode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
2
Issue
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
31246570
编写于
12月 10, 2021
作者:
每日一练社区
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化45题选项生成
上级
8b4f11f6
变更
45
隐藏空白更改
内联
并排
Showing
45 changed file
with
3119 addition
and
335 deletion
+3119
-335
data/2.dailycode中阶/1.cpp/43.exercises/solution.md
data/2.dailycode中阶/1.cpp/43.exercises/solution.md
+59
-21
data/2.dailycode中阶/1.cpp/44.exercises/solution.md
data/2.dailycode中阶/1.cpp/44.exercises/solution.md
+117
-4
data/2.dailycode中阶/1.cpp/45.exercises/solution.md
data/2.dailycode中阶/1.cpp/45.exercises/solution.md
+55
-4
data/2.dailycode中阶/1.cpp/46.exercises/solution.md
data/2.dailycode中阶/1.cpp/46.exercises/solution.md
+47
-4
data/2.dailycode中阶/1.cpp/47.exercises/solution.md
data/2.dailycode中阶/1.cpp/47.exercises/solution.md
+40
-4
data/2.dailycode中阶/1.cpp/48.exercises/solution.md
data/2.dailycode中阶/1.cpp/48.exercises/solution.md
+118
-4
data/2.dailycode中阶/1.cpp/49.exercises/solution.md
data/2.dailycode中阶/1.cpp/49.exercises/solution.md
+93
-4
data/2.dailycode中阶/1.cpp/50.exercises/solution.md
data/2.dailycode中阶/1.cpp/50.exercises/solution.md
+98
-4
data/2.dailycode中阶/1.cpp/51.exercises/solution.md
data/2.dailycode中阶/1.cpp/51.exercises/solution.md
+61
-4
data/2.dailycode中阶/1.cpp/52.exercises/solution.md
data/2.dailycode中阶/1.cpp/52.exercises/solution.md
+60
-4
data/2.dailycode中阶/1.cpp/53.exercises/solution.md
data/2.dailycode中阶/1.cpp/53.exercises/solution.md
+30
-4
data/2.dailycode中阶/1.cpp/54.exercises/solution.md
data/2.dailycode中阶/1.cpp/54.exercises/solution.md
+61
-4
data/2.dailycode中阶/1.cpp/55.exercises/solution.md
data/2.dailycode中阶/1.cpp/55.exercises/solution.md
+70
-4
data/2.dailycode中阶/1.cpp/56.exercises/solution.md
data/2.dailycode中阶/1.cpp/56.exercises/solution.md
+87
-52
data/2.dailycode中阶/1.cpp/57.exercises/solution.md
data/2.dailycode中阶/1.cpp/57.exercises/solution.md
+55
-4
data/2.dailycode中阶/1.cpp/58.exercises/solution.md
data/2.dailycode中阶/1.cpp/58.exercises/solution.md
+73
-4
data/2.dailycode中阶/1.cpp/59.exercises/solution.md
data/2.dailycode中阶/1.cpp/59.exercises/solution.md
+110
-4
data/2.dailycode中阶/1.cpp/60.exercises/solution.md
data/2.dailycode中阶/1.cpp/60.exercises/solution.md
+45
-4
data/2.dailycode中阶/1.cpp/61.exercises/solution.md
data/2.dailycode中阶/1.cpp/61.exercises/solution.md
+44
-4
data/2.dailycode中阶/1.cpp/62.exercises/solution.md
data/2.dailycode中阶/1.cpp/62.exercises/solution.md
+72
-52
data/2.dailycode中阶/1.cpp/63.exercises/solution.md
data/2.dailycode中阶/1.cpp/63.exercises/solution.md
+95
-4
data/2.dailycode中阶/1.cpp/64.exercises/solution.md
data/2.dailycode中阶/1.cpp/64.exercises/solution.md
+45
-4
data/2.dailycode中阶/1.cpp/65.exercises/solution.md
data/2.dailycode中阶/1.cpp/65.exercises/solution.md
+72
-4
data/2.dailycode中阶/1.cpp/66.exercises/solution.md
data/2.dailycode中阶/1.cpp/66.exercises/solution.md
+66
-4
data/2.dailycode中阶/1.cpp/67.exercises/solution.md
data/2.dailycode中阶/1.cpp/67.exercises/solution.md
+37
-4
data/2.dailycode中阶/1.cpp/68.exercises/solution.md
data/2.dailycode中阶/1.cpp/68.exercises/solution.md
+44
-4
data/2.dailycode中阶/1.cpp/69.exercises/solution.md
data/2.dailycode中阶/1.cpp/69.exercises/solution.md
+66
-4
data/2.dailycode中阶/1.cpp/70.exercises/solution.md
data/2.dailycode中阶/1.cpp/70.exercises/solution.md
+88
-4
data/2.dailycode中阶/1.cpp/71.exercises/solution.md
data/2.dailycode中阶/1.cpp/71.exercises/solution.md
+57
-4
data/2.dailycode中阶/1.cpp/72.exercises/solution.md
data/2.dailycode中阶/1.cpp/72.exercises/solution.md
+60
-4
data/2.dailycode中阶/1.cpp/73.exercises/solution.md
data/2.dailycode中阶/1.cpp/73.exercises/solution.md
+84
-4
data/2.dailycode中阶/1.cpp/74.exercises/solution.md
data/2.dailycode中阶/1.cpp/74.exercises/solution.md
+85
-4
data/2.dailycode中阶/1.cpp/75.exercises/solution.md
data/2.dailycode中阶/1.cpp/75.exercises/solution.md
+46
-4
data/2.dailycode中阶/1.cpp/76.exercises/solution.md
data/2.dailycode中阶/1.cpp/76.exercises/solution.md
+77
-4
data/2.dailycode中阶/1.cpp/77.exercises/solution.md
data/2.dailycode中阶/1.cpp/77.exercises/solution.md
+50
-4
data/2.dailycode中阶/1.cpp/78.exercises/solution.md
data/2.dailycode中阶/1.cpp/78.exercises/solution.md
+78
-4
data/2.dailycode中阶/1.cpp/79.exercises/solution.md
data/2.dailycode中阶/1.cpp/79.exercises/solution.md
+70
-4
data/2.dailycode中阶/1.cpp/80.exercises/solution.md
data/2.dailycode中阶/1.cpp/80.exercises/solution.md
+100
-4
data/2.dailycode中阶/1.cpp/81.exercises/solution.md
data/2.dailycode中阶/1.cpp/81.exercises/solution.md
+63
-4
data/2.dailycode中阶/1.cpp/82.exercises/solution.md
data/2.dailycode中阶/1.cpp/82.exercises/solution.md
+50
-4
data/2.dailycode中阶/1.cpp/83.exercises/solution.md
data/2.dailycode中阶/1.cpp/83.exercises/solution.md
+70
-46
data/2.dailycode中阶/1.cpp/84.exercises/solution.md
data/2.dailycode中阶/1.cpp/84.exercises/solution.md
+51
-4
data/2.dailycode中阶/1.cpp/85.exercises/solution.md
data/2.dailycode中阶/1.cpp/85.exercises/solution.md
+76
-4
data/2.dailycode中阶/1.cpp/86.exercises/solution.md
data/2.dailycode中阶/1.cpp/86.exercises/solution.md
+101
-4
data/2.dailycode中阶/1.cpp/87.exercises/solution.md
data/2.dailycode中阶/1.cpp/87.exercises/solution.md
+93
-4
未找到文件。
data/2.dailycode中阶/1.cpp/43.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,33 +2,58 @@
<p>给你 <code>n</code> 个非负整数 <code>a<sub>1</sub>,a<sub>2,</sub>...,a</code><sub><code>n</code>,</sub>每个数代表坐标中的一个点 <code>(i, a<sub>i</sub>)</code> 。在坐标内画 <code>n</code> 条垂直线,垂直线 <code>i</code> 的两个端点分别为 <code>(i, a<sub>i</sub>)</code> 和 <code>(i, 0)</code> 。找出其中的两条线,使得它们与 <code>x</code> 轴共同构成的容器可以容纳最多的水。</p><p><strong>说明:</strong>你不能倾斜容器。</p><p> </p><p><strong>示例 1:</strong></p><p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0011.Container%20With%20Most%20Water/images/question_11.jpg" style="height: 287px; width: 600px;" /></p><pre><strong>输入:</strong>[1,8,6,2,5,4,8,3,7]<strong><br />输出:</strong>49 <strong><br />解释:</strong>图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>height = [1,1]<strong><br />输出:</strong>1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>height = [4,3,2,1,4]<strong><br />输出:</strong>16</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>height = [1,2,1]<strong><br />输出:</strong>2</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>n = height.length</code></li> <li><code>2 <= n <= 3 * 10<sup>4</sup></code></li> <li><code>0 <= height[i] <= 3 * 10<sup>4</sup></code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
class
Solution
{
public:
int
maxArea
(
vector
<
int
>
&
height
)
{
int
ret
=
0
;
int
temp
;
for
(
int
i
=
0
,
j
=
height
.
size
()
-
1
;
i
<
j
;)
{
temp
=
min
(
height
[
i
],
height
[
j
])
*
(
j
-
i
);
______________________
}
return
ret
;
}
};
```
## template
```
cpp
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
#define MIN(a, b) (((a) > (b)) ? (b) : (a))
int
maxArea
(
int
*
height
,
int
heightSize
)
class
Solution
{
int
max
=
0
;
int
i
=
0
,
j
=
heightSize
-
1
;
int
a
;
while
(
i
<
j
)
{
a
=
MIN
(
height
[
i
],
height
[
j
])
*
(
j
-
i
);
max
=
MAX
(
max
,
a
);
if
(
height
[
i
]
>
height
[
j
])
--
j
;
else
++
i
;
}
return
max
;
}
public:
int
maxArea
(
vector
<
int
>
&
height
)
{
int
ret
=
0
;
int
temp
;
for
(
int
i
=
0
,
j
=
height
.
size
()
-
1
;
i
<
j
;)
{
temp
=
min
(
height
[
i
],
height
[
j
])
*
(
j
-
i
);
ret
=
ret
>
temp
?
ret
:
temp
;
if
(
height
[
i
]
>
height
[
j
])
j
--
;
else
i
++
;
}
return
ret
;
}
};
```
## 答案
```
cpp
ret
=
ret
>
temp
?
ret
:
temp
;
if
(
height
[
i
]
>
height
[
j
])
j
--
;
else
i
++
;
```
## 选项
...
...
@@ -36,17 +61,29 @@ int maxArea(int *height, int heightSize)
### A
```
cpp
ret
=
ret
>
temp
?
ret
:
temp
;
if
(
height
[
i
]
<
height
[
j
])
j
--
;
else
i
++
;
```
### B
```
cpp
ret
=
ret
>
temp
?
temp
:
ret
;
if
(
height
[
i
]
<
height
[
j
])
j
--
;
else
i
++
;
```
### C
```
cpp
ret
=
ret
>
temp
?
temp
:
ret
;
if
(
height
[
i
]
<
height
[
j
])
i
++
;
else
j
--
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/44.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,92 @@
<p>给定一个只包含数字的字符串,用以表示一个 IP 地址,返回所有可能从 <code>s</code> 获得的 <strong>有效 IP 地址 </strong>。你可以按任何顺序返回答案。</p><p><strong>有效 IP 地址</strong> 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 <code>0</code>),整数之间用 <code>'.'</code> 分隔。</p><p>例如:"0.1.2.201" 和 "192.168.1.1" 是 <strong>有效</strong> IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 <strong>无效</strong> IP 地址。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "25525511135"<strong><br />输出:</strong>["255.255.11.135","255.255.111.35"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "0000"<strong><br />输出:</strong>["0.0.0.0"]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "1111"<strong><br />输出:</strong>["1.1.1.1"]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "010010"<strong><br />输出:</strong>["0.10.0.10","0.100.1.0"]</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = "101023"<strong><br />输出:</strong>["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 3000</code></li> <li><code>s</code> 仅由数字组成</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static
bool
valid
(
char
*
ip
,
int
len
)
{
if
(
len
>
1
&&
ip
[
0
]
==
'0'
)
{
return
false
;
}
if
(
len
==
3
)
{
int
n
=
(
ip
[
0
]
-
'0'
)
*
100
+
(
ip
[
1
]
-
'0'
)
*
10
+
(
ip
[
2
]
-
'0'
);
if
(
n
>
255
)
{
return
false
;
}
}
return
true
;
}
#define WIDTH 4
static
void
dfs
(
char
*
s
,
int
start
,
char
*
stack
,
int
num
,
char
**
results
,
int
*
count
)
{
int
i
,
j
;
if
(
num
==
4
)
{
if
(
s
[
start
]
==
'\0'
)
{
results
[
*
count
]
=
malloc
(
3
*
4
+
3
+
1
);
char
*
p
=
results
[
*
count
];
for
(
j
=
0
;
j
<
num
;
j
++
)
{
char
*
q
=
stack
+
j
*
WIDTH
;
______________________
}
(
*
count
)
++
;
}
}
else
{
char
*
p
=
stack
+
num
*
WIDTH
;
char
*
q
=
p
;
for
(
i
=
start
;
s
[
i
]
!=
'\0'
&&
i
<
start
+
3
;
i
++
)
{
*
q
++
=
s
[
i
];
*
q
=
'\0'
;
if
(
!
valid
(
p
,
q
-
p
))
{
return
;
}
dfs
(
s
,
i
+
1
,
stack
,
num
+
1
,
results
,
count
);
if
(
num
+
1
<
4
)
{
memset
(
stack
+
(
num
+
1
)
*
WIDTH
,
0
,
WIDTH
);
}
}
}
}
static
char
**
restoreIpAddresses
(
char
*
s
,
int
*
returnSize
)
{
int
count
=
0
;
char
**
results
=
malloc
(
100
*
sizeof
(
char
*
));
char
addr
[
16
]
=
{
'\0'
};
dfs
(
s
,
0
,
addr
,
0
,
results
,
&
count
);
*
returnSize
=
count
;
return
results
;
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
2
)
{
fprintf
(
stderr
,
"Usage: ./test num
\n
"
);
exit
(
-
1
);
}
int
i
,
count
=
0
;
char
**
list
=
restoreIpAddresses
(
argv
[
1
],
&
count
);
for
(
i
=
0
;
i
<
count
;
i
++
)
{
printf
(
"%s
\n
"
,
list
[
i
]);
}
}
```
## template
```
cpp
...
...
@@ -97,7 +183,13 @@ int main(int argc, char **argv)
## 答案
```
cpp
while
((
*
p
++
=
*
q
++
)
!=
'\0'
)
{
}
if
(
j
!=
3
)
{
*
(
p
-
1
)
=
'.'
;
}
```
## 选项
...
...
@@ -105,17 +197,37 @@ int main(int argc, char **argv)
### A
```
cpp
while
((
*
p
++
=
*
q
++
)
!=
'\0'
)
{
*
p
++
;
}
if
(
j
!=
3
)
{
*
(
p
-
1
)
=
'.'
;
}
```
### B
```
cpp
while
((
*
p
++
=
*
q
++
)
!=
'\0'
)
{
*
p
++
;
}
if
(
j
!=
3
)
{
*
p
=
'.'
;
}
```
### C
```
cpp
while
((
*
p
++
=
*
q
++
)
!=
'\0'
)
{
}
if
(
j
!=
3
)
{
*
p
=
'.'
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/45.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -9,6 +9,40 @@
<p><strong>
示例
2:
</strong></p>
<pre><strong>
输入:
</strong>
0
<strong><br
/>
输出:
</strong>
[0]
<strong><br
/>
解释:
</strong>
我们定义格雷编码序列必须以 0 开头。给定编码总位数为
<em>
n
</em>
的格雷编码序列,其长度为 2
<sup>
n
</sup>
。当
<em>
n
</em>
= 0 时,长度为 2
<sup>
0
</sup>
= 1。因此,当
<em>
n
</em>
= 0 时,其格雷编码序列为 [0]。
</pre>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
int
*
grayCode
(
int
n
,
int
*
returnSize
)
{
if
(
n
<
0
)
{
return
NULL
;
}
int
i
,
count
=
1
<<
n
;
int
*
codes
=
malloc
(
count
*
sizeof
(
int
));
_________________
return
codes
;
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
2
)
{
fprintf
(
stderr
,
"Usage: ./test n
\n
"
);
exit
(
-
1
);
}
int
i
,
count
;
int
*
list
=
grayCode
(
atoi
(
argv
[
1
]),
&
count
);
for
(
i
=
0
;
i
<
count
;
i
++
)
{
printf
(
"%d "
,
list
[
i
]);
}
printf
(
"
\n
"
);
return
0
;
}
```
## template
```
cpp
...
...
@@ -50,7 +84,11 @@ int main(int argc, char **argv)
## 答案
```
cpp
for
(
i
=
0
;
i
<
count
;
i
++
)
{
codes
[
i
]
=
(
i
>>
1
)
^
i
;
}
*
returnSize
=
1
<<
n
;
```
## 选项
...
...
@@ -58,17 +96,29 @@ int main(int argc, char **argv)
### A
```
cpp
for
(
i
=
0
;
i
<
count
;
i
++
)
{
codes
[
i
]
=
(
i
>>
1
)
^
i
;
}
*
returnSize
=
1
<<
n
-
1
;
```
### B
```
cpp
for
(
i
=
0
;
i
<
count
;
i
++
)
{
codes
[
i
]
=
(
i
>>
2
)
^
i
;
}
*
returnSize
=
1
<<
n
;
```
### C
```
cpp
for
(
i
=
0
;
i
<
count
;
i
++
)
{
codes
[
i
]
=
(
i
>>
1
)
^
i
;
}
*
returnSize
=
1
>>
n
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/46.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,36 @@
<p>
实现获取
<strong>
下一个排列
</strong>
的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。
</p><p>
如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。
</p><p>
必须
<strong><a
href=
"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95"
target=
"_blank"
>
原地
</a></strong>
修改,只允许使用额外常数空间。
</p><p>
</p><p><strong>
示例 1:
</strong></p><pre><strong>
输入:
</strong>
nums = [1,2,3]
<strong><br
/>
输出:
</strong>
[1,3,2]
</pre><p><strong>
示例 2:
</strong></p><pre><strong>
输入:
</strong>
nums = [3,2,1]
<strong><br
/>
输出:
</strong>
[1,2,3]
</pre><p><strong>
示例 3:
</strong></p><pre><strong>
输入:
</strong>
nums = [1,1,5]
<strong><br
/>
输出:
</strong>
[1,5,1]
</pre><p><strong>
示例 4:
</strong></p><pre><strong>
输入:
</strong>
nums = [1]
<strong><br
/>
输出:
</strong>
[1]
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li><code>
1
<
=
nums.length
<=
100</
code
></li>
<li><code>
0
<
=
nums
[
i
]
<=
100</
code
></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
void
nextPermutation
(
vector
<
int
>
&
nums
)
{
if
(
nums
.
size
()
<
2
)
{
return
;
}
int
i
=
nums
.
size
()
-
2
;
while
(
i
>=
0
&&
nums
[
i
]
>=
nums
[
i
+
1
])
{
i
--
;
}
if
(
i
>=
0
)
{
int
j
=
nums
.
size
()
-
1
;
________________________
swap
(
nums
.
begin
()
+
i
,
nums
.
begin
()
+
j
);
}
reverse
(
nums
.
begin
()
+
i
+
1
,
nums
.
end
());
}
};
```
## template
```
cpp
...
...
@@ -38,7 +68,10 @@ public:
## 答案
```
cpp
while
(
j
>=
0
&&
nums
[
j
]
>=
nums
[
i
])
{
j
--
;
}
```
## 选项
...
...
@@ -46,17 +79,26 @@ public:
### A
```
cpp
while
(
nums
[
j
]
>=
nums
[
i
])
{
j
--
;
}
```
### B
```
cpp
while
(
j
>=
0
)
{
j
--
;
}
```
### C
```
cpp
while
(
nums
[
j
]
<
nums
[
i
])
{
j
--
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/47.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,41 @@
<p>
给你一个整数数组
<code>
nums
</code>
,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
</p><p>
解集
<strong>
不能
</strong>
包含重复的子集。返回的解集中,子集可以按
<strong>
任意顺序
</strong>
排列。
</p><div
class=
"original__bRMd"
><div><p>
</p><p><strong>
示例 1:
</strong></p><pre><strong>
输入:
</strong>
nums = [1,2,2]
<strong><br
/>
输出:
</strong>
[[],[1],[1,2],[1,2,2],[2],[2,2]]
</pre><p><strong>
示例 2:
</strong></p><pre><strong>
输入:
</strong>
nums = [0]
<strong><br
/>
输出:
</strong>
[[],[0]]
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li><code>
1
<
=
nums.length
<=
10</
code
></li>
<li><code>
-10
<
=
nums
[
i
]
<=
10</
code
></li></ul></div></div>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
vector
<
vector
<
int
>>
subsetsWithDup
(
vector
<
int
>
&
nums
)
{
vector
<
vector
<
int
>>
res
;
sort
(
nums
.
begin
(),
nums
.
end
());
dfs
(
nums
,
0
,
res
);
return
res
;
}
private:
vector
<
int
>
stack
;
void
dfs
(
vector
<
int
>
&
nums
,
int
start
,
vector
<
vector
<
int
>>
&
res
)
{
res
.
push_back
(
stack
);
int
last
=
INT_MIN
;
for
(
int
i
=
start
;
i
<
nums
.
size
();
i
++
)
{
if
(
last
!=
nums
[
i
])
{
stack
.
push_back
(
nums
[
i
]);
____________________
stack
.
pop_back
();
}
last
=
nums
[
i
];
}
}
};
```
## template
```
cpp
...
...
@@ -40,7 +75,7 @@ private:
## 答案
```
cpp
dfs
(
nums
,
i
+
1
,
res
);
```
## 选项
...
...
@@ -48,17 +83,17 @@ private:
### A
```
cpp
dfs
(
nums
,
i
-
1
,
res
);
```
### B
```
cpp
dfs
(
nums
,
i
,
res
);
```
### C
```
cpp
dfs
(
nums
,
1
,
res
);
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/48.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,99 @@
<p>
给你一个字符串
<code>
path
</code>
,表示指向某一文件或目录的 Unix 风格
<strong>
绝对路径
</strong>
(以
<code>
'/'
</code>
开头),请你将其转化为更加简洁的规范路径。
</p><p
class=
"MachineTrans-lang-zh-CN"
>
在 Unix 风格的文件系统中,一个点(
<code>
.
</code>
)表示当前目录本身;此外,两个点 (
<code>
..
</code>
) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,
<code>
'//'
</code>
)都被视为单个斜杠
<code>
'/'
</code>
。 对于此问题,任何其他格式的点(例如,
<code>
'...'
</code>
)均被视为文件/目录名称。
</p><p>
请注意,返回的
<strong>
规范路径
</strong>
必须遵循下述格式:
</p><ul>
<li>
始终以斜杠
<code>
'/'
</code>
开头。
</li>
<li>
两个目录名之间必须只有一个斜杠
<code>
'/'
</code>
。
</li>
<li>
最后一个目录名(如果存在)
<strong>
不能
</strong>
以
<code>
'/'
</code>
结尾。
</li>
<li>
此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含
<code>
'.'
</code>
或
<code>
'..'
</code>
)。
</li></ul><p>
返回简化后得到的
<strong>
规范路径
</strong>
。
</p><p>
</p><p><strong>
示例 1:
</strong></p><pre><strong>
输入:
</strong>
path = "/home/"
<strong><br
/>
输出:
</strong>
"/home"
<strong><br
/>
解释:
</strong>
注意,最后一个目录名后面没有斜杠。
</pre><p><strong>
示例 2:
</strong></p><pre><strong>
输入:
</strong>
path = "/../"
<strong><br
/>
输出:
</strong>
"/"
<strong><br
/>
解释:
</strong>
从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。
</pre><p><strong>
示例 3:
</strong></p><pre><strong>
输入:
</strong>
path = "/home//foo/"
<strong><br
/>
输出:
</strong>
"/home/foo"
<strong><br
/>
解释:
</strong>
在规范路径中,多个连续斜杠需要用一个斜杠替换。
</pre><p><strong>
示例 4:
</strong></p><pre><strong>
输入:
</strong>
path = "/a/./b/../../c/"
<strong><br
/>
输出:
</strong>
"/c"
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li><code>
1
<
=
path.length
<=
3000</
code
></li>
<li><code>
path
</code>
由英文字母,数字,
<code>
'.'
</code>
,
<code>
'/'
</code>
或
<code>
'_'
</code>
组成。
</li>
<li><code>
path
</code>
是一个有效的 Unix 风格绝对路径。
</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static
char
*
simplifyPath
(
char
*
path
)
{
int
len
=
strlen
(
path
);
if
(
len
==
0
)
{
return
path
;
}
char
*
p
=
path
;
int
*
indexes
=
malloc
(
len
*
sizeof
(
int
));
int
depth
=
0
;
int
name_start
=
1
;
while
(
*
p
!=
'\0'
)
{
if
(
*
p
==
'/'
)
{
if
(
p
>
path
&&
*
(
p
-
1
)
!=
'/'
&&
*
(
p
-
1
)
!=
'.'
)
{
name_start
=
1
;
}
}
else
if
(
*
p
==
'.'
)
{
if
(
*
(
p
+
1
)
==
'\0'
||
*
(
p
+
1
)
==
'/'
)
{
p
+=
1
;
}
else
if
(
*
(
p
+
1
)
==
'.'
&&
(
*
(
p
+
2
)
==
'\0'
||
*
(
p
+
2
)
==
'/'
))
{
if
(
depth
>
0
)
{
depth
--
;
name_start
=
1
;
}
p
+=
2
;
}
else
{
indexes
[
depth
++
]
=
p
-
path
;
while
(
*
p
!=
'/'
&&
*
p
!=
'\0'
)
{
p
++
;
}
}
if
(
*
p
==
'\0'
)
{
break
;
}
}
else
{
if
(
name_start
&&
depth
>=
0
)
{
indexes
[
depth
++
]
=
p
-
path
;
name_start
=
0
;
}
}
p
++
;
}
int
i
;
char
*
result
=
malloc
(
len
+
1
);
char
*
q
=
result
;
if
(
depth
<=
0
)
{
*
q
++
=
'/'
;
}
else
{
for
(
i
=
0
;
i
<
depth
;
i
++
)
{
____________________
}
}
*
q
=
'\0'
;
return
result
;
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
2
)
{
fprintf
(
stderr
,
"Usage: ./test path
\n
"
);
exit
(
-
1
);
}
printf
(
"%s
\n
"
,
simplifyPath
(
argv
[
1
]));
return
0
;
}
```
## template
```
cpp
...
...
@@ -103,7 +196,12 @@ int main(int argc, char **argv)
## 答案
```
cpp
p
=
path
+
indexes
[
i
];
*
q
++
=
'/'
;
while
(
*
p
!=
'/'
)
{
*
q
++
=
*
p
++
;
}
```
## 选项
...
...
@@ -111,17 +209,32 @@ int main(int argc, char **argv)
### A
```
cpp
p
=
path
+
indexes
[
i
++
];
*
q
++
=
'/'
;
while
(
*
p
==
'/'
)
{
*
q
++
=
*
p
++
;
}
```
### B
```
cpp
p
=
path
+
indexes
[
i
++
];
*
q
++
=
'/'
;
while
(
*
p
!=
'/'
)
{
*
q
++
=
*
p
++
;
}
```
### C
```
cpp
p
=
path
+
indexes
[
i
];
*
q
++
=
'/'
;
while
(
*
p
==
'/'
)
{
*
q
++
=
*
p
++
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/49.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,50 @@
<p>给你两个 <strong>非空</strong> 的链表,表示两个非负的整数。它们每位数字都是按照 <strong>逆序</strong> 的方式存储的,并且每个节点只能存储 <strong>一位</strong> 数字。</p><p>请你将两个数相加,并以相同形式返回一个表示和的链表。</p><p>你可以假设除了数字 0 之外,这两个数都不会以 0 开头。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0002.Add%20Two%20Numbers/images/addtwonumber1.jpg" style="width: 483px; height: 342px;" /><pre><strong>输入:</strong>l1 = [2,4,3], l2 = [5,6,4]<strong><br />输出:</strong>[7,0,8]<strong><br />解释:</strong>342 + 465 = 807.</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>l1 = [0], l2 = [0]<strong><br />输出:</strong>[0]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]<strong><br />输出:</strong>[8,9,9,9,0,0,0,1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>每个链表中的节点数在范围 <code>[1, 100]</code> 内</li> <li><code>0 <= Node.val <= 9</code></li> <li>题目数据保证列表表示的数字不含前导零</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
struct
ListNode
{
int
val
;
struct
ListNode
*
next
;
};
struct
ListNode
*
addTwoNumbers
(
struct
ListNode
*
l1
,
struct
ListNode
*
l2
)
{
struct
ListNode
*
pp
=
NULL
,
*
p
=
l1
;
struct
ListNode
*
qp
=
NULL
,
*
q
=
l2
;
int
carry
=
0
;
while
(
p
!=
NULL
&&
q
!=
NULL
)
{
p
->
val
+=
q
->
val
+
carry
;
carry
=
0
;
if
(
p
->
val
>=
10
)
{
carry
=
1
;
p
->
val
-=
10
;
}
pp
=
p
;
p
=
p
->
next
;
qp
=
q
;
q
=
q
->
next
;
}
if
(
q
)
{
pp
->
next
=
p
=
q
;
qp
->
next
=
NULL
;
}
____________________
if
(
carry
)
{
struct
ListNode
*
n
=
(
struct
ListNode
*
)
malloc
(
sizeof
(
struct
ListNode
));
n
->
val
=
1
;
n
->
next
=
NULL
;
pp
->
next
=
n
;
}
return
l1
;
}
```
## template
```
cpp
...
...
@@ -60,7 +104,18 @@ struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
## 答案
```
cpp
while
(
carry
&&
p
)
{
p
->
val
+=
carry
;
carry
=
0
;
if
(
p
->
val
>=
10
)
{
carry
=
1
;
p
->
val
-=
10
;
}
pp
=
p
;
p
=
p
->
next
;
}
```
## 选项
...
...
@@ -68,17 +123,50 @@ struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
### A
```
cpp
while
(
carry
&&
p
)
{
p
->
val
+=
carry
;
carry
=
0
;
if
(
p
->
val
>=
10
)
{
carry
=
1
;
p
->
val
-=
10
;
}
p
=
p
->
next
;
pp
=
p
;
}
```
### B
```
cpp
while
(
carry
&&
p
)
{
p
->
val
+=
carry
;
carry
=
0
;
if
(
p
->
val
>=
10
)
{
carry
+=
1
;
p
->
val
-=
10
;
}
p
=
p
->
next
;
pp
=
p
;
}
```
### C
```
cpp
while
(
carry
&&
p
)
{
p
->
val
+=
carry
;
carry
=
0
;
if
(
p
->
val
>=
10
)
{
carry
+=
1
;
p
->
val
-=
10
;
}
p
=
p
->
next
;
pp
=
p
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/50.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,83 @@
<p>给你一个链表的头节点 <code>head</code> 和一个特定值<em> </em><code>x</code> ,请你对链表进行分隔,使得所有 <strong>小于</strong> <code>x</code> 的节点都出现在 <strong>大于或等于</strong> <code>x</code> 的节点之前。</p><p>你应当 <strong>保留</strong> 两个分区中每个节点的初始相对位置。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0086.Partition%20List/images/partition.jpg" style="width: 662px; height: 222px;" /><pre><strong>输入:</strong>head = [1,4,3,2,5,2], x = 3<strong><br />输出</strong>:[1,2,2,4,3,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [2,1], x = 2<strong><br />输出</strong>:[1,2]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点的数目在范围 <code>[0, 200]</code> 内</li> <li><code>-100 <= Node.val <= 100</code></li> <li><code>-200 <= x <= 200</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
struct
ListNode
{
int
val
;
struct
ListNode
*
next
;
};
struct
ListNode
*
partition
(
struct
ListNode
*
head
,
int
x
)
{
struct
ListNode
dummy
;
struct
ListNode
*
prev1
=
&
dummy
,
*
pivot
;
dummy
.
next
=
head
;
for
(
pivot
=
head
;
pivot
!=
NULL
;
pivot
=
pivot
->
next
)
{
if
(
pivot
->
val
>=
x
)
{
break
;
}
prev1
=
pivot
;
}
struct
ListNode
*
p
=
pivot
->
next
;
struct
ListNode
*
prev2
=
pivot
;
while
(
p
!=
NULL
)
{
if
(
p
->
val
<
x
)
{
__________________
}
else
{
prev2
=
p
;
p
=
p
->
next
;
}
}
return
dummy
.
next
;
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
<
2
)
{
fprintf
(
stderr
,
"Usage: ./test target n1 n2 n3...
\n
"
);
exit
(
-
1
);
}
int
i
,
target
=
atoi
(
argv
[
1
]);
struct
ListNode
*
head
=
NULL
;
struct
ListNode
*
prev
=
NULL
;
struct
ListNode
*
p
;
for
(
i
=
0
;
i
<
argc
-
2
;
i
++
)
{
p
=
malloc
(
sizeof
(
*
p
));
p
->
val
=
atoi
(
argv
[
i
+
2
]);
p
->
next
=
NULL
;
if
(
head
==
NULL
)
{
head
=
p
;
prev
=
head
;
}
else
{
prev
->
next
=
p
;
prev
=
p
;
}
}
p
=
partition
(
head
,
target
);
while
(
p
!=
NULL
)
{
printf
(
"%d "
,
p
->
val
);
p
=
p
->
next
;
}
printf
(
"
\n
"
);
return
0
;
}
```
## template
```
cpp
...
...
@@ -86,7 +163,11 @@ int main(int argc, char **argv)
## 答案
```
cpp
prev2
->
next
=
p
->
next
;
p
->
next
=
prev1
->
next
;
prev1
->
next
=
p
;
prev1
=
p
;
p
=
prev2
->
next
;
```
## 选项
...
...
@@ -94,17 +175,29 @@ int main(int argc, char **argv)
### A
```
cpp
p
->
next
=
prev1
->
next
;
prev1
->
next
=
p
;
prev2
->
next
=
p
->
next
;
prev1
=
p
;
p
=
prev2
->
next
;
```
### B
```
cpp
p
->
next
=
prev1
->
next
;
prev1
->
next
=
p
;
prev2
->
next
=
p
->
next
;
p
=
prev2
->
next
;
prev1
=
p
;
```
### C
```
cpp
p
->
next
=
prev1
->
next
;
prev2
->
next
=
p
->
next
;
prev1
->
next
=
p
;
p
=
prev2
->
next
;
prev1
=
p
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/51.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -70,6 +70,46 @@ M 1000</pre>
</ul>
</div>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
struct
rmap
{
char
*
r
;
int
v
;
}
units
[]
=
{
{
"M"
,
1000
},
{
"CM"
,
900
},
{
"D"
,
500
},
{
"CD"
,
400
},
{
"C"
,
100
},
{
"XC"
,
90
},
{
"L"
,
50
},
{
"XL"
,
40
},
{
"X"
,
10
},
{
"IX"
,
9
},
{
"V"
,
5
},
{
"IV"
,
4
},
{
"I"
,
1
}};
#include <string.h>
char
result
[
64
];
char
*
intToRoman
(
int
num
)
{
result
[
0
]
=
0
;
int
ri
=
0
;
int
i
=
0
;
while
(
num
)
{
______________
else
{
i
++
;
}
}
return
result
;
}
```
## template
```
cpp
...
...
@@ -117,7 +157,11 @@ char *intToRoman(int num)
## 答案
```
cpp
if
(
num
>=
units
[
i
].
v
)
{
strcat
(
result
,
units
[
i
].
r
);
num
-=
units
[
i
].
v
;
}
```
## 选项
...
...
@@ -125,17 +169,29 @@ char *intToRoman(int num)
### A
```
cpp
if
(
num
>=
units
[
i
].
v
)
{
strcat
(
result
,
units
[
i
].
r
);
num
=
units
[
i
].
v
;
}
```
### B
```
cpp
if
(
num
<=
units
[
i
].
v
)
{
strcat
(
result
,
units
[
i
].
r
);
num
-=
units
[
i
].
v
;
}
```
### C
```
cpp
if
(
num
<=
units
[
i
].
v
)
{
strcat
(
result
,
units
[
i
].
r
);
num
=
units
[
i
].
v
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/52.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,49 @@
<p>
给定一个可包含重复数字的序列
<code>
nums
</code>
,
<strong>
按任意顺序
</strong>
返回所有不重复的全排列。
</p><p>
</p><p><strong>
示例 1:
</strong></p><pre><strong>
输入:
</strong>
nums = [1,1,2]
<strong><br
/>
输出:
</strong>
[[1,1,2], [1,2,1], [2,1,1]]
</pre><p><strong>
示例 2:
</strong></p><pre><strong>
输入:
</strong>
nums = [1,2,3]
<strong><br
/>
输出:
</strong>
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li><code>
1
<
=
nums.length
<=
8</
code
></li>
<li><code>
-10
<
=
nums
[
i
]
<=
10</
code
></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
vector
<
vector
<
int
>>
permuteUnique
(
vector
<
int
>
&
nums
)
{
vector
<
vector
<
int
>>
res
;
vector
<
bool
>
used
(
nums
.
size
());
sort
(
nums
.
begin
(),
nums
.
end
());
dfs
(
nums
,
used
,
res
);
return
res
;
}
private:
vector
<
int
>
stack
;
void
dfs
(
vector
<
int
>
&
nums
,
vector
<
bool
>
&
used
,
vector
<
vector
<
int
>>
&
res
)
{
if
(
stack
.
size
()
==
nums
.
size
())
{
res
.
push_back
(
stack
);
}
else
{
for
(
int
i
=
0
;
i
<
nums
.
size
();
i
++
)
{
if
(
!
used
[
i
])
{
_____________________
stack
.
push_back
(
nums
[
i
]);
used
[
i
]
=
true
;
dfs
(
nums
,
used
,
res
);
stack
.
pop_back
();
used
[
i
]
=
false
;
}
}
}
}
};
```
## template
```
cpp
...
...
@@ -51,7 +94,10 @@ private:
## 答案
```
cpp
if
(
i
>
0
&&
!
used
[
i
-
1
]
&&
nums
[
i
-
1
]
==
nums
[
i
])
{
continue
;
}
```
## 选项
...
...
@@ -59,17 +105,26 @@ private:
### A
```
cpp
if
(
i
>
0
&&
!
used
[
i
-
1
]
&&
nums
[
i
-
1
]
==
nums
[
i
])
{
break
;
}
```
### B
```
cpp
if
(
nums
[
i
-
1
]
==
nums
[
i
])
{
continue
;
}
```
### C
```
cpp
if
(
nums
[
i
-
1
]
==
nums
[
i
])
{
break
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/53.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -3,6 +3,23 @@
<p>
给定一个字符串,请你找出其中不含有重复字符的
<strong>
最长子串
</strong>
的长度。
</p><p>
</p><p><strong>
示例 1:
</strong></p><pre><strong>
输入:
</strong>
s = "abcabcbb"
<strong><br
/>
输出:
</strong>
3
<strong><br
/>
解释:
</strong>
因为无重复字符的最长子串是 "abc",所以其长度为 3。
</pre><p><strong>
示例 2:
</strong></p><pre><strong>
输入:
</strong>
s = "bbbbb"
<strong><br
/>
输出:
</strong>
1
<strong><br
/>
解释:
</strong>
因为无重复字符的最长子串是 "b",所以其长度为 1。
</pre><p><strong>
示例 3:
</strong></p><pre><strong>
输入:
</strong>
s = "pwwkew"
<strong><br
/>
输出:
</strong>
3
<strong><br
/>
解释:
</strong>
因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是
<strong>
子串
</strong>
的长度,"pwke" 是一个
<em>
子序列,
</em>
不是子串。
</pre><p><strong>
示例 4:
</strong></p><pre><strong>
输入:
</strong>
s = ""
<strong><br
/>
输出:
</strong>
0
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li><code>
0
<
=
s.length
<=
5
*
10<
sup
>
4
</sup></code></li>
<li><code>
s
</code>
由英文字母、数字、符号和空格组成
</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
int
hset
[
128
];
int
lengthOfLongestSubstring
(
char
*
s
)
{
int
i
=
0
,
j
=
0
;
int
m
=
0
;
memset
(
hset
,
0
,
sizeof
hset
);
for
(;
s
[
j
];
j
++
)
{
___________________
}
return
m
;
}
```
## template
```
cpp
...
...
@@ -25,7 +42,9 @@ int lengthOfLongestSubstring(char *s)
## 答案
```
cpp
i
=
hset
[
s
[
j
]]
>
i
?
hset
[
s
[
j
]]
:
i
;
m
=
m
>
j
-
i
+
1
?
m
:
j
-
i
+
1
;
hset
[
s
[
j
]]
=
j
+
1
;
```
## 选项
...
...
@@ -33,17 +52,23 @@ int lengthOfLongestSubstring(char *s)
### A
```
cpp
i
=
hset
[
s
[
j
]]
>
i
?
hset
[
s
[
j
]]
:
i
;
m
=
m
>
j
-
i
+
1
?
m
:
j
-
i
+
1
;
hset
[
s
[
j
]]
=
j
-
1
;
```
### B
```
cpp
i
=
hset
[
s
[
j
]]
>
i
?
hset
[
s
[
j
]]
:
i
;
m
=
m
>
j
-
i
+
1
?
j
-
i
+
1
:
m
;
hset
[
s
[
j
]]
=
j
-
1
;
```
### C
```
cpp
i
=
hset
[
s
[
j
]]
>
i
?
hset
[
s
[
j
]]
:
i
;
m
=
m
>
j
-
i
+
1
?
j
-
i
+
1
:
m
;
hset
[
s
[
j
]]
=
j
+
1
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/54.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -28,6 +28,42 @@
<li><code>
s
</code>
只包含数字,并且可能包含前导零。
</li>
</ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static
int
numDecodings
(
char
*
s
)
{
int
len
=
strlen
(
s
);
if
(
len
==
0
)
{
return
0
;
}
int
a
=
1
;
int
b
=
s
[
0
]
==
'0'
?
0
:
a
;
int
c
=
b
;
for
(
int
i
=
2
;
i
<=
len
;
i
++
)
{
_________________
a
=
b
;
b
=
c
;
}
return
c
;
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
2
)
{
fprintf
(
stderr
,
"Usage: ./test number
\n
"
);
exit
(
-
1
);
}
printf
(
"%d
\n
"
,
numDecodings
(
argv
[
1
]));
return
0
;
}
```
## template
```
cpp
...
...
@@ -72,7 +108,12 @@ int main(int argc, char **argv)
## 答案
```
cpp
c
=
s
[
i
-
1
]
==
'0'
?
0
:
b
;
int
num
=
(
s
[
i
-
2
]
-
'0'
)
*
10
+
(
s
[
i
-
1
]
-
'0'
);
if
(
num
>=
10
&&
num
<=
26
)
{
c
+=
a
;
}
```
## 选项
...
...
@@ -80,17 +121,32 @@ int main(int argc, char **argv)
### A
```
cpp
c
=
s
[
i
-
1
]
==
'0'
?
b
:
0
;
int
num
=
(
s
[
i
-
2
]
-
'0'
)
*
10
+
(
s
[
i
-
1
]
-
'0'
);
if
(
num
>=
10
&&
num
<=
26
)
{
c
+=
a
;
}
```
### B
```
cpp
c
=
s
[
i
-
1
]
==
'0'
?
b
:
0
;
int
num
=
(
s
[
i
-
2
]
-
'0'
)
+
(
s
[
i
-
1
]
-
'0'
);
if
(
num
>=
10
&&
num
<=
26
)
{
c
+=
a
;
}
```
### C
```
cpp
c
=
s
[
i
-
1
]
==
'0'
?
0
:
b
;
int
num
=
(
s
[
i
-
2
]
-
'0'
)
+
(
s
[
i
-
1
]
-
'0'
)
*
10
;
if
(
num
>=
10
&&
num
<=
26
)
{
c
+=
a
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/55.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,51 @@
<p>
给定两个整数,被除数
<code>
dividend
</code>
和除数
<code>
divisor
</code>
。将两数相除,要求不使用乘法、除法和 mod 运算符。
</p><p>
返回被除数
<code>
dividend
</code>
除以除数
<code>
divisor
</code>
得到的商。
</p><p>
整数除法的结果应当截去(
<code>
truncate
</code>
)其小数部分,例如:
<code>
truncate(8.345) = 8
</code>
以及
<code>
truncate(-2.7335) = -2
</code></p><p>
</p><p><strong>
示例
1:
</strong></p><pre><strong>
输入:
</strong>
dividend = 10, divisor = 3
<strong><br
/>
输出:
</strong>
3
<strong><br
/>
解释:
</strong>
10/3 = truncate(3.33333..) = truncate(3) = 3
</pre><p><strong>
示例
2:
</strong></p><pre><strong>
输入:
</strong>
dividend = 7, divisor = -3
<strong><br
/>
输出:
</strong>
-2
<strong><br
/>
解释:
</strong>
7/-3 = truncate(-2.33333..) = -2
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li>
被除数和除数均为 32 位有符号整数。
</li>
<li>
除数不为
0。
</li>
<li>
假设我们的环境只能存储 32 位有符号整数,其数值范围是 [
−
2
<sup>
31
</sup>
,
2
<sup>
31
</sup>
−
1]。本题中,如果除法结果溢出,则返回 2
<sup>
31
</sup>
−
1。
</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
int
divide
(
int
dividend
,
int
divisor
)
{
int
signal
=
1
;
unsigned
int
dvd
=
dividend
;
if
(
dividend
<
0
)
{
signal
*=
-
1
;
dvd
=
~
dvd
+
1
;
}
unsigned
int
dvs
=
divisor
;
if
(
divisor
<
0
)
{
signal
*=
-
1
;
dvs
=
~
dvs
+
1
;
}
int
shift
=
0
;
while
(
dvd
>
dvs
<<
shift
)
{
shift
++
;
}
unsigned
int
res
=
0
;
while
(
dvd
>=
dvs
)
{
________________
}
if
(
signal
==
1
&&
res
>=
INT_MAX
)
{
return
INT_MAX
;
}
else
{
return
res
*
signal
;
}
}
};
```
## template
```
cpp
...
...
@@ -55,7 +100,12 @@ public:
## 答案
```
cpp
while
(
dvd
<
dvs
<<
shift
)
{
shift
--
;
}
res
|=
(
unsigned
int
)
1
<<
shift
;
dvd
-=
dvs
<<
shift
;
```
## 选项
...
...
@@ -63,17 +113,32 @@ public:
### A
```
cpp
while
(
dvd
<
dvs
<<
shift
)
{
shift
--
;
}
res
&=
(
unsigned
int
)
1
<<
shift
;
dvd
-=
dvs
<<
shift
;
```
### B
```
cpp
while
(
dvd
<
dvs
<<
shift
)
{
shift
--
;
}
res
&=
(
unsigned
int
)
1
>>
shift
;
dvd
-=
dvs
<<
shift
;
```
### C
```
cpp
while
(
dvd
<
dvs
<<
shift
)
{
shift
--
;
}
res
|=
(
unsigned
int
)
1
>>
shift
;
dvd
-=
dvs
<<
shift
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/56.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,45 @@
<p>给你一个 <code>m</code> 行 <code>n</code> 列的矩阵 <code>matrix</code> ,请按照 <strong>顺时针螺旋顺序</strong> ,返回矩阵中的所有元素。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0054.Spiral%20Matrix/images/spiral1.jpg" style="width: 242px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<strong><br />输出:</strong>[1,2,3,6,9,8,7,4,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0054.Spiral%20Matrix/images/spiral.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]<strong><br />输出:</strong>[1,2,3,4,8,12,11,10,9,5,6,7]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 <= m, n <= 10</code></li> <li><code>-100 <= matrix[i][j] <= 100</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
vector
<
int
>
spiralOrder
(
vector
<
vector
<
int
>>
&
matrix
)
{
vector
<
int
>
ans
;
if
(
matrix
.
size
()
==
0
)
return
ans
;
int
cir
=
0
;
int
row
=
matrix
.
size
();
int
col
=
matrix
[
0
].
size
();
int
max_cir
=
int
(
min
(
matrix
.
size
(),
matrix
[
0
].
size
())
+
1
)
/
2
;
for
(;
cir
<
max_cir
;
cir
++
)
{
for
(
int
i
=
cir
;
i
<
col
-
cir
;
i
++
)
{
ans
.
push_back
(
matrix
[
cir
][
i
]);
}
for
(
int
i
=
cir
+
1
;
i
<
row
-
cir
;
i
++
)
{
ans
.
push_back
(
matrix
[
i
][
col
-
1
-
cir
]);
}
__________________
for
(
int
i
=
row
-
2
-
cir
;
i
>
cir
&&
(
col
-
1
-
cir
!=
cir
);
i
--
)
{
ans
.
push_back
(
matrix
[
i
][
cir
]);
}
}
return
ans
;
}
};
```
## template
```
cpp
...
...
@@ -10,61 +49,47 @@ using namespace std;
class
Solution
{
public:
vector
<
int
>
spiralOrder
(
vector
<
vector
<
int
>>
&
matrix
)
{
vector
<
int
>
res
;
int
hor_top
=
0
;
int
hor_bottom
=
matrix
.
size
()
-
1
;
int
ver_left
=
0
;
int
ver_right
=
matrix
[
0
].
size
()
-
1
;
int
direction
=
0
;
while
(
hor_top
<=
hor_bottom
&&
ver_left
<=
ver_right
)
{
switch
(
direction
)
{
case
0
:
for
(
int
i
=
ver_left
;
i
<=
ver_right
;
i
++
)
{
res
.
push_back
(
matrix
[
hor_top
][
i
]);
}
hor_top
++
;
break
;
case
1
:
for
(
int
i
=
hor_top
;
i
<=
hor_bottom
;
i
++
)
{
res
.
push_back
(
matrix
[
i
][
ver_right
]);
}
ver_right
--
;
break
;
case
2
:
for
(
int
i
=
ver_right
;
i
>=
ver_left
;
i
--
)
{
res
.
push_back
(
matrix
[
hor_bottom
][
i
]);
}
hor_bottom
--
;
break
;
case
3
:
for
(
int
i
=
hor_bottom
;
i
>=
hor_top
;
i
--
)
{
res
.
push_back
(
matrix
[
i
][
ver_left
]);
}
ver_left
++
;
break
;
default:
break
;
}
direction
++
;
direction
%=
4
;
}
return
res
;
}
vector
<
int
>
spiralOrder
(
vector
<
vector
<
int
>>
&
matrix
)
{
vector
<
int
>
ans
;
if
(
matrix
.
size
()
==
0
)
return
ans
;
int
cir
=
0
;
int
row
=
matrix
.
size
();
int
col
=
matrix
[
0
].
size
();
int
max_cir
=
int
(
min
(
matrix
.
size
(),
matrix
[
0
].
size
())
+
1
)
/
2
;
for
(;
cir
<
max_cir
;
cir
++
)
{
for
(
int
i
=
cir
;
i
<
col
-
cir
;
i
++
)
{
ans
.
push_back
(
matrix
[
cir
][
i
]);
}
for
(
int
i
=
cir
+
1
;
i
<
row
-
cir
;
i
++
)
{
ans
.
push_back
(
matrix
[
i
][
col
-
1
-
cir
]);
}
for
(
int
i
=
col
-
2
-
cir
;
i
>=
cir
&&
(
row
-
1
-
cir
!=
cir
);
i
--
)
{
ans
.
push_back
(
matrix
[
row
-
cir
-
1
][
i
]);
}
for
(
int
i
=
row
-
2
-
cir
;
i
>
cir
&&
(
col
-
1
-
cir
!=
cir
);
i
--
)
{
ans
.
push_back
(
matrix
[
i
][
cir
]);
}
}
return
ans
;
}
};
```
## 答案
```
cpp
for
(
int
i
=
col
-
2
-
cir
;
i
>=
cir
&&
(
row
-
1
-
cir
!=
cir
);
i
--
)
{
ans
.
push_back
(
matrix
[
row
-
cir
-
1
][
i
]);
}
```
## 选项
...
...
@@ -72,17 +97,26 @@ public:
### A
```
cpp
for
(
int
i
=
col
-
2
-
cir
;
i
>=
cir
&&
(
row
-
1
-
cir
!=
cir
);
i
--
)
{
ans
.
push_back
(
matrix
[
row
-
cir
+
1
][
i
]);
}
```
### B
```
cpp
for
(
int
i
=
col
-
2
-
cir
;
i
>=
cir
&&
(
row
-
1
-
cir
!=
cir
);
i
--
)
{
ans
.
push_back
(
matrix
[
row
-
cir
][
i
]);
}
```
### C
```
cpp
for
(
int
i
=
col
-
2
-
cir
;
row
-
1
-
cir
!=
cir
;
i
--
)
{
ans
.
push_back
(
matrix
[
row
-
cir
+
1
][
i
]);
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/57.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,43 @@
<p>
给定一个
<strong>
没有重复
</strong>
数字的序列,返回其所有可能的全排列。
</p><p><strong>
示例:
</strong></p><pre><strong>
输入:
</strong>
[1,2,3]
<strong><br
/>
输出:
</strong>
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
</pre>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
vector
<
vector
<
int
>>
permute
(
vector
<
int
>
&
nums
)
{
vector
<
vector
<
int
>>
res
;
vector
<
bool
>
used
(
nums
.
size
());
dfs
(
nums
,
used
,
res
);
return
res
;
}
private:
vector
<
int
>
stack
;
void
dfs
(
vector
<
int
>
&
nums
,
vector
<
bool
>
&
used
,
vector
<
vector
<
int
>>
&
res
)
{
if
(
stack
.
size
()
==
nums
.
size
())
{
res
.
push_back
(
stack
);
}
else
{
for
(
int
i
=
0
;
i
<
nums
.
size
();
i
++
)
{
if
(
!
used
[
i
])
{
_______________
}
}
}
}
};
```
## template
```
cpp
...
...
@@ -46,7 +83,11 @@ private:
## 答案
```
cpp
used
[
i
]
=
true
;
stack
.
push_back
(
nums
[
i
]);
dfs
(
nums
,
used
,
res
);
stack
.
pop_back
();
used
[
i
]
=
false
;
```
## 选项
...
...
@@ -54,17 +95,26 @@ private:
### A
```
cpp
used
[
i
]
=
false
;
stack
.
push_back
(
nums
[
i
]);
dfs
(
nums
,
used
,
res
);
stack
.
pop_back
();
used
[
i
]
=
true
;
```
### B
```
cpp
stack
.
push_back
(
nums
[
i
]);
dfs
(
nums
,
used
,
res
);
stack
.
pop_back
();
used
[
i
]
=
true
;
```
### C
```
cpp
stack
.
push_back
(
nums
[
i
]);
dfs
(
nums
,
used
,
res
);
stack
.
pop_back
();
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/58.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -27,6 +27,46 @@
<p>
</p>
<p><strong>
进阶:
</strong>
你可以设计一个时间复杂度为
<code>
O(log n)
</code>
的解决方案吗?
</p>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
int
search
(
vector
<
int
>
&
nums
,
int
target
)
{
int
lo
=
0
;
int
hi
=
nums
.
size
()
-
1
;
for
(
lo
<=
hi
)
{
int
mid
=
lo
+
(
hi
-
lo
)
/
2
;
if
(
nums
[
mid
]
==
target
)
{
return
mid
;
}
if
(
nums
[
lo
]
<=
nums
[
mid
])
{
____________
}
else
{
if
(
nums
[
mid
]
<
target
&&
target
<=
nums
[
hi
])
{
lo
=
mid
+
1
;
}
else
{
hi
=
mid
-
1
;
}
}
}
return
-
1
;
}
};
```
## template
```
cpp
...
...
@@ -77,7 +117,14 @@ public:
## 答案
```
cpp
if
(
nums
[
lo
]
<=
target
&&
target
<
nums
[
mid
])
{
hi
=
mid
-
1
;
}
else
{
lo
=
mid
+
1
;
}
```
## 选项
...
...
@@ -85,17 +132,38 @@ public:
### A
```
cpp
if
(
nums
[
lo
]
<=
target
&&
target
<
nums
[
mid
])
{
hi
=
mid
+
1
;
}
else
{
lo
=
mid
-
1
;
}
```
### B
```
cpp
if
(
nums
[
lo
]
<=
target
&&
target
<
nums
[
mid
])
{
lo
=
mid
-
1
;
}
else
{
hi
=
mid
+
1
;
}
```
### C
```
cpp
if
(
nums
[
lo
]
<=
target
&&
target
<
nums
[
mid
])
{
lo
=
mid
+
1
;
}
else
{
hi
=
mid
-
1
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/59.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,99 @@
<p>编写一个高效的算法来判断 <code>m x n</code> 矩阵中,是否存在一个目标值。该矩阵具有如下特性:</p><ul> <li>每行中的整数从左到右按升序排列。</li> <li>每行的第一个整数大于前一行的最后一个整数。</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0074.Search%20a%202D%20Matrix/images/mat.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0074.Search%20a%202D%20Matrix/images/mat2.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 <= m, n <= 100</code></li> <li><code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static
int
binary_search
(
int
*
nums
,
int
len
,
int
target
)
{
int
low
=
-
1
;
int
high
=
len
;
while
(
low
+
1
<
high
)
{
int
mid
=
low
+
(
high
-
low
)
/
2
;
if
(
target
>
nums
[
mid
])
{
low
=
mid
;
}
else
{
high
=
mid
;
}
}
______________________
else
{
return
high
;
}
}
static
bool
searchMatrix
(
int
**
matrix
,
int
matrixRowSize
,
int
matrixColSize
,
int
target
)
{
if
(
matrixRowSize
==
0
||
matrixColSize
==
0
)
{
return
false
;
}
if
(
target
<
matrix
[
0
][
0
]
||
target
>
matrix
[
matrixRowSize
-
1
][
matrixColSize
-
1
])
{
return
false
;
}
int
row
=
0
;
int
*
nums
=
NULL
;
if
(
matrixRowSize
>
0
)
{
nums
=
malloc
(
matrixRowSize
*
sizeof
(
int
));
for
(
row
=
0
;
row
<
matrixRowSize
;
row
++
)
{
nums
[
row
]
=
matrix
[
row
][
0
];
}
row
=
binary_search
(
nums
,
matrixRowSize
,
target
);
if
(
row
>=
0
)
{
return
true
;
}
else
{
row
=
-
row
-
1
;
if
(
row
==
0
)
{
return
false
;
}
else
{
row
--
;
}
}
}
int
col
=
binary_search
(
matrix
[
row
],
matrixColSize
,
target
);
return
col
>=
0
;
}
int
main
(
int
argc
,
char
**
argv
)
{
int
row
=
3
;
int
col
=
4
;
int
**
mat
=
malloc
(
row
*
sizeof
(
int
*
));
mat
[
0
]
=
malloc
(
col
*
sizeof
(
int
));
mat
[
0
][
0
]
=
1
;
mat
[
0
][
1
]
=
3
;
mat
[
0
][
2
]
=
5
;
mat
[
0
][
3
]
=
7
;
mat
[
1
]
=
malloc
(
col
*
sizeof
(
int
));
mat
[
1
][
0
]
=
10
;
mat
[
1
][
1
]
=
11
;
mat
[
1
][
2
]
=
16
;
mat
[
1
][
3
]
=
20
;
mat
[
2
]
=
malloc
(
col
*
sizeof
(
int
));
mat
[
2
][
0
]
=
23
;
mat
[
2
][
1
]
=
30
;
mat
[
2
][
2
]
=
34
;
mat
[
2
][
3
]
=
50
;
printf
(
"%s
\n
"
,
searchMatrix
(
mat
,
row
,
col
,
atoi
(
argv
[
1
]))
?
"true"
:
"false"
);
return
0
;
}
```
## template
```
cpp
...
...
@@ -101,7 +194,10 @@ int main(int argc, char **argv)
## 答案
```
cpp
if
(
high
==
len
||
nums
[
high
]
!=
target
)
{
return
-
high
-
1
;
}
```
## 选项
...
...
@@ -109,17 +205,26 @@ int main(int argc, char **argv)
### A
```
cpp
if
(
high
==
len
||
nums
[
high
]
!=
target
)
{
return
-
high
+
1
;
}
```
### B
```
cpp
if
(
high
==
len
||
nums
[
high
]
!=
target
)
{
return
high
-
1
;
}
```
### C
```
cpp
if
(
high
==
len
||
nums
[
high
]
!=
target
)
{
return
high
+
1
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/60.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,46 @@
<p>
实现
<a
href=
"https://www.cplusplus.com/reference/valarray/pow/"
target=
"_blank"
>
pow(
<em>
x
</em>
,
<em>
n
</em>
)
</a>
,即计算 x 的 n 次幂函数(即,x
<sup><span
style=
"font-size:10.8333px"
>
n
</span></sup>
)。
</p><p>
</p><p><strong>
示例 1:
</strong></p><pre><strong>
输入:
</strong>
x = 2.00000, n = 10
<strong><br
/>
输出:
</strong>
1024.00000
</pre><p><strong>
示例 2:
</strong></p><pre><strong>
输入:
</strong>
x = 2.10000, n = 3
<strong><br
/>
输出:
</strong>
9.26100
</pre><p><strong>
示例 3:
</strong></p><pre><strong>
输入:
</strong>
x = 2.00000, n = -2
<strong><br
/>
输出:
</strong>
0.25000
<strong><br
/>
解释:
</strong>
2
<sup>
-2
</sup>
= 1/2
<sup>
2
</sup>
= 1/4 = 0.25
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li><code>
-100.0
<
x
<
100.0</
code
></li>
<li><code>
-2
<sup>
31
</sup>
<
=
n
<= 2<
sup
>
31
</sup>
-1
</code></li>
<li><code>
-10
<sup>
4
</sup>
<
=
x
<
sup
>
n
</sup>
<
=
10<
sup
>
4
</sup></code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
double
myPow
(
double
x
,
int
n
)
{
if
(
n
==
INT_MIN
)
{
double
t
=
dfs
(
x
,
-
(
n
/
2
));
return
1
/
t
*
1
/
t
;
}
else
{
___________________
}
}
private:
double
dfs
(
double
x
,
int
n
)
{
if
(
n
==
0
)
{
return
1
;
}
else
if
(
n
==
1
)
{
return
x
;
}
else
{
double
t
=
dfs
(
x
,
n
/
2
);
return
(
n
%
2
)
?
(
x
*
t
*
t
)
:
(
t
*
t
);
}
}
};
```
## template
```
cpp
...
...
@@ -45,7 +85,7 @@ private:
## 答案
```
cpp
return
n
<
0
?
1
/
dfs
(
x
,
-
n
)
:
dfs
(
x
,
n
);
```
## 选项
...
...
@@ -53,17 +93,17 @@ private:
### A
```
cpp
return
n
<
0
?
1
/
dfs
(
x
,
n
)
:
dfs
(
x
,
-
n
);
```
### B
```
cpp
return
n
<
0
?
dfs
(
x
,
-
n
)
:
1
/
dfs
(
x
,
n
);
```
### C
```
cpp
return
n
<
0
?
dfs
(
x
,
n
)
:
1
/
dfs
(
x
,
-
n
);
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/61.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -9,6 +9,41 @@
<li>
不考虑答案输出的顺序。
</li>
</ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
vector
<
vector
<
string
>>
groupAnagrams
(
vector
<
string
>
&
strs
)
{
vector
<
vector
<
string
>>
res
;
unordered_map
<
string
,
vector
<
string
>>
ht
;
for
(
const
auto
&
str
:
strs
)
{
int
counts
[
26
]
=
{
0
};
for
(
char
c
:
str
)
{
counts
[
c
-
'a'
]
++
;
}
string
key
;
for
(
int
i
:
counts
)
{
________________
}
ht
[
key
].
push_back
(
str
);
}
for
(
const
auto
&
t
:
ht
)
{
res
.
push_back
(
t
.
second
);
}
return
res
;
}
};
```
## template
```
cpp
...
...
@@ -48,7 +83,8 @@ public:
## 答案
```
cpp
key
.
push_back
(
'#'
);
key
.
push_back
(
i
+
'0'
);
```
## 选项
...
...
@@ -56,17 +92,20 @@ public:
### A
```
cpp
key
.
push_back
(
'#'
);
key
.
push_back
(
'0'
);
```
### B
```
cpp
key
.
push_back
(
i
+
'#'
);
key
.
push_back
(
i
+
'0'
);
```
### C
```
cpp
key
.
push_back
(
i
+
'#'
);
key
.
push_back
(
'0'
);
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/62.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,66 +2,82 @@
<p>给定一个 <code><em>m</em> x <em>n</em></code> 的矩阵,如果一个元素为 <strong>0 </strong>,则将其所在行和列的所有元素都设为 <strong>0</strong> 。请使用 <strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地</a></strong> 算法<strong>。</strong></p><p><strong>进阶:</strong></p><ul> <li>一个直观的解决方案是使用 <code>O(<em>m</em><em>n</em>)</code> 的额外空间,但这并不是一个好的解决方案。</li> <li>一个简单的改进方案是使用 <code>O(<em>m</em> + <em>n</em>)</code> 的额外空间,但这仍然不是最好的解决方案。</li> <li>你能想出一个仅使用常量空间的解决方案吗?</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0073.Set%20Matrix%20Zeroes/images/mat1.jpg" style="width: 450px; height: 169px;" /><pre><strong>输入:</strong>matrix = [[1,1,1],[1,0,1],[1,1,1]]<strong><br />输出:</strong>[[1,0,1],[0,0,0],[1,0,1]]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0073.Set%20Matrix%20Zeroes/images/mat2.jpg" style="width: 450px; height: 137px;" /><pre><strong>输入:</strong>matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]<strong><br />输出:</strong>[[0,0,0,0],[0,4,5,0],[0,3,1,0]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[0].length</code></li> <li><code>1 <= m, n <= 200</code></li> <li><code>-2<sup>31</sup> <= matrix[i][j] <= 2<sup>31</sup> - 1</code></li></ul>
## template
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
void
setZeroes
(
vector
<
vector
<
int
>>
&
matrix
)
void
setZeroes
(
vector
<
vector
<
int
>>
&
matrix
)
{
int
i
=
matrix
.
size
();
int
j
=
matrix
[
0
].
size
();
vector
<
int
>
hang
(
i
);
vector
<
int
>
lie
(
j
);
for
(
int
m
=
0
;
m
<
i
;
m
++
)
{
for
(
int
n
=
0
;
n
<
j
;
n
++
)
{
if
(
!
matrix
[
m
][
n
])
{
hang
[
m
]
=
lie
[
n
]
=
true
;
}
}
}
for
(
int
m
=
0
;
m
<
i
;
m
++
)
{
for
(
int
n
=
0
;
n
<
j
;
n
++
)
{
__________________
}
}
}
};
```
## template
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
bool
bRow
=
false
,
bCol
=
false
;
for
(
int
row
=
0
;
row
<
matrix
.
size
();
row
++
)
{
for
(
int
col
=
0
;
col
<
matrix
[
row
].
size
();
col
++
)
{
if
(
matrix
[
row
][
col
]
==
0
)
{
if
(
row
==
0
)
{
bRow
=
true
;
}
if
(
col
==
0
)
{
bCol
=
true
;
}
matrix
[
0
][
col
]
=
matrix
[
row
][
0
]
=
0
;
}
}
}
for
(
int
row
=
1
;
row
<
matrix
.
size
();
row
++
)
{
for
(
int
col
=
1
;
col
<
matrix
[
row
].
size
();
col
++
)
{
if
(
matrix
[
0
][
col
]
==
0
||
matrix
[
row
][
0
]
==
0
)
{
matrix
[
row
][
col
]
=
0
;
}
}
}
if
(
bRow
)
{
for
(
auto
&
m
:
matrix
[
0
])
{
m
=
0
;
}
}
if
(
bCol
)
{
for
(
int
row
=
0
;
row
<
matrix
.
size
();
row
++
)
{
matrix
[
row
][
0
]
=
0
;
}
}
}
}
;
public:
void
setZeroes
(
vector
<
vector
<
int
>>
&
matrix
)
{
int
i
=
matrix
.
size
();
int
j
=
matrix
[
0
].
size
();
vector
<
int
>
hang
(
i
);
vector
<
int
>
lie
(
j
);
for
(
int
m
=
0
;
m
<
i
;
m
++
)
{
for
(
int
n
=
0
;
n
<
j
;
n
++
)
{
if
(
!
matrix
[
m
][
n
])
{
hang
[
m
]
=
lie
[
n
]
=
true
;
}
}
}
for
(
int
m
=
0
;
m
<
i
;
m
++
)
{
for
(
int
n
=
0
;
n
<
j
;
n
++
)
{
if
(
hang
[
m
]
||
lie
[
n
])
matrix
[
m
][
n
]
=
0
;
}
}
}
};
```
## 答案
```
cpp
if
(
hang
[
m
]
||
lie
[
n
])
matrix
[
m
][
n
]
=
0
;
```
## 选项
...
...
@@ -69,17 +85,20 @@ void setZeroes(vector<vector<int>> &matrix)
### A
```
cpp
if
(
hang
[
m
]
&&
lie
[
n
])
matrix
[
m
][
n
]
=
0
;
```
### B
```
cpp
if
(
hang
[
m
]
>=
lie
[
n
])
matrix
[
m
][
n
]
=
0
;
```
### C
```
cpp
if
(
hang
[
m
]
<=
lie
[
n
])
matrix
[
m
][
n
]
=
0
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/63.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -22,6 +22,68 @@
<li><code>
-10
<sup>
9
</sup>
<
=
target
<=
10<
sup
>
9
</sup></code></li>
</ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
vector
<
int
>
searchRange
(
vector
<
int
>
&
nums
,
int
target
)
{
vector
<
int
>
res
;
res
.
push_back
(
binary_search_begin
(
nums
,
target
));
res
.
push_back
(
binary_search_end
(
nums
,
target
));
return
res
;
}
private:
int
binary_search_begin
(
vector
<
int
>
nums
,
int
target
)
{
int
lo
=
-
1
;
int
hi
=
nums
.
size
();
while
(
lo
+
1
<
hi
)
{
int
mid
=
lo
+
(
hi
-
lo
)
/
2
;
if
(
target
>
nums
[
mid
])
{
lo
=
mid
;
}
else
{
hi
=
mid
;
}
}
if
(
hi
==
nums
.
size
()
||
nums
[
hi
]
!=
target
)
{
return
-
1
;
}
else
{
return
hi
;
}
}
int
binary_search_end
(
vector
<
int
>
nums
,
int
target
)
{
int
lo
=
-
1
;
int
hi
=
nums
.
size
();
while
(
lo
+
1
<
hi
)
{
int
mid
=
lo
+
(
hi
-
lo
)
/
2
;
______________
}
if
(
lo
==
-
1
||
nums
[
lo
]
!=
target
)
{
return
-
1
;
}
else
{
return
lo
;
}
}
};
```
## template
```
cpp
...
...
@@ -94,7 +156,14 @@ private:
## 答案
```
cpp
if
(
target
<
nums
[
mid
])
{
hi
=
mid
;
}
else
{
lo
=
mid
;
}
```
## 选项
...
...
@@ -102,17 +171,38 @@ private:
### A
```
cpp
if
(
target
<
nums
[
mid
])
{
lo
=
mid
;
}
else
{
hi
=
mid
;
}
```
### B
```
cpp
if
(
target
>
nums
[
mid
])
{
hi
=
mid
;
}
else
{
lo
=
mid
;
}
```
### C
```
cpp
if
(
target
>=
nums
[
mid
])
{
hi
=
mid
;
}
else
{
lo
=
mid
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/64.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -8,6 +8,30 @@
<p><strong>
说明:
</strong></p>
<p>
假设你总是可以到达数组的最后一个位置。
</p>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
int
jump
(
vector
<
int
>
&
nums
)
{
int
steps
=
0
;
int
lo
=
0
,
hi
=
0
;
while
(
hi
<
nums
.
size
()
-
1
)
{
int
right
=
0
;
_______________________
hi
=
right
;
steps
++
;
}
return
steps
;
}
};
```
## template
```
cpp
...
...
@@ -39,7 +63,11 @@ public:
## 答案
```
cpp
for
(
int
i
=
lo
;
i
<=
hi
;
i
++
)
{
right
=
max
(
i
+
nums
[
i
],
right
);
}
lo
=
hi
+
1
;
```
## 选项
...
...
@@ -47,17 +75,29 @@ public:
### A
```
cpp
for
(
int
i
=
lo
;
i
<=
hi
;
i
++
)
{
right
=
max
(
i
+
nums
[
i
],
right
);
}
lo
=
hi
-
1
;
```
### B
```
cpp
for
(
int
i
=
lo
;
i
<=
hi
;
i
++
)
{
right
=
max
(
nums
[
i
],
right
);
}
lo
=
hi
-
1
;
```
### C
```
cpp
for
(
int
i
=
lo
;
i
<=
hi
;
i
++
)
{
right
=
max
(
nums
[
i
],
right
);
}
lo
=
hi
+
1
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/65.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,65 @@
给你单链表的头指针
<code>head</code> 和两个整数 <code>left</code> 和 <code>right</code> ,其中 <code>left <= right</code> 。请你反转从位置 <code>left</code> 到位置 <code>right</code> 的链表节点,返回 <strong>反转后的链表</strong> 。<p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0092.Reverse%20Linked%20List%20II/images/rev2ex2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], left = 2, right = 4<strong><br />输出:</strong>[1,4,3,2,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [5], left = 1, right = 1<strong><br />输出:</strong>[5]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点数目为 <code>n</code></li> <li><code>1 <= n <= 500</code></li> <li><code>-500 <= Node.val <= 500</code></li> <li><code>1 <= left <= right <= n</code></li></ul><p> </p><p><strong>进阶:</strong> 你可以使用一趟扫描完成反转吗?</p>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
struct
ListNode
{
int
val
;
struct
ListNode
*
next
;
};
static
struct
ListNode
*
reverseBetween
(
struct
ListNode
*
head
,
int
m
,
int
n
)
{
int
i
;
struct
ListNode
dummy
;
struct
ListNode
*
prev
=
&
dummy
;
prev
->
next
=
head
;
for
(
i
=
1
;
i
<
m
;
i
++
)
{
prev
=
prev
->
next
;
}
struct
ListNode
*
p
=
prev
->
next
;
for
(
i
=
m
;
i
<
n
;
i
++
)
{
struct
ListNode
*
q
=
p
->
next
;
__________________
}
return
dummy
.
next
;
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
<
3
)
{
fprintf
(
stderr
,
"Usage: ./test m n 1 2 3...
\n
"
);
exit
(
-
1
);
}
int
i
,
count
=
argc
-
3
;
struct
ListNode
dummy
;
struct
ListNode
*
prev
=
&
dummy
;
struct
ListNode
*
p
;
for
(
i
=
0
;
i
<
count
;
i
++
)
{
p
=
malloc
(
sizeof
(
*
p
));
p
->
val
=
atoi
(
argv
[
i
+
3
]);
p
->
next
=
NULL
;
prev
->
next
=
p
;
prev
=
p
;
}
int
m
=
atoi
(
argv
[
1
]);
int
n
=
atoi
(
argv
[
2
]);
struct
ListNode
*
head
=
reverseBetween
(
dummy
.
next
,
m
,
n
);
for
(
p
=
head
;
p
!=
NULL
;
p
=
p
->
next
)
{
printf
(
"%d "
,
p
->
val
);
}
printf
(
"
\n
"
);
return
0
;
}
```
## template
```
cpp
...
...
@@ -66,7 +125,9 @@ int main(int argc, char **argv)
## 答案
```
cpp
p
->
next
=
q
->
next
;
q
->
next
=
prev
->
next
;
prev
->
next
=
q
;
```
## 选项
...
...
@@ -74,17 +135,23 @@ int main(int argc, char **argv)
### A
```
cpp
q
->
next
=
prev
->
next
;
p
->
next
=
q
->
next
;
prev
->
next
=
q
;
```
### B
```
cpp
prev
->
next
=
q
;
p
->
next
=
q
->
next
;
q
->
next
=
prev
->
next
;
```
### C
```
cpp
q
->
next
=
prev
->
next
;
p
->
next
=
q
->
next
;
prev
->
next
=
q
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/66.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,67 @@
<p>给定一个包含非负整数的 <code><em>m</em> x <em>n</em></code> 网格 <code>grid</code> ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。</p><p><strong>说明:</strong>每次只能向下或者向右移动一步。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0064.Minimum%20Path%20Sum/images/minpath.jpg" style="width: 242px; height: 242px;" /><pre><strong>输入:</strong>grid = [[1,3,1],[1,5,1],[4,2,1]]<strong><br />输出:</strong>7<strong><br />解释:</strong>因为路径 1→3→1→1→1 的总和最小。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>grid = [[1,2,3],[4,5,6]]<strong><br />输出:</strong>12</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 <= m, n <= 200</code></li> <li><code>0 <= grid[i][j] <= 100</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static
inline
int
min
(
int
a
,
int
b
)
{
return
a
<
b
?
a
:
b
;
}
int
minPathSum
(
int
**
grid
,
int
gridRowSize
,
int
gridColSize
)
{
int
i
,
j
;
int
**
dp
=
malloc
(
gridRowSize
*
sizeof
(
int
*
));
for
(
i
=
0
;
i
<
gridRowSize
;
i
++
)
{
dp
[
i
]
=
malloc
(
gridColSize
*
sizeof
(
int
));
}
dp
[
0
][
0
]
=
grid
[
0
][
0
];
int
sum
=
dp
[
0
][
0
];
for
(
i
=
1
;
i
<
gridRowSize
;
i
++
)
{
sum
+=
grid
[
i
][
0
];
dp
[
i
][
0
]
=
sum
;
}
sum
=
dp
[
0
][
0
];
for
(
i
=
1
;
i
<
gridColSize
;
i
++
)
{
sum
+=
grid
[
0
][
i
];
dp
[
0
][
i
]
=
sum
;
}
for
(
i
=
1
;
i
<
gridRowSize
;
i
++
)
{
for
(
j
=
1
;
j
<
gridColSize
;
j
++
)
{
_____________________
}
}
return
dp
[
gridRowSize
-
1
][
gridColSize
-
1
];
}
int
main
(
int
argc
,
char
**
argv
)
{
int
i
,
j
;
int
row
=
argc
-
1
;
int
col
=
strlen
(
argv
[
1
]);
int
**
grid
=
malloc
(
row
*
sizeof
(
int
*
));
for
(
i
=
0
;
i
<
row
;
i
++
)
{
grid
[
i
]
=
malloc
(
col
*
sizeof
(
int
));
for
(
j
=
0
;
j
<
col
;
j
++
)
{
grid
[
i
][
j
]
=
argv
[
i
+
1
][
j
]
-
'0'
;
printf
(
"%d "
,
grid
[
i
][
j
]);
}
printf
(
"
\n
"
);
}
printf
(
"%d
\n
"
,
minPathSum
(
grid
,
row
,
col
));
return
0
;
}
```
## template
```
cpp
...
...
@@ -66,7 +127,7 @@ int main(int argc, char **argv)
## 答案
```
cpp
dp
[
i
][
j
]
=
grid
[
i
][
j
]
+
min
(
dp
[
i
-
1
][
j
],
dp
[
i
][
j
-
1
]);
```
## 选项
...
...
@@ -74,17 +135,17 @@ int main(int argc, char **argv)
### A
```
cpp
dp
[
i
][
j
]
=
grid
[
i
][
j
]
+
min
(
dp
[
i
][
j
],
dp
[
i
-
1
][
j
-
1
]);
```
### B
```
cpp
dp
[
i
][
j
]
=
grid
[
i
][
j
]
+
min
(
dp
[
i
][
j
-
1
],
dp
[
i
-
1
][
j
-
1
]);
```
### C
```
cpp
dp
[
i
][
j
]
=
grid
[
i
][
j
]
+
min
(
dp
[
i
-
1
][
j
+
1
],
dp
[
i
+
1
][
j
-
1
]);
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/67.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,38 @@
<p>给你一个链表,删除链表的倒数第 <code>n</code><em> </em>个结点,并且返回链表的头结点。</p><p><strong>进阶:</strong>你能尝试使用一趟扫描实现吗?</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0019.Remove%20Nth%20Node%20From%20End%20of%20List/images/remove_ex1.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], n = 2<strong><br />输出:</strong>[1,2,3,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [1], n = 1<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1,2], n = 1<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中结点的数目为 <code>sz</code></li> <li><code>1 <= sz <= 30</code></li> <li><code>0 <= Node.val <= 100</code></li> <li><code>1 <= n <= sz</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
struct
ListNode
{
int
val
;
ListNode
*
next
;
ListNode
()
:
val
(
0
),
next
(
nullptr
)
{}
ListNode
(
int
x
)
:
val
(
x
),
next
(
nullptr
)
{}
ListNode
(
int
x
,
ListNode
*
next
)
:
val
(
x
),
next
(
next
)
{}
};
#include <vector>
class
Solution
{
public:
ListNode
*
removeNthFromEnd
(
ListNode
*
head
,
int
n
)
{
ListNode
empty_node
(
0
,
head
);
ListNode
*
p
=
&
empty_node
;
std
::
vector
<
ListNode
*>
pv
;
while
(
p
!=
nullptr
)
{
pv
.
push_back
(
p
);
p
=
p
->
next
;
}
_________________
p
->
next
=
p
->
next
->
next
;
return
empty_node
.
next
;
}
};
```
## template
```
cpp
...
...
@@ -37,7 +69,7 @@ public:
## 答案
```
cpp
p
=
pv
[
pv
.
size
()
-
1
-
n
];
```
## 选项
...
...
@@ -45,17 +77,17 @@ public:
### A
```
cpp
p
=
pv
[
pv
.
size
()
+
1
-
n
];
```
### B
```
cpp
p
=
pv
[
pv
.
size
()
-
1
+
n
];
```
### C
```
cpp
p
=
pv
[
pv
.
size
()
+
1
+
n
];
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/68.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -21,6 +21,45 @@
<li><code>
1
<
= target
<
= 500
</code></li>
</ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
vector
<
vector
<
int
>>
combinationSum
(
vector
<
int
>
&
candidates
,
int
target
)
{
vector
<
vector
<
int
>>
res
;
dfs
(
candidates
,
0
,
target
,
res
);
return
res
;
}
private:
vector
<
int
>
stack
;
void
dfs
(
vector
<
int
>
&
candidates
,
int
start
,
int
target
,
vector
<
vector
<
int
>>
&
res
)
{
if
(
target
<
0
)
{
return
;
}
else
if
(
target
==
0
)
{
res
.
push_back
(
stack
);
}
else
{
for
(
int
i
=
start
;
i
<
candidates
.
size
();
i
++
)
{
stack
.
push_back
(
candidates
[
i
]);
_____________________________
stack
.
pop_back
();
}
}
}
};
```
## template
```
cpp
...
...
@@ -63,7 +102,7 @@ private:
## 答案
```
cpp
dfs
(
candidates
,
i
,
target
-
candidates
[
i
],
res
);
```
## 选项
...
...
@@ -71,17 +110,17 @@ private:
### A
```
cpp
dfs
(
candidates
,
i
,
target
-
i
,
res
);
```
### B
```
cpp
dfs
(
candidates
,
i
,
candidates
[
i
],
res
);
```
### C
```
cpp
dfs
(
candidates
,
i
,
target
+
candidates
[
i
],
res
);
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/69.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,51 @@
<p>
给你一个字符串
<code>
s
</code>
,找到
<code>
s
</code>
中最长的回文子串。
</p><p>
</p><p><strong>
示例 1:
</strong></p><pre><strong>
输入:
</strong>
s = "babad"
<strong><br
/>
输出:
</strong>
"bab"
<strong><br
/>
解释:
</strong>
"aba" 同样是符合题意的答案。
</pre><p><strong>
示例 2:
</strong></p><pre><strong>
输入:
</strong>
s = "cbbd"
<strong><br
/>
输出:
</strong>
"bb"
</pre><p><strong>
示例 3:
</strong></p><pre><strong>
输入:
</strong>
s = "a"
<strong><br
/>
输出:
</strong>
"a"
</pre><p><strong>
示例 4:
</strong></p><pre><strong>
输入:
</strong>
s = "ac"
<strong><br
/>
输出:
</strong>
"a"
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li><code>
1
<
=
s.length
<=
1000</
code
></li>
<li><code>
s
</code>
仅由数字和英文字母(大写和/或小写)组成
</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
class
Solution
{
public:
string
longestPalindrome
(
string
s
)
{
int
ti
=
0
,
maxlen
=
0
,
i
,
t
;
for
(
i
=
0
;
s
[
i
];
i
++
)
{
t
=
1
;
while
(
t
<=
i
&&
s
[
i
+
t
])
{
if
(
s
[
i
+
t
]
==
s
[
i
-
t
])
t
++
;
else
break
;
}
t
--
;
if
(
2
*
t
+
1
>
maxlen
)
{
ti
=
i
-
t
;
maxlen
=
2
*
t
+
1
;
}
}
for
(
i
=
0
;
s
[
i
];
i
++
)
{
t
=
1
;
while
(
t
<=
i
+
1
&&
s
[
i
+
t
])
{
if
(
s
[
i
-
t
+
1
]
==
s
[
i
+
t
])
t
++
;
else
break
;
}
t
--
;
________________
}
s
[
ti
+
maxlen
]
=
0
;
return
s
.
c_str
()
+
ti
;
}
};
```
## template
```
cpp
...
...
@@ -54,7 +99,11 @@ public:
## 答案
```
cpp
if
(
2
*
t
>
maxlen
)
{
ti
=
i
-
t
+
1
;
maxlen
=
2
*
t
;
}
```
## 选项
...
...
@@ -62,17 +111,29 @@ public:
### A
```
cpp
if
(
t
>
maxlen
)
{
ti
=
i
-
t
+
1
;
maxlen
=
2
*
t
;
}
```
### B
```
cpp
if
(
2
*
t
>
maxlen
)
{
ti
=
i
+
t
+
1
;
maxlen
=
2
*
t
;
}
```
### C
```
cpp
if
(
t
>
maxlen
)
{
ti
=
i
+
t
+
1
;
maxlen
=
2
*
t
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/70.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -31,6 +31,61 @@
<li>
这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?
</li>
</ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static
bool
search
(
int
*
nums
,
int
numsSize
,
int
target
)
{
int
lo
=
0
;
int
hi
=
numsSize
-
1
;
while
(
lo
<=
hi
)
{
int
mid
=
lo
+
(
hi
-
lo
)
/
2
;
if
(
nums
[
mid
]
==
target
)
{
return
true
;
}
if
(
nums
[
lo
]
==
nums
[
mid
]
&&
nums
[
mid
]
==
nums
[
hi
])
{
lo
++
;
hi
--
;
}
else
if
(
nums
[
lo
]
<=
nums
[
mid
])
{
___________________
}
else
{
if
(
nums
[
mid
]
<
target
&&
target
<=
nums
[
hi
])
{
lo
=
mid
+
1
;
}
else
{
hi
=
mid
-
1
;
}
}
}
return
false
;
}
int
main
(
int
argc
,
char
**
argv
)
{
int
i
;
int
target
=
atoi
(
argv
[
1
]);
int
size
=
argc
-
2
;
int
*
nums
=
malloc
(
size
*
sizeof
(
int
));
for
(
i
=
0
;
i
<
argc
-
2
;
i
++
)
{
nums
[
i
]
=
atoi
(
argv
[
i
+
2
]);
}
printf
(
"%d
\n
"
,
search
(
nums
,
size
,
target
));
return
0
;
}
```
## template
```
cpp
...
...
@@ -96,7 +151,14 @@ int main(int argc, char **argv)
## 答案
```
cpp
if
(
nums
[
lo
]
<=
target
&&
target
<
nums
[
mid
])
{
hi
=
mid
-
1
;
}
else
{
lo
=
mid
+
1
;
}
```
## 选项
...
...
@@ -104,17 +166,38 @@ int main(int argc, char **argv)
### A
```
cpp
if
(
nums
[
lo
]
<=
target
&&
target
<
nums
[
mid
])
{
hi
=
mid
+
1
;
}
else
{
lo
=
mid
-
1
;
}
```
### B
```
cpp
if
(
nums
[
lo
]
<=
target
&&
target
<
nums
[
mid
])
{
lo
=
mid
+
1
;
}
else
{
hi
=
mid
-
1
;
}
```
### C
```
cpp
if
(
nums
[
lo
]
<=
target
&&
target
<
nums
[
mid
])
{
lo
=
mid
-
1
;
}
else
{
hi
=
mid
+
1
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/71.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -29,6 +29,51 @@
<li><code>
nums
</code>
已按升序排列
</li>
</ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
static
int
removeDuplicates
(
int
*
nums
,
int
numsSize
)
{
if
(
numsSize
==
0
)
{
return
0
;
}
int
i
;
int
len
=
0
;
int
count
=
1
;
for
(
i
=
1
;
i
<
numsSize
;
i
++
)
{
if
(
nums
[
len
]
==
nums
[
i
])
{
_____________________
}
else
{
count
=
1
;
nums
[
++
len
]
=
nums
[
i
];
}
}
return
len
+
1
;
}
int
main
(
int
argc
,
char
**
argv
)
{
int
i
,
count
=
argc
-
1
;
int
*
nums
=
malloc
(
count
*
sizeof
(
int
));
for
(
i
=
0
;
i
<
count
;
i
++
)
{
nums
[
i
]
=
atoi
(
argv
[
i
+
1
]);
}
count
=
removeDuplicates
(
nums
,
count
);
for
(
i
=
0
;
i
<
count
;
i
++
)
{
printf
(
"%d "
,
nums
[
i
]);
}
printf
(
"
\n
"
);
}
```
## template
```
cpp
...
...
@@ -81,7 +126,11 @@ int main(int argc, char **argv)
## 答案
```
cpp
if
(
count
<
2
)
{
count
++
;
nums
[
++
len
]
=
nums
[
i
];
}
```
## 选项
...
...
@@ -89,17 +138,20 @@ int main(int argc, char **argv)
### A
```
cpp
count
++
;
nums
[
++
len
]
=
nums
[
i
];
```
### B
```
cpp
count
++
;
nums
[
len
++
]
=
nums
[
i
];
```
### C
```
cpp
count
++
;
nums
[
--
len
]
=
nums
[
i
];
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/72.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,61 @@
<p>给定三个字符串 <code>s1</code>、<code>s2</code>、<code>s3</code>,请你帮忙验证 <code>s3</code> 是否是由 <code>s1</code> 和 <code>s2</code><em> </em><strong>交错 </strong>组成的。</p><p>两个字符串 <code>s</code> 和 <code>t</code> <strong>交错</strong> 的定义与过程如下,其中每个字符串都会被分割成若干 <strong>非空</strong> 子字符串:</p><ul> <li><code>s = s<sub>1</sub> + s<sub>2</sub> + ... + s<sub>n</sub></code></li> <li><code>t = t<sub>1</sub> + t<sub>2</sub> + ... + t<sub>m</sub></code></li> <li><code>|n - m| <= 1</code></li> <li><strong>交错</strong> 是 <code>s<sub>1</sub> + t<sub>1</sub> + s<sub>2</sub> + t<sub>2</sub> + s<sub>3</sub> + t<sub>3</sub> + ...</code> 或者 <code>t<sub>1</sub> + s<sub>1</sub> + t<sub>2</sub> + s<sub>2</sub> + t<sub>3</sub> + s<sub>3</sub> + ...</code></li></ul><p><strong>提示:</strong><code>a + b</code> 意味着字符串 <code>a</code> 和 <code>b</code> 连接。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0097.Interleaving%20String/images/interleave.jpg" style="width: 561px; height: 203px;" /><pre><strong>输入:</strong>s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"<strong><br />输出:</strong>false</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s1 = "", s2 = "", s3 = ""<strong><br />输出:</strong>true</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s1.length, s2.length <= 100</code></li> <li><code>0 <= s3.length <= 200</code></li> <li><code>s1</code>、<code>s2</code>、和 <code>s3</code> 都由小写英文字母组成</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static
bool
isInterleave
(
char
*
s1
,
char
*
s2
,
char
*
s3
)
{
int
i
,
j
;
int
len1
=
strlen
(
s1
);
int
len2
=
strlen
(
s2
);
int
len3
=
strlen
(
s3
);
if
(
len1
+
len2
!=
len3
)
{
return
false
;
}
bool
*
table
=
malloc
((
len1
+
1
)
*
(
len2
+
1
)
*
sizeof
(
bool
));
bool
**
dp
=
malloc
((
len1
+
1
)
*
sizeof
(
bool
*
));
for
(
i
=
0
;
i
<
len1
+
1
;
i
++
)
{
dp
[
i
]
=
&
table
[
i
*
(
len2
+
1
)];
}
dp
[
0
][
0
]
=
true
;
for
(
i
=
1
;
i
<
len1
+
1
;
i
++
)
{
dp
[
i
][
0
]
=
dp
[
i
-
1
][
0
]
&&
s1
[
i
-
1
]
==
s3
[
i
-
1
];
}
for
(
i
=
1
;
i
<
len2
+
1
;
i
++
)
{
____________________
}
for
(
i
=
1
;
i
<
len1
+
1
;
i
++
)
{
for
(
j
=
1
;
j
<
len2
+
1
;
j
++
)
{
bool
up
=
dp
[
i
-
1
][
j
]
&&
s1
[
i
-
1
]
==
s3
[
i
+
j
-
1
];
bool
left
=
dp
[
i
][
j
-
1
]
&&
s2
[
j
-
1
]
==
s3
[
i
+
j
-
1
];
dp
[
i
][
j
]
=
up
||
left
;
}
}
return
dp
[
len1
][
len2
];
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
4
)
{
fprintf
(
stderr
,
"Usage: ./test s1 s2 s3
\n
"
);
exit
(
-
1
);
}
printf
(
"%s
\n
"
,
isInterleave
(
argv
[
1
],
argv
[
2
],
argv
[
3
])
?
"true"
:
"false"
);
return
0
;
}
```
## template
```
cpp
...
...
@@ -60,7 +115,7 @@ int main(int argc, char **argv)
## 答案
```
cpp
dp
[
0
][
i
]
=
dp
[
0
][
i
-
1
]
&&
s2
[
i
-
1
]
==
s3
[
i
-
1
];
```
## 选项
...
...
@@ -68,17 +123,17 @@ int main(int argc, char **argv)
### A
```
cpp
dp
[
0
][
i
]
=
dp
[
0
][
i
+
1
]
&&
s2
[
i
-
1
]
==
s3
[
i
-
1
];
```
### B
```
cpp
dp
[
0
][
i
]
=
dp
[
0
][
i
-
1
]
&&
s2
[
i
+
1
]
==
s3
[
i
+
1
];
```
### C
```
cpp
dp
[
0
][
i
]
=
dp
[
0
][
i
-
1
];
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/73.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,81 @@
<p>
以数组
<code>
intervals
</code>
表示若干个区间的集合,其中单个区间为
<code>
intervals[i] = [start
<sub>
i
</sub>
, end
<sub>
i
</sub>
]
</code>
。请你合并所有重叠的区间,并返回一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间。
</p><p>
</p><p><strong>
示例 1:
</strong></p><pre><strong>
输入:
</strong>
intervals = [[1,3],[2,6],[8,10],[15,18]]
<strong><br
/>
输出:
</strong>
[[1,6],[8,10],[15,18]]
<strong><br
/>
解释:
</strong>
区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
</pre><p><strong>
示例 2:
</strong></p><pre><strong>
输入:
</strong>
intervals = [[1,4],[4,5]]
<strong><br
/>
输出:
</strong>
[[1,5]]
<strong><br
/>
解释:
</strong>
区间 [1,4] 和 [4,5] 可被视为重叠区间。
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li><code>
1
<
=
intervals.length
<=
10<
sup
>
4
</sup></code></li>
<li><code>
intervals[i].length == 2
</code></li>
<li><code>
0
<
=
start
<
sub
>
i
</sub>
<
=
end
<
sub
>
i
</sub>
<
=
10<
sup
>
4
</sup></code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static
int
compare
(
const
void
*
a
,
const
void
*
b
)
{
return
((
int
*
)
a
)[
0
]
-
((
int
*
)
b
)[
0
];
}
int
**
merge
(
int
**
intervals
,
int
intervalsSize
,
int
*
intervalsColSize
,
int
*
returnSize
,
int
**
returnColumnSizes
)
{
if
(
intervalsSize
==
0
)
{
*
returnSize
=
0
;
return
intervals
;
}
int
i
,
len
=
0
;
int
*
tmp
=
malloc
(
intervalsSize
*
2
*
sizeof
(
int
));
for
(
i
=
0
;
i
<
intervalsSize
;
i
++
)
{
tmp
[
i
*
2
]
=
intervals
[
i
][
0
];
tmp
[
i
*
2
+
1
]
=
intervals
[
i
][
1
];
}
qsort
(
tmp
,
intervalsSize
,
2
*
sizeof
(
int
),
compare
);
intervals
[
0
][
0
]
=
tmp
[
0
];
intervals
[
0
][
1
]
=
tmp
[
1
];
for
(
i
=
1
;
i
<
intervalsSize
;
i
++
)
{
if
(
tmp
[
i
*
2
]
>
intervals
[
len
][
1
])
{
len
++
;
___________________________
}
else
if
(
tmp
[
i
*
2
+
1
]
>
intervals
[
len
][
1
])
{
intervals
[
len
][
1
]
=
tmp
[
i
*
2
+
1
];
}
}
len
+=
1
;
*
returnSize
=
len
;
*
returnColumnSizes
=
malloc
(
len
*
sizeof
(
int
));
for
(
i
=
0
;
i
<
len
;
i
++
)
{
(
*
returnColumnSizes
)[
i
]
=
2
;
}
return
intervals
;
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
<
1
||
argc
%
2
==
0
)
{
fprintf
(
stderr
,
"Usage: ./test s0 e0 s1 e1..."
);
exit
(
-
1
);
}
int
i
,
count
=
0
;
int
*
sizes
=
malloc
((
argc
-
1
)
/
2
*
sizeof
(
int
));
int
**
intervals
=
malloc
((
argc
-
1
)
/
2
*
sizeof
(
int
*
));
for
(
i
=
0
;
i
<
(
argc
-
1
)
/
2
;
i
++
)
{
sizes
[
i
]
=
2
;
intervals
[
i
]
=
malloc
(
2
*
sizeof
(
int
));
intervals
[
i
][
0
]
=
atoi
(
argv
[
i
*
2
+
1
]);
intervals
[
i
][
1
]
=
atoi
(
argv
[
i
*
2
+
2
]);
}
int
*
col_sizes
;
int
**
results
=
merge
(
intervals
,
(
argc
-
1
)
/
2
,
sizes
,
&
count
,
&
col_sizes
);
for
(
i
=
0
;
i
<
count
;
i
++
)
{
printf
(
"[%d,%d]
\n
"
,
results
[
i
][
0
],
results
[
i
][
1
]);
}
return
0
;
}
```
## template
```
cpp
...
...
@@ -81,7 +156,8 @@ int main(int argc, char **argv)
## 答案
```
cpp
intervals
[
len
][
0
]
=
tmp
[
i
*
2
];
intervals
[
len
][
1
]
=
tmp
[
i
*
2
+
1
];
```
## 选项
...
...
@@ -89,17 +165,20 @@ int main(int argc, char **argv)
### A
```
cpp
intervals
[
len
][
0
]
=
tmp
[
i
];
intervals
[
len
][
1
]
=
tmp
[
i
+
1
];
```
### B
```
cpp
intervals
[
len
][
0
]
=
tmp
[
i
];
intervals
[
len
][
1
]
=
tmp
[
i
-
1
];
```
### C
```
cpp
intervals
[
len
][
0
]
=
tmp
[
i
*
2
];
intervals
[
len
][
1
]
=
tmp
[
i
*
2
-
1
];
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/74.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,62 @@
<p>
给你一个包含
<code>
n
</code>
个整数的数组
<code>
nums
</code>
,判断
<code>
nums
</code>
中是否存在三个元素
<em>
a,b,c ,
</em>
使得
<em>
a + b + c =
</em>
0 ?请你找出所有和为
<code>
0
</code>
且不重复的三元组。
</p><p><strong>
注意:
</strong>
答案中不可以包含重复的三元组。
</p><p>
</p><p><strong>
示例 1:
</strong></p><pre><strong>
输入:
</strong>
nums = [-1,0,1,2,-1,-4]
<strong><br
/>
输出:
</strong>
[[-1,-1,2],[-1,0,1]]
</pre><p><strong>
示例 2:
</strong></p><pre><strong>
输入:
</strong>
nums = []
<strong><br
/>
输出:
</strong>
[]
</pre><p><strong>
示例 3:
</strong></p><pre><strong>
输入:
</strong>
nums = [0]
<strong><br
/>
输出:
</strong>
[]
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li><code>
0
<
=
nums.length
<=
3000</
code
></li>
<li><code>
-10
<sup>
5
</sup>
<
=
nums
[
i
]
<=
10<
sup
>
5
</sup></code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <algorithm>
#include <set>
class
Solution
{
public:
vector
<
vector
<
int
>>
threeSum
(
vector
<
int
>
&
nums
)
{
vector
<
vector
<
int
>>
r
;
if
(
nums
.
size
()
==
0
)
return
r
;
sort
(
nums
.
begin
(),
nums
.
end
());
int
cur
,
left
,
right
;
cur
=
0
;
while
(
cur
<
nums
.
size
())
{
if
(
nums
[
cur
]
>
0
)
break
;
left
=
cur
+
1
;
right
=
nums
.
size
()
-
1
;
while
(
left
<
right
)
{
int
n
=
nums
[
cur
]
+
nums
[
left
]
+
nums
[
right
];
if
(
n
==
0
)
{
r
.
emplace_back
(
vector
<
int
>
({
nums
[
cur
],
nums
[
left
],
nums
[
right
]}));
int
t
=
left
+
1
;
__________________________
}
else
if
(
n
>
0
)
{
int
t
=
right
-
1
;
while
(
t
>
left
&&
nums
[
t
]
==
nums
[
right
])
t
--
;
right
=
t
;
}
else
{
int
t
=
left
+
1
;
while
(
t
<
right
&&
nums
[
t
]
==
nums
[
left
])
t
++
;
left
=
t
;
}
}
int
t
=
cur
+
1
;
while
(
t
<
nums
.
size
()
&&
nums
[
t
]
==
nums
[
cur
])
t
++
;
cur
=
t
;
}
return
r
;
}
};
```
## template
```
cpp
...
...
@@ -67,7 +123,13 @@ public:
## 答案
```
cpp
while
(
t
<
right
&&
nums
[
t
]
==
nums
[
left
])
t
++
;
left
=
t
;
t
=
right
-
1
;
while
(
t
>
left
&&
nums
[
t
]
==
nums
[
right
])
t
--
;
right
=
t
;
```
## 选项
...
...
@@ -75,17 +137,35 @@ public:
### A
```
cpp
while
(
t
<
right
)
t
++
;
left
=
t
;
t
=
right
-
1
;
while
(
t
>
left
)
t
--
;
right
=
t
;
```
### B
```
cpp
while
(
t
<
right
)
t
++
;
left
=
t
;
t
=
right
+
1
;
while
(
t
>
left
)
t
--
;
right
=
t
;
```
### C
```
cpp
while
(
t
<
right
&&
nums
[
t
]
==
nums
[
left
])
t
++
;
left
=
t
;
t
=
right
+
1
;
while
(
t
>
left
&&
nums
[
t
]
==
nums
[
right
])
t
--
;
right
=
t
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/75.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,39 @@
<p>
给定两个以字符串形式表示的非负整数
<code>
num1
</code>
和
<code>
num2
</code>
,返回
<code>
num1
</code>
和
<code>
num2
</code>
的乘积,它们的乘积也表示为字符串形式。
</p><p><strong>
示例 1:
</strong></p><pre><strong>
输入:
</strong>
num1 =
"
2
"
, num2 =
"
3
"
<strong><br
/>
输出:
</strong>
"
6
"
</pre><p><strong>
示例
2:
</strong></p><pre><strong>
输入:
</strong>
num1 =
"
123
"
, num2 =
"
456
"
<strong><br
/>
输出:
</strong>
"
56088
"
</pre><p><strong>
说明:
</strong></p><ol>
<li><code>
num1
</code>
和
<code>
num2
</code>
的长度小于110。
</li>
<li><code>
num1
</code>
和
<code>
num2
</code>
只包含数字
<code>
0-9
</code>
。
</li>
<li><code>
num1
</code>
和
<code>
num2
</code>
均不以零开头,除非是数字 0 本身。
</li>
<li><strong>
不能使用任何标准库的大数类型(比如 BigInteger)
</strong>
或
<strong>
直接将输入转换为整数来处理
</strong>
。
</li></ol>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
string
multiply
(
string
num1
,
string
num2
)
{
string
res
(
num1
.
length
()
+
num2
.
length
(),
'0'
);
for
(
int
i
=
num2
.
length
()
-
1
;
i
>=
0
;
i
--
)
{
int
j
,
carry
=
0
;
for
(
j
=
num1
.
length
()
-
1
;
j
>=
0
;
j
--
)
{
_____________________
}
res
[
i
+
j
+
1
]
=
carry
+
'0'
;
}
int
i
;
for
(
i
=
0
;
i
<
res
.
length
()
-
1
;
i
++
)
{
if
(
res
[
i
]
!=
'0'
)
{
break
;
}
}
return
res
.
substr
(
i
);
}
};
```
## template
```
cpp
...
...
@@ -40,7 +73,9 @@ public:
## 答案
```
cpp
carry
+=
(
num1
[
j
]
-
'0'
)
*
(
num2
[
i
]
-
'0'
)
+
(
res
[
i
+
j
+
1
]
-
'0'
);
res
[
i
+
j
+
1
]
=
carry
%
10
+
'0'
;
carry
/=
10
;
```
## 选项
...
...
@@ -48,17 +83,23 @@ public:
### A
```
cpp
carry
+=
(
num1
[
j
]
-
'0'
)
*
(
num2
[
i
]
-
'0'
)
+
(
res
[
i
+
j
]
-
'0'
);
res
[
i
+
j
]
=
carry
%
10
+
'0'
;
carry
/=
10
;
```
### B
```
cpp
carry
+=
(
num1
[
j
]
-
'0'
)
*
(
num2
[
i
]
-
'0'
)
+
(
res
[
i
+
j
]
-
'0'
);
res
[
i
+
j
]
=
carry
%
10
+
'0'
;
carry
%=
10
;
```
### C
```
cpp
carry
+=
(
num1
[
j
]
-
'0'
)
*
(
num2
[
i
]
-
'0'
)
+
(
res
[
i
+
j
-
1
]
-
'0'
);
res
[
i
+
j
-
1
]
=
carry
%
10
+
'0'
;
carry
%=
10
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/76.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,54 @@
<p>
给定一个包括
<em>
n
</em>
个整数的数组
<code>
nums
</code><em>
</em>
和 一个目标值
<code>
target
</code>
。找出
<code>
nums
</code><em>
</em>
中的三个整数,使得它们的和与
<code>
target
</code>
最接近。返回这三个数的和。假定每组输入只存在唯一答案。
</p><p>
</p><p><strong>
示例:
</strong></p><pre><strong>
输入:
</strong>
nums = [-1,2,1,-4], target = 1
<strong><br
/>
输出:
</strong>
2
<strong><br
/>
解释:
</strong>
与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li><code>
3
<
= nums.length
<
= 10^3
</code></li>
<li><code>
-10^3
<
= nums[i]
<
= 10^3
</code></li>
<li><code>
-10^4
<
= target
<
= 10^4
</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <cstdlib>
class
Solution
{
public:
int
threeSumClosest
(
vector
<
int
>
&
nums
,
int
target
)
{
sort
(
nums
.
begin
(),
nums
.
end
());
int
cur
,
left
,
right
;
cur
=
0
;
int
closest
=
nums
[
0
]
+
nums
[
1
]
+
nums
[
2
];
while
(
cur
<
nums
.
size
()
-
2
)
{
left
=
cur
+
1
;
right
=
nums
.
size
()
-
1
;
int
n
;
while
(
left
<
right
)
{
n
=
nums
[
cur
]
+
nums
[
left
]
+
nums
[
right
];
if
(
abs
(
target
-
n
)
<
abs
(
target
-
closest
))
{
closest
=
n
;
}
if
(
n
==
target
)
{
break
;
}
____________________________
else
{
int
t
=
left
+
1
;
while
(
t
<
right
&&
nums
[
t
]
==
nums
[
left
])
t
++
;
left
=
t
;
}
}
int
t
=
cur
+
1
;
while
(
t
<
nums
.
size
()
&&
nums
[
t
]
==
nums
[
cur
])
t
++
;
cur
=
t
;
}
return
closest
;
}
};
```
## template
```
cpp
...
...
@@ -59,7 +107,13 @@ public:
## 答案
```
cpp
else
if
(
n
>
target
)
{
int
t
=
right
-
1
;
while
(
t
>
left
&&
nums
[
t
]
==
nums
[
right
])
t
--
;
right
=
t
;
}
```
## 选项
...
...
@@ -67,17 +121,35 @@ public:
### A
```
cpp
else
if
(
left
>
target
)
{
int
t
=
right
+
1
;
while
(
t
>
left
&&
nums
[
t
]
==
nums
[
right
])
t
--
;
right
=
t
;
}
```
### B
```
cpp
else
if
(
left
>
target
)
{
int
t
=
right
-
1
;
while
(
t
>
left
&&
nums
[
t
]
==
nums
[
right
])
t
--
;
right
=
t
+
1
;
}
```
### C
```
cpp
else
if
(
left
>
target
)
{
int
t
=
right
-
1
;
while
(
t
>
left
&&
nums
[
t
]
==
nums
[
right
])
t
--
;
right
=
t
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/77.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,39 @@
<p>
给定一个非负整数数组
<code>
nums
</code>
,你最初位于数组的
<strong>
第一个下标
</strong>
。
</p><p>
数组中的每个元素代表你在该位置可以跳跃的最大长度。
</p><p>
判断你是否能够到达最后一个下标。
</p><p>
</p><p><strong>
示例 1:
</strong></p><pre><strong>
输入:
</strong>
nums = [2,3,1,1,4]
<strong><br
/>
输出:
</strong>
true
<strong><br
/>
解释:
</strong>
可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
</pre><p><strong>
示例 2:
</strong></p><pre><strong>
输入:
</strong>
nums = [3,2,1,0,4]
<strong><br
/>
输出:
</strong>
false
<strong><br
/>
解释:
</strong>
无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li><code>
1
<
=
nums.length
<=
3
*
10<
sup
>
4
</sup></code></li>
<li><code>
0
<
=
nums
[
i
]
<=
10<
sup
>
5
</sup></code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static
inline
int
max
(
int
a
,
int
b
)
{
return
a
>
b
?
a
:
b
;
}
static
bool
canJump
(
int
*
nums
,
int
numsSize
)
{
int
i
,
pos
=
0
;
for
(
i
=
0
;
i
<
numsSize
-
1
;
i
++
)
{
___________________
pos
=
max
(
i
+
nums
[
i
],
pos
);
}
return
pos
>=
numsSize
-
1
;
}
int
main
(
int
argc
,
char
**
argv
)
{
int
i
,
count
=
argc
-
1
;
int
*
nums
=
malloc
(
count
*
sizeof
(
int
));
for
(
i
=
0
;
i
<
count
;
i
++
)
{
nums
[
i
]
=
atoi
(
argv
[
i
+
1
]);
}
printf
(
"%s
\n
"
,
canJump
(
nums
,
count
)
?
"true"
:
"false"
);
return
0
;
}
```
## template
```
cpp
...
...
@@ -41,7 +74,10 @@ int main(int argc, char **argv)
## 答案
```
cpp
if
(
pos
<
i
||
pos
>=
numsSize
-
1
)
{
break
;
}
```
## 选项
...
...
@@ -49,17 +85,26 @@ int main(int argc, char **argv)
### A
```
cpp
if
(
pos
<
i
||
pos
>=
numsSize
-
1
)
{
continue
;
}
```
### B
```
cpp
if
(
pos
<
i
&&
pos
>=
numsSize
-
1
)
{
break
;
}
```
### C
```
cpp
if
(
pos
<
i
&&
pos
>=
numsSize
-
1
)
{
continue
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/78.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,79 @@
<p>给定一个 <code>m x n</code> 二维字符网格 <code>board</code> 和一个字符串单词 <code>word</code> 。如果 <code>word</code> 存在于网格中,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p><p>单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word2.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word-1.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"<strong><br />输出:</strong>true</pre><p><strong>示例 3:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word3.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == board.length</code></li> <li><code>n = board[i].length</code></li> <li><code>1 <= m, n <= 6</code></li> <li><code>1 <= word.length <= 15</code></li> <li><code>board</code> 和 <code>word</code> 仅由大小写英文字母组成</li></ul><p> </p><p><strong>进阶:</strong>你可以使用搜索剪枝的技术来优化解决方案,使其在 <code>board</code> 更大的情况下可以更快解决问题?</p>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static
bool
dfs
(
char
*
word
,
char
**
board
,
bool
*
used
,
int
row
,
int
col
,
int
row_size
,
int
col_size
)
{
if
(
board
[
row
][
col
]
!=
*
word
)
{
return
false
;
}
used
[
row
*
col_size
+
col
]
=
true
;
if
(
*
(
word
+
1
)
==
'\0'
)
{
return
true
;
}
bool
result
=
false
;
if
(
row
>
0
&&
!
used
[(
row
-
1
)
*
col_size
+
col
])
{
result
=
dfs
(
word
+
1
,
board
,
used
,
row
-
1
,
col
,
row_size
,
col_size
);
}
if
(
!
result
&&
row
<
row_size
-
1
&&
!
used
[(
row
+
1
)
*
col_size
+
col
])
{
result
=
dfs
(
word
+
1
,
board
,
used
,
row
+
1
,
col
,
row_size
,
col_size
);
}
if
(
!
result
&&
col
>
0
&&
!
used
[
row
*
col_size
+
col
-
1
])
{
result
=
dfs
(
word
+
1
,
board
,
used
,
row
,
col
-
1
,
row_size
,
col_size
);
}
if
(
!
result
&&
col
<
col_size
-
1
&&
!
used
[
row
*
col_size
+
col
+
1
])
{
__________________________
}
used
[
row
*
col_size
+
col
]
=
false
;
return
result
;
}
static
bool
exist
(
char
**
board
,
int
boardRowSize
,
int
boardColSize
,
char
*
word
)
{
int
i
,
j
;
int
len
=
strlen
(
word
);
if
(
len
>
boardRowSize
*
boardColSize
)
{
return
false
;
}
bool
*
used
=
malloc
(
boardRowSize
*
boardColSize
);
for
(
i
=
0
;
i
<
boardRowSize
;
i
++
)
{
for
(
j
=
0
;
j
<
boardColSize
;
j
++
)
{
memset
(
used
,
false
,
boardRowSize
*
boardColSize
);
if
(
dfs
(
word
,
board
,
used
,
i
,
j
,
boardRowSize
,
boardColSize
))
{
return
true
;
}
}
}
return
false
;
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
<
3
)
{
fprintf
(
stderr
,
"Usage: ./test word row1 row2...
\n
"
);
exit
(
-
1
);
}
printf
(
"%s
\n
"
,
exist
(
argv
+
2
,
argc
-
2
,
strlen
(
argv
[
2
]),
argv
[
1
])
?
"true"
:
"false"
);
return
0
;
}
```
## template
```
cpp
...
...
@@ -78,7 +151,7 @@ int main(int argc, char **argv)
## 答案
```
cpp
result
=
dfs
(
word
+
1
,
board
,
used
,
row
,
col
+
1
,
row_size
,
col_size
);
```
## 选项
...
...
@@ -86,17 +159,17 @@ int main(int argc, char **argv)
### A
```
cpp
result
=
dfs
(
word
-
1
,
board
,
used
,
row
,
col
+
1
,
row_size
,
col_size
);
```
### B
```
cpp
result
=
dfs
(
word
+
1
,
board
,
used
,
row
,
col
-
1
,
row_size
,
col_size
);
```
### C
```
cpp
result
=
dfs
(
word
-
1
,
board
,
used
,
row
,
col
-
1
,
row_size
,
col_size
);
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/79.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -36,6 +36,47 @@
</ul>
</div>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
struct
TreeNode
{
int
val
;
TreeNode
*
left
;
TreeNode
*
right
;
TreeNode
()
:
val
(
0
),
left
(
nullptr
),
right
(
nullptr
)
{}
TreeNode
(
int
x
)
:
val
(
x
),
left
(
nullptr
),
right
(
nullptr
)
{}
TreeNode
(
int
x
,
TreeNode
*
left
,
TreeNode
*
right
)
:
val
(
x
),
left
(
left
),
right
(
right
)
{}
};
class
Solution
{
public:
bool
isValidBST
(
TreeNode
*
root
)
{
stack
<
TreeNode
*>
stk
;
int
prev
=
INT_MIN
;
bool
first
=
true
;
while
(
!
stk
.
empty
()
||
root
!=
nullptr
)
{
if
(
root
!=
nullptr
)
{
stk
.
push
(
root
);
root
=
root
->
left
;
}
else
{
root
=
stk
.
top
();
stk
.
pop
();
_______________________
}
}
return
true
;
}
};
```
## template
```
cpp
...
...
@@ -86,7 +127,13 @@ public:
## 答案
```
cpp
if
(
!
first
&&
prev
>=
root
->
val
)
{
return
false
;
}
first
=
false
;
prev
=
root
->
val
;
root
=
root
->
right
;
```
## 选项
...
...
@@ -94,17 +141,35 @@ public:
### A
```
cpp
if
(
first
&&
prev
>=
root
->
val
)
{
return
false
;
}
first
=
false
;
prev
=
root
->
val
;
root
=
root
->
right
;
```
### B
```
cpp
if
(
first
||
prev
>=
root
->
val
)
{
return
false
;
}
first
=
false
;
prev
=
root
->
val
;
root
=
root
->
right
;
```
### C
```
cpp
if
(
!
first
||
prev
>=
root
->
val
)
{
return
false
;
}
first
=
false
;
prev
=
root
->
val
;
root
=
root
->
right
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/80.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,62 @@
<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 <strong>没有重复出现</strong><em> </em>的数字。</p><p>返回同样按升序排列的结果链表。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist1.jpg" style="width: 500px; height: 142px;" /><pre><strong>输入:</strong>head = [1,2,3,3,4,4,5]<strong><br />输出:</strong>[1,2,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist2.jpg" style="width: 500px; height: 205px;" /><pre><strong>输入:</strong>head = [1,1,1,2,3]<strong><br />输出:</strong>[2,3]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点数目在范围 <code>[0, 300]</code> 内</li> <li><code>-100 <= Node.val <= 100</code></li> <li>题目数据保证链表已经按升序排列</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
struct
ListNode
{
int
val
;
struct
ListNode
*
next
;
};
struct
ListNode
*
deleteDuplicates
(
struct
ListNode
*
head
)
{
struct
ListNode
dummy
;
struct
ListNode
*
p
,
*
q
,
*
prev
;
prev
=
&
dummy
;
dummy
.
next
=
head
;
p
=
q
=
head
;
while
(
p
!=
NULL
)
{
_____________________
}
return
dummy
.
next
;
}
int
main
(
int
argc
,
char
**
argv
)
{
int
i
;
struct
ListNode
*
head
=
NULL
;
struct
ListNode
*
prev
=
NULL
;
struct
ListNode
*
p
;
for
(
i
=
0
;
i
<
argc
-
1
;
i
++
)
{
p
=
malloc
(
sizeof
(
*
p
));
p
->
val
=
atoi
(
argv
[
i
+
1
]);
p
->
next
=
NULL
;
if
(
head
==
NULL
)
{
head
=
p
;
prev
=
head
;
}
else
{
prev
->
next
=
p
;
prev
=
p
;
}
}
p
=
deleteDuplicates
(
head
);
while
(
p
!=
NULL
)
{
printf
(
"%d "
,
p
->
val
);
p
=
p
->
next
;
}
printf
(
"
\n
"
);
return
0
;
}
```
## template
```
cpp
...
...
@@ -73,7 +129,19 @@ int main(int argc, char **argv)
## 答案
```
cpp
while
(
q
!=
NULL
&&
q
->
val
==
p
->
val
)
{
q
=
q
->
next
;
}
if
(
p
->
next
==
q
)
{
prev
=
p
;
}
else
{
prev
->
next
=
q
;
}
p
=
q
;
```
## 选项
...
...
@@ -81,17 +149,44 @@ int main(int argc, char **argv)
### A
```
cpp
while
(
q
!=
NULL
&&
q
->
val
==
p
->
val
)
{
q
=
q
->
next
;
}
if
(
p
->
next
==
q
)
{
prev
=
p
;
}
else
{
prev
->
next
=
q
;
}
```
### B
```
cpp
while
(
q
!=
NULL
&&
q
->
val
==
p
->
val
)
{
q
=
q
->
next
;
}
if
(
p
->
next
==
q
)
{
prev
=
p
;
}
p
=
q
;
```
### C
```
cpp
while
(
q
!=
NULL
&&
q
->
val
==
p
->
val
)
{
q
=
q
->
next
;
}
if
(
p
->
next
==
q
)
{
prev
->
next
=
q
;
}
p
=
q
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/81.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -51,6 +51,52 @@ P I
</ul>
</div>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
class
Solution
{
public:
string
convert
(
string
s
,
int
numRows
)
{
if
(
numRows
==
1
)
return
s
;
int
len
=
s
.
size
();
if
(
len
<=
numRows
)
return
s
;
int
cycle_len
=
2
*
numRows
-
2
;
int
full_cycles
=
len
/
cycle_len
;
int
left
=
len
%
cycle_len
;
string
r
;
int
i
;
for
(
i
=
0
;
i
<
full_cycles
;
++
i
)
{
r
+=
s
[
i
*
cycle_len
];
}
if
(
left
)
r
+=
s
[
i
*
cycle_len
];
for
(
i
=
0
;
i
<
numRows
-
2
;
++
i
)
{
int
j
;
for
(
j
=
0
;
j
<
full_cycles
;
++
j
)
{
r
+=
s
[
j
*
cycle_len
+
i
+
1
];
r
+=
s
[
j
*
cycle_len
+
i
+
1
+
cycle_len
-
2
*
(
i
+
1
)];
}
if
(
left
)
{
_____________________
}
}
for
(
i
=
0
;
i
<
full_cycles
;
++
i
)
r
+=
s
[
i
*
cycle_len
+
numRows
-
1
];
if
(
left
>=
numRows
)
r
+=
s
[
i
*
cycle_len
+
numRows
-
1
];
return
r
;
}
};
```
## template
```
cpp
...
...
@@ -103,7 +149,10 @@ public:
## 答案
```
cpp
if
(
j
*
cycle_len
+
i
+
1
<
len
)
r
+=
s
[
j
*
cycle_len
+
i
+
1
];
if
(
j
*
cycle_len
+
i
+
1
+
cycle_len
-
2
*
(
i
+
1
)
<
len
)
r
+=
s
[
j
*
cycle_len
+
i
+
1
+
cycle_len
-
2
*
(
i
+
1
)];
```
## 选项
...
...
@@ -111,17 +160,26 @@ public:
### A
```
cpp
if
(
j
*
cycle_len
+
i
+
1
<
len
)
r
+=
s
[
j
*
cycle_len
+
i
];
if
(
j
*
cycle_len
+
i
+
cycle_len
-
2
*
(
i
+
1
)
<
len
)
r
+=
s
[
j
*
cycle_len
+
i
+
cycle_len
-
2
*
(
i
+
1
)];
```
### B
```
cpp
if
(
j
*
cycle_len
+
i
+
1
<
len
)
r
+=
s
[
j
*
cycle_len
+
i
+
1
];
if
(
j
*
cycle_len
+
i
+
1
+
cycle_len
-
1
*
(
i
+
1
)
<
len
)
r
+=
s
[
j
*
cycle_len
+
i
+
1
+
cycle_len
-
1
*
(
i
+
1
)];
```
### C
```
cpp
if
(
j
*
cycle_len
+
i
-
1
<
len
)
r
+=
s
[
j
*
cycle_len
+
i
+
1
];
if
(
j
*
cycle_len
+
i
-
1
+
cycle_len
-
1
*
(
i
+
1
)
<
len
)
r
+=
s
[
j
*
cycle_len
+
i
-
1
+
cycle_len
-
1
*
(
i
+
1
)];
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/82.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -13,6 +13,51 @@
<p><strong>
示例
2:
</strong></p>
<pre><strong>
输入:
</strong>
candidates =
[2,5,2,1,2], target =
5,
<strong><br
/>
所求解集为:
</strong>
[[1,2,2],[5]]
</pre>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
vector
<
vector
<
int
>>
combinationSum2
(
vector
<
int
>
&
candidates
,
int
target
)
{
vector
<
vector
<
int
>>
res
;
sort
(
candidates
.
begin
(),
candidates
.
end
());
dfs
(
candidates
,
0
,
target
,
res
);
return
res
;
}
private:
vector
<
int
>
stack
;
void
dfs
(
vector
<
int
>
&
candidates
,
int
start
,
int
target
,
vector
<
vector
<
int
>>
&
res
)
{
if
(
target
<
0
)
{
return
;
}
else
if
(
target
==
0
)
{
res
.
push_back
(
stack
);
}
else
{
int
last
=
INT_MIN
;
for
(
int
i
=
start
;
i
<
candidates
.
size
();
i
++
)
{
if
(
last
!=
candidates
[
i
])
{
stack
.
push_back
(
candidates
[
i
]);
_________________________________
stack
.
pop_back
();
}
last
=
candidates
[
i
];
}
}
}
};
```
## template
```
cpp
...
...
@@ -61,7 +106,7 @@ private:
## 答案
```
cpp
dfs
(
candidates
,
i
+
1
,
target
-
candidates
[
i
],
res
);
```
## 选项
...
...
@@ -69,17 +114,17 @@ private:
### A
```
cpp
dfs
(
candidates
,
i
-
1
,
target
-
candidates
[
i
],
res
);
```
### B
```
cpp
dfs
(
candidates
,
i
,
target
+
candidates
[
i
],
res
);
```
### C
```
cpp
dfs
(
candidates
,
i
+
1
,
target
+
candidates
[
i
],
res
);
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/83.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,29 @@
<p>给你一个正整数 <code>n</code> ,生成一个包含 <code>1</code> 到 <code>n<sup>2</sup></code> 所有元素,且元素按顺时针顺序螺旋排列的 <code>n x n</code> 正方形矩阵 <code>matrix</code> 。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0059.Spiral%20Matrix%20II/images/spiraln.jpg" style="width: 242px; height: 242px;" /><pre><strong>输入:</strong>n = 3<strong><br />输出:</strong>[[1,2,3],[8,9,4],[7,6,5]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>[[1]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 20</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
vector
<
vector
<
int
>>
generateMatrix
(
int
n
)
{
vector
<
vector
<
int
>>
ans
(
n
,
vector
<
int
>
(
n
,
0
));
int
i
,
j
=
0
,
time
=
0
,
cnt
=
1
;
//time记录第几圈
ans
[
0
][
0
]
=
1
;
while
(
cnt
<
n
*
n
)
{
___________________
time
++
;
}
return
ans
;
}
};
```
## template
```
cpp
...
...
@@ -12,50 +35,22 @@ class Solution
public:
vector
<
vector
<
int
>>
generateMatrix
(
int
n
)
{
vector
<
vector
<
int
>>
matrix
(
n
,
vector
<
int
>
(
n
));
int
direction
=
0
;
int
hor_top
=
0
;
int
hor_bottom
=
n
-
1
;
int
ver_left
=
0
;
int
ver_right
=
n
-
1
;
int
num
=
0
;
while
(
num
<
n
*
n
)
vector
<
vector
<
int
>>
ans
(
n
,
vector
<
int
>
(
n
,
0
));
int
i
,
j
=
0
,
time
=
0
,
cnt
=
1
;
//time记录第几圈
ans
[
0
][
0
]
=
1
;
while
(
cnt
<
n
*
n
)
{
switch
(
direction
)
{
case
0
:
for
(
int
i
=
ver_left
;
i
<=
ver_right
;
i
++
)
{
matrix
[
hor_top
][
i
]
=
++
num
;
}
hor_top
++
;
break
;
case
1
:
for
(
int
i
=
hor_top
;
i
<=
hor_bottom
;
i
++
)
{
matrix
[
i
][
ver_right
]
=
++
num
;
}
ver_right
--
;
break
;
case
2
:
for
(
int
i
=
ver_right
;
i
>=
ver_left
;
i
--
)
{
matrix
[
hor_bottom
][
i
]
=
++
num
;
}
hor_bottom
--
;
break
;
case
3
:
for
(
int
i
=
hor_bottom
;
i
>=
hor_top
;
i
--
)
{
matrix
[
i
][
ver_left
]
=
++
num
;
}
ver_left
++
;
break
;
}
direction
++
;
direction
%=
4
;
for
(
i
=
time
,
j
++
;
j
<
n
-
time
&&
cnt
<
n
*
n
;
j
++
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
j
--
,
i
++
;
i
<
n
-
time
&&
cnt
<
n
*
n
;
i
++
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
i
--
,
j
--
;
j
>=
time
&&
cnt
<
n
*
n
;
j
--
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
j
++
,
i
--
;
i
>
time
&&
cnt
<
n
*
n
;
i
--
)
ans
[
i
][
j
]
=
++
cnt
;
time
++
;
}
return
matrix
;
return
ans
;
}
};
```
...
...
@@ -63,7 +58,14 @@ public:
## 答案
```
cpp
for
(
i
=
time
,
j
++
;
j
<
n
-
time
&&
cnt
<
n
*
n
;
j
++
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
j
--
,
i
++
;
i
<
n
-
time
&&
cnt
<
n
*
n
;
i
++
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
i
--
,
j
--
;
j
>=
time
&&
cnt
<
n
*
n
;
j
--
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
j
++
,
i
--
;
i
>
time
&&
cnt
<
n
*
n
;
i
--
)
ans
[
i
][
j
]
=
++
cnt
;
```
## 选项
...
...
@@ -71,17 +73,38 @@ public:
### A
```
cpp
for
(
i
=
time
,
j
++
;
j
<
n
-
time
&&
cnt
<
n
*
n
;
j
++
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
j
--
,
i
++
;
i
<
n
-
time
&&
cnt
<
n
*
n
;
i
++
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
i
++
,
j
--
;
j
>=
time
&&
cnt
<
n
*
n
;
j
--
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
j
--
,
i
++
;
i
>
time
&&
cnt
<
n
*
n
;
i
--
)
ans
[
i
][
j
]
=
++
cnt
;
```
### B
```
cpp
for
(
i
=
time
,
j
--
;
j
<
n
-
time
&&
cnt
<
n
*
n
;
j
++
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
j
--
,
i
++
;
i
<
n
-
time
&&
cnt
<
n
*
n
;
i
++
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
i
--
,
j
++
;
j
>=
time
&&
cnt
<
n
*
n
;
j
--
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
j
++
,
i
--
;
i
>
time
&&
cnt
<
n
*
n
;
i
--
)
ans
[
i
][
j
]
=
++
cnt
;
```
### C
```
cpp
for
(
i
=
time
,
j
++
;
j
<
n
-
time
&&
cnt
<
n
*
n
;
j
++
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
j
--
,
i
++
;
i
<
n
-
time
&&
cnt
<
n
*
n
;
i
++
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
i
--
,
j
++
;
j
>=
time
&&
cnt
<
n
*
n
;
j
--
)
ans
[
i
][
j
]
=
++
cnt
;
for
(
j
--
,
i
--
;
i
>
time
&&
cnt
<
n
*
n
;
i
--
)
ans
[
i
][
j
]
=
++
cnt
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/84.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,36 @@
<p>给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。</p><p><strong>你不能只是单纯的改变节点内部的值</strong>,而是需要实际的进行节点交换。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0024.Swap%20Nodes%20in%20Pairs/images/swap_ex1.jpg" style="width: 422px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4]<strong><br />输出:</strong>[2,1,4,3]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1]<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点的数目在范围 <code>[0, 100]</code> 内</li> <li><code>0 <= Node.val <= 100</code></li></ul><p> </p><p><strong>进阶:</strong>你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)</p>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
struct
ListNode
{
int
val
;
ListNode
*
next
;
ListNode
()
:
val
(
0
),
next
(
nullptr
)
{}
ListNode
(
int
x
)
:
val
(
x
),
next
(
nullptr
)
{}
ListNode
(
int
x
,
ListNode
*
next
)
:
val
(
x
),
next
(
next
)
{}
};
class
Solution
{
public:
ListNode
*
swapPairs
(
ListNode
*
head
)
{
struct
ListNode
dummy
,
*
prev
=
&
dummy
,
*
p
=
head
;
dummy
.
next
=
head
;
while
(
p
!=
nullptr
&&
p
->
next
!=
nullptr
)
{
struct
ListNode
*
q
=
p
->
next
;
_____________________
}
return
dummy
.
next
;
}
};
```
## template
```
cpp
...
...
@@ -39,7 +69,11 @@ public:
## 答案
```
cpp
p
->
next
=
q
->
next
;
q
->
next
=
prev
->
next
;
prev
->
next
=
q
;
prev
=
p
;
p
=
p
->
next
;
```
## 选项
...
...
@@ -47,17 +81,29 @@ public:
### A
```
cpp
q
->
next
=
prev
->
next
;
p
->
next
=
q
->
next
;
prev
->
next
=
q
;
prev
=
p
;
p
=
p
->
next
;
```
### B
```
cpp
prev
->
next
=
q
;
p
->
next
=
q
->
next
;
q
->
next
=
prev
->
next
;
prev
=
p
;
p
=
p
->
next
;
```
### C
```
cpp
p
->
next
=
q
->
next
;
prev
->
next
=
q
;
prev
=
p
;
q
->
next
=
prev
->
next
;
p
=
p
->
next
;
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/85.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -2,6 +2,41 @@
<p>
给定一个包含红色、白色和蓝色,一共
<code>
n
</code><em>
</em>
个元素的数组,
<strong><a
href=
"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95"
target=
"_blank"
>
原地
</a></strong>
对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
</p><p>
此题中,我们使用整数
<code>
0
</code>
、
<code>
1
</code>
和
<code>
2
</code>
分别表示红色、白色和蓝色。
</p><ul></ul><p>
</p><p><strong>
示例 1:
</strong></p><pre><strong>
输入:
</strong>
nums = [2,0,2,1,1,0]
<strong><br
/>
输出:
</strong>
[0,0,1,1,2,2]
</pre><p><strong>
示例 2:
</strong></p><pre><strong>
输入:
</strong>
nums = [2,0,1]
<strong><br
/>
输出:
</strong>
[0,1,2]
</pre><p><strong>
示例 3:
</strong></p><pre><strong>
输入:
</strong>
nums = [0]
<strong><br
/>
输出:
</strong>
[0]
</pre><p><strong>
示例 4:
</strong></p><pre><strong>
输入:
</strong>
nums = [1]
<strong><br
/>
输出:
</strong>
[1]
</pre><p>
</p><p><strong>
提示:
</strong></p><ul>
<li><code>
n == nums.length
</code></li>
<li><code>
1
<
=
n
<=
300</
code
></li>
<li><code>
nums[i]
</code>
为
<code>
0
</code>
、
<code>
1
</code>
或
<code>
2
</code></li></ul><p>
</p><p><strong>
进阶:
</strong></p><ul>
<li>
你可以不使用代码库中的排序函数来解决这道题吗?
</li>
<li>
你能想出一个仅使用常数空间的一趟扫描算法吗?
</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
void
sortColors
(
vector
<
int
>
&
nums
)
{
int
i
=
0
,
j
=
nums
.
size
()
-
1
;
while
(
i
<
j
)
{
if
(
nums
[
i
]
==
0
)
{
i
++
;
continue
;
}
if
(
nums
[
j
]
!=
0
)
{
j
--
;
continue
;
}
swap
(
nums
[
i
],
nums
[
j
]);
}
j
=
nums
.
size
()
-
1
;
while
(
i
<
j
)
{
_____________________
swap
(
nums
[
i
],
nums
[
j
]);
}
}
};
```
## template
```
cpp
...
...
@@ -49,7 +84,16 @@ public:
## 答案
```
cpp
if
(
nums
[
i
]
==
1
)
{
i
++
;
continue
;
}
if
(
nums
[
j
]
!=
1
)
{
j
--
;
continue
;
}
```
## 选项
...
...
@@ -57,17 +101,44 @@ public:
### A
```
cpp
if
(
nums
[
i
]
!=
1
)
{
i
++
;
continue
;
}
if
(
nums
[
j
]
==
1
)
{
j
--
;
continue
;
}
```
### B
```
cpp
if
(
nums
[
i
]
==
1
)
{
i
++
;
break
;
}
if
(
nums
[
j
]
!=
1
)
{
j
--
;
break
;
}
```
### C
```
cpp
if
(
nums
[
i
]
!=
1
)
{
i
++
;
break
;
}
if
(
nums
[
j
]
==
1
)
{
j
--
;
break
;
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/86.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -24,6 +24,90 @@
<li><code>
obstacleGrid
[
i
][
j
]
</code>
为
<code>
0
</code>
或
<code>
1
</code></li>
</ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <stdio.h>
#include <stdlib.h>
static
int
uniquePathsWithObstacles
(
int
**
obstacleGrid
,
int
obstacleGridRowSize
,
int
obstacleGridColSize
)
{
int
row
,
col
;
int
reset
=
0
;
for
(
row
=
0
;
row
<
obstacleGridRowSize
;
row
++
)
{
if
(
reset
)
{
obstacleGrid
[
row
][
0
]
=
1
;
}
else
{
if
(
obstacleGrid
[
row
][
0
]
==
1
)
{
reset
=
1
;
}
}
}
reset
=
0
;
for
(
col
=
0
;
col
<
obstacleGridColSize
;
col
++
)
{
if
(
reset
)
{
obstacleGrid
[
0
][
col
]
=
1
;
}
else
{
if
(
obstacleGrid
[
0
][
col
]
==
1
)
{
reset
=
1
;
}
}
}
for
(
row
=
0
;
row
<
obstacleGridRowSize
;
row
++
)
{
int
*
line
=
obstacleGrid
[
row
];
for
(
col
=
0
;
col
<
obstacleGridColSize
;
col
++
)
{
line
[
col
]
^=
1
;
}
}
for
(
row
=
1
;
row
<
obstacleGridRowSize
;
row
++
)
{
int
*
last_line
=
obstacleGrid
[
row
-
1
];
int
*
line
=
obstacleGrid
[
row
];
for
(
col
=
1
;
col
<
obstacleGridColSize
;
col
++
)
{
________________________
}
}
return
obstacleGrid
[
obstacleGridRowSize
-
1
][
obstacleGridColSize
-
1
];
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
<
3
)
{
fprintf
(
stderr
,
"Usage: ./test m n
\n
"
);
exit
(
-
1
);
}
int
i
,
j
,
k
=
3
;
int
row_size
=
atoi
(
argv
[
1
]);
int
col_size
=
atoi
(
argv
[
2
]);
int
**
grids
=
malloc
(
row_size
*
sizeof
(
int
*
));
for
(
i
=
0
;
i
<
row_size
;
i
++
)
{
grids
[
i
]
=
malloc
(
col_size
*
sizeof
(
int
));
int
*
line
=
grids
[
i
];
for
(
j
=
0
;
j
<
col_size
;
j
++
)
{
line
[
j
]
=
atoi
(
argv
[
k
++
]);
printf
(
"%d "
,
line
[
j
]);
}
printf
(
"
\n
"
);
}
printf
(
"%d
\n
"
,
uniquePathsWithObstacles
(
grids
,
row_size
,
col_size
));
return
0
;
}
```
## template
```
cpp
...
...
@@ -114,7 +198,10 @@ int main(int argc, char **argv)
## 答案
```
cpp
if
(
line
[
col
]
!=
0
)
{
line
[
col
]
=
line
[
col
-
1
]
+
last_line
[
col
];
}
```
## 选项
...
...
@@ -122,17 +209,26 @@ int main(int argc, char **argv)
### A
```
cpp
if
(
line
[
col
]
==
0
)
{
line
[
col
]
=
line
[
col
-
1
]
+
last_line
[
col
];
}
```
### B
```
cpp
if
(
line
[
col
]
!=
0
)
{
line
[
col
]
=
line
[
col
+
1
]
+
last_line
[
col
];
}
```
### C
```
cpp
if
(
line
[
col
]
==
0
)
{
line
[
col
]
=
line
[
col
+
1
]
+
last_line
[
col
];
}
```
\ No newline at end of file
data/2.dailycode中阶/1.cpp/87.exercises/solution.md
浏览文件 @
31246570
...
...
@@ -49,6 +49,70 @@
<li><code>
board
[
i
][
j
]
</code>
是一位数字或者
<code>
'.'
</code></li>
</ul>
以下程序实现了这一功能,请你填补空白处内容:
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
bool
isValidSudoku
(
vector
<
vector
<
char
>>
&
board
)
{
for
(
int
i
=
0
;
i
<
board
.
size
();
i
++
)
{
vector
<
bool
>
mark
(
10
);
for
(
int
j
=
0
;
j
<
board
.
size
();
j
++
)
{
if
(
!
valid
(
board
,
mark
,
i
,
j
))
{
return
false
;
}
}
}
for
(
int
j
=
0
;
j
<
board
.
size
();
j
++
)
{
vector
<
bool
>
mark
(
10
);
for
(
int
i
=
0
;
i
<
board
.
size
();
i
++
)
{
if
(
!
valid
(
board
,
mark
,
i
,
j
))
{
return
false
;
}
}
}
for
(
int
k
=
0
;
k
<
board
.
size
();
k
++
)
{
int
sr
=
k
/
3
*
3
;
int
sc
=
(
k
%
3
)
*
3
;
vector
<
bool
>
mark
(
10
);
for
(
int
i
=
sr
;
i
<
sr
+
3
;
i
++
)
{
________________________
}
}
return
true
;
}
private:
bool
valid
(
vector
<
vector
<
char
>>
&
board
,
vector
<
bool
>
&
mark
,
int
i
,
int
j
)
{
if
(
board
[
i
][
j
]
!=
'.'
)
{
int
index
=
board
[
i
][
j
]
-
'0'
;
if
(
mark
[
index
])
{
return
false
;
}
else
{
mark
[
index
]
=
1
;
}
}
return
true
;
}
};
```
## template
```
cpp
...
...
@@ -122,7 +186,13 @@ private:
## 答案
```
cpp
for
(
int
j
=
sc
;
j
<
sc
+
3
;
j
++
)
{
if
(
!
valid
(
board
,
mark
,
i
,
j
))
{
return
false
;
}
}
```
## 选项
...
...
@@ -130,17 +200,35 @@ private:
### A
```
cpp
for
(
int
j
=
sc
;
j
<
sc
;
j
++
)
{
if
(
!
valid
(
board
,
mark
,
i
,
j
))
{
return
false
;
}
}
```
### B
```
cpp
for
(
int
j
=
sc
;
j
<
sc
+
3
;
j
++
)
{
if
(
valid
(
board
,
mark
,
i
,
j
))
{
return
false
;
}
}
```
### C
```
cpp
for
(
int
j
=
sc
;
j
<
sc
;
j
++
)
{
if
(
valid
(
board
,
mark
,
i
,
j
))
{
return
false
;
}
}
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录