提交 8b4f11f6 编写于 作者: 每日一练社区's avatar 每日一练社区

优化21道习题选项

上级 45858ce0
# 输入一个正整数n(代表图形的行数),输出如样例形式的图形 # 输入一个正整数n(代表图形的行数),输出如样例形式的图形
输入:5 输入:5
输出: 输出:
```json
A A
ABA ABA
ABCBA ABCBA
ABCDCBA ABCDCBA
ABCDEDCBA ABCDEDCBA
```
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int N;
cin >> N;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N - i; j++)
printf(" ");
______________________
printf("\n");
}
return 0;
}
```
## template ## template
...@@ -14,24 +39,31 @@ ...@@ -14,24 +39,31 @@
#include <stdio.h> #include <stdio.h>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() { int main()
int N; {
cin >> N; int N;
for (int i = 0; i < N; i++) cin >> N;
{ for (int i = 0; i < N; i++)
for (int j = 0; j < N - i; j++) printf(" "); {
for (int j = 0; j < i; j++) printf("%c", (char)(j + 'A')); for (int j = 0; j < N - i; j++)
for (int j = i; j >= 0; j--) printf("%c", (char)(j + 'A')); printf(" ");
printf("\n"); for (int j = 0; j < i; j++)
} printf("%c", (char)(j + 'A'));
return 0; for (int j = i; j >= 0; j--)
printf("%c", (char)(j + 'A'));
printf("\n");
}
return 0;
} }
``` ```
## 答案 ## 答案
```cpp ```cpp
for (int j = 0; j < i; j++)
printf("%c", (char)(j + 'A'));
for (int j = i; j >= 0; j--)
printf("%c", (char)(j + 'A'));
``` ```
## 选项 ## 选项
...@@ -39,17 +71,26 @@ int main() { ...@@ -39,17 +71,26 @@ int main() {
### A ### A
```cpp ```cpp
for (int j = 0; j < i; j--)
printf("%c", (char)(j + 'A'));
for (int j = i; j = 0; j++)
printf("%c", (char)(j + 'A'));
``` ```
### B ### B
```cpp ```cpp
for (int j = 0; j < i; j--)
printf("%c", (char)(j + 'A'));
for (int j = i; j <= 0; j--)
printf("%c", (char)(j + 'A'));
``` ```
### C ### C
```cpp ```cpp
for (int j = 0; j < i; j--)
printf("%c", (char)(j + 'A'));
for (int j = i; j <= 0; j--)
printf("%c", (char)(j - 'A'));
``` ```
\ No newline at end of file
...@@ -14,37 +14,70 @@ ...@@ -14,37 +14,70 @@
<pre> <pre>
<code>2</code></pre> <code>2</code></pre>
## template 以下程序实现了这一功能,请你填补空白处内容:
```cpp ```cpp
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int main() int main()
{ {
int n,m; int n, m;
scanf("%d",&n); scanf("%d", &n);
if(n<1||n>10){ if (n < 1 || n > 10)
printf("1≤n≤10"); {
return 0; printf("1≤n≤10");
} return 0;
int *a = (int*)malloc(sizeof(int)*n);
for(int i=n-1; i>=0; i-- ){
scanf("%d",&a[i]);
} }
scanf("%d",&m); int *a = (int *)malloc(sizeof(int) * n);
if(m<0||m>=n){ __________________
printf("0≤m<n"); scanf("%d", &m);
return 0; if (m < 0 || m >= n)
} {
printf("%d",a[m]); printf("0≤m<n");
return 0; return 0;
}
printf("%d", a[m]);
return 0;
} }
``` ```
## 答案 ## template
```cpp ```cpp
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, m;
scanf("%d", &n);
if (n < 1 || n > 10)
{
printf("1≤n≤10");
return 0;
}
int *a = (int *)malloc(sizeof(int) * n);
for (int i = n - 1; i >= 0; i--)
{
scanf("%d", &a[i]);
}
scanf("%d", &m);
if (m < 0 || m >= n)
{
printf("0≤m<n");
return 0;
}
printf("%d", a[m]);
return 0;
}
```
## 答案
```cpp
for (int i = n - 1; i >= 0; i--)
{
scanf("%d", &a[i]);
}
``` ```
## 选项 ## 选项
...@@ -52,17 +85,26 @@ int main() ...@@ -52,17 +85,26 @@ int main()
### A ### A
```cpp ```cpp
for (int i = n; i >= 0; i--)
{
scanf("%d", &a[i]);
}
``` ```
### B ### B
```cpp ```cpp
for (int i = n; i > 0; i--)
{
scanf("%d", &a[i]);
}
``` ```
### C ### C
```cpp ```cpp
for (int i = n - 1; i < 0; i--)
{
scanf("%d", &a[i]);
}
``` ```
\ No newline at end of file
...@@ -2,47 +2,85 @@ ...@@ -2,47 +2,85 @@
在数组中有n个字符,使前面各字符顺序向后移动m个位置,并使最后m个字符变成最前面的 m 个字符 在数组中有n个字符,使前面各字符顺序向后移动m个位置,并使最后m个字符变成最前面的 m 个字符
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <iostream>
using namespace std;
void funShift(int m, char *p, int n)
{
char c;
for (int j = 0; j < m; j++)
{
_____________________
}
}
int main()
{
int i, m, n;
cin >> m >> n;
char *p = new char[n + 1];
p[n] = 0;
for (i = 0; i < n; ++i)
cin >> p[i];
funShift(m, p, n);
for (i = 0; i < n; ++i)
cout << p[i] << ' ';
cout << endl;
delete[] p;
getchar();
getchar();
return 0;
}
```
## template ## template
```cpp ```cpp
#include <iostream> #include <iostream>
using namespace std; using namespace std;
void funShift(int m,char *p,int n) void funShift(int m, char *p, int n)
{ {
char c; char c;
for (int j = 0; j < m;j++) for (int j = 0; j < m; j++)
{ {
c = p[n-1]; c = p[n - 1];
for (int i = n-1; i > 0; i--) for (int i = n - 1; i > 0; i--)
{ {
p[i] = p[i-1]; p[i] = p[i - 1];
} }
p[0] = c; p[0] = c;
} }
} }
int main() int main()
{ {
int i,m,n; int i, m, n;
cin >> m >> n; cin >> m >> n;
char *p =new char[n+1]; char *p = new char[n + 1];
p[n] = 0; p[n] = 0;
for(i = 0; i < n; ++i) for (i = 0; i < n; ++i)
cin >> p[i]; cin >> p[i];
funShift(m,p,n); funShift(m, p, n);
for(i = 0; i < n; ++i) for (i = 0; i < n; ++i)
cout << p[i] << ' '; cout << p[i] << ' ';
cout << endl; cout << endl;
delete [] p; delete[] p;
getchar(); getchar();
getchar(); getchar();
return 0; return 0;
} }
``` ```
## 答案 ## 答案
```cpp ```cpp
c = p[n - 1];
for (int i = n - 1; i > 0; i--)
{
p[i] = p[i - 1];
}
p[0] = c;
``` ```
## 选项 ## 选项
...@@ -50,17 +88,32 @@ int main() ...@@ -50,17 +88,32 @@ int main()
### A ### A
```cpp ```cpp
c = p[n];
for (int i = n - 1; i > 0; i--)
{
p[i] = p[i - 1];
}
p[0] = c;
``` ```
### B ### B
```cpp ```cpp
c = p[n - 1];
for (int i = n - 1; i > 0; i--)
{
p[i] = p[i + 1];
}
p[0] = c;
``` ```
### C ### C
```cpp ```cpp
c = p[n];
for (int i = n - 1; i > 0; i--)
{
p[i] = p[i + 1];
}
p[0] = c;
``` ```
\ No newline at end of file
# 求前n个素数之和 # 求前n个素数之和
题目描述 **题目描述**
求前n个素数的和。 求前n个素数的和。
例如,前5个素数是2、3、5、7、11,它们的和是28。 例如,前5个素数是2、3、5、7、11,它们的和是28。
输入
**输入**
一个整数n,1<=n<=1000。 一个整数n,1<=n<=1000。
输出
**输出**
前n个素数的和 前n个素数的和
样例输入
**样例输入**
```json
5 5
样例输出 ```
**样例输出**
```json
28 28
提示 ```
**提示**
第1000个素数是7919。 第1000个素数是7919。
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <iostream>
using namespace std;
int main()
{
int n, i, j, sum, a;
cin >> n;
a = 0;
i = 2;
sum = 0;
while (a < n)
{
__________________
if (j == i)
{
sum += i;
++a;
}
++i;
}
cout << sum;
}
```
## template ## template
...@@ -22,30 +64,33 @@ ...@@ -22,30 +64,33 @@
using namespace std; using namespace std;
int main() int main()
{ {
int n,i,j,sum,a; int n, i, j, sum, a;
cin>>n; cin >> n;
a = 0; a = 0;
i = 2; i = 2;
sum=0; sum = 0;
while(a<n){ while (a < n)
for(j=2;j<=i;j++) {
if(i%j == 0) for (j = 2; j <= i; j++)
break; if (i % j == 0)
if(j == i) break;
{ if (j == i)
sum += i; {
++a; sum += i;
} ++a;
++i; }
} ++i;
cout<<sum; }
cout << sum;
} }
``` ```
## 答案 ## 答案
```cpp ```cpp
for (j = 2; j <= i; j++)
if (i % j == 0)
break;
``` ```
## 选项 ## 选项
...@@ -53,17 +98,23 @@ int main() ...@@ -53,17 +98,23 @@ int main()
### A ### A
```cpp ```cpp
for (j = 1; j <= i; j++)
if (i % j == 0)
break;
``` ```
### B ### B
```cpp ```cpp
for (j = 1; j <= i; j++)
if (i / j == 0)
break;
``` ```
### C ### C
```cpp ```cpp
for (j = 2; j <= i; j++)
if (i / j == 0)
break;
``` ```
\ No newline at end of file
...@@ -3,6 +3,48 @@ ...@@ -3,6 +3,48 @@
<p>请编写一个fun函数&#xff0c;实现如下功能&#xff1a;将一个字符串中第一个连续数字转换成整数&#xff0c;作为函数值返回&#xff0c;否则返回0&#xff08;程序的输入输出要有提示&#xff09; <p>请编写一个fun函数&#xff0c;实现如下功能&#xff1a;将一个字符串中第一个连续数字转换成整数&#xff0c;作为函数值返回&#xff0c;否则返回0&#xff08;程序的输入输出要有提示&#xff09;
比如&#xff1a;字符串中的内容为:&#34;abc123 def45gh&#34;&#xff0c;则函数的返回值为123。</p> 比如&#xff1a;字符串中的内容为:&#34;abc123 def45gh&#34;&#xff0c;则函数的返回值为123。</p>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <iostream>
#include <string>
using namespace std;
int fun(string str);
int main(void)
{
string str = "abc123 def45gh";
cout << fun(str);
return 0;
}
int fun(string str)
{
int index = -1;
int score = 0;
for (int i = 0; i < str.length(); i++)
{
_____________________
}
if (index == -1)
{
return score;
}
score = str[index] - '0';
for (int i = index + 1; i < str.length(); i++)
{
if (str[i] >= 48 && str[i] <= 57)
{
score *= 10;
score += str[i] - '0';
}
else
{
break;
}
}
return score;
}
```
## template ## template
```cpp ```cpp
...@@ -10,40 +52,53 @@ ...@@ -10,40 +52,53 @@
#include <string> #include <string>
using namespace std; using namespace std;
int fun(string str); int fun(string str);
int main(void) { int main(void)
string str = "abc123 def45gh"; {
cout << fun(str); string str = "abc123 def45gh";
return 0; cout << fun(str);
return 0;
} }
int fun(string str) { int fun(string str)
int index = -1; {
int score = 0; int index = -1;
for (int i = 0; i < str.length(); i++) { int score = 0;
if ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i + 1] >= 48 && str[i + 1] <= 57)) { for (int i = 0; i < str.length(); i++)
index = i; {
break; if ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i + 1] >= 48 && str[i + 1] <= 57))
} {
} index = i;
if (index == -1) { break;
return score; }
} }
score = str[index] - '0'; if (index == -1)
for (int i = index + 1; i < str.length(); i++) { {
if (str[i] >= 48 && str[i] <= 57) { return score;
score *= 10; }
score += str[i] - '0'; score = str[index] - '0';
} else { for (int i = index + 1; i < str.length(); i++)
break; {
} if (str[i] >= 48 && str[i] <= 57)
} {
return score; score *= 10;
score += str[i] - '0';
}
else
{
break;
}
}
return score;
} }
``` ```
## 答案 ## 答案
```cpp ```cpp
if ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i + 1] >= 48 && str[i + 1] <= 57))
{
index = i;
break;
}
``` ```
## 选项 ## 选项
...@@ -51,17 +106,29 @@ int fun(string str) { ...@@ -51,17 +106,29 @@ int fun(string str) {
### A ### A
```cpp ```cpp
if ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i + 1] >= 48 && str[i + 1] <= 57))
{
index = i;
continue;
}
``` ```
### B ### B
```cpp ```cpp
if ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i] >= 48 && str[i] <= 57))
{
index = i;
break;
}
``` ```
### C ### C
```cpp ```cpp
if ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i] >= 48 && str[i] <= 57))
{
index = i;
continue;
}
``` ```
\ No newline at end of file
# 计算sin(x) # 计算sin(x)
描述
计算sin(x)=x-x^3/3!+x^5/5!-X^7/7!+......,直到最后一项的绝对值小于10-7时停止计算。其中-2Π<=x<=2Π,^表示次方,如x^3表示x的3次方。 计算sin(x)=x-x^3/3!+x^5/5!-X^7/7!+......,直到最后一项的绝对值小于10-7时停止计算。其中-2Π<=x<=2Π,^表示次方,如x^3表示x的3次方。
输入
**输入**
一个实数x,-2Π<=x<=2Π 一个实数x,-2Π<=x<=2Π
输出
**输出**
sin(x)的值 sin(x)的值
输入样例 1
**输入样例**
```json
3.142 3.142
输出样例 1 ```
**输出样例**
```json
-0.000407347 -0.000407347
```
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <stdio.h>
#include <math.h>
double sin(double);
double nResult(double, double);
int main()
{
double x = 0;
scanf("%lf", &x);
printf("sin(%lf)=%lf\n", x, sin(x));
return 0;
}
double sin(double x)
{
int i = 0;
double result = 0, n = 0;
____________________
return result;
}
double nResult(double x, double n)
{
return n == 1 ? x : nResult(x, n - 1) * x / n;
}
```
## template ## template
```cpp ```cpp
#include<stdio.h> #include <stdio.h>
#include<math.h> #include <math.h>
double sin(double); double sin(double);
double nResult(double,double); double nResult(double, double);
int main() int main()
{ {
double x=0; double x = 0;
scanf("%lf",&x); scanf("%lf", &x);
printf("sin(%lf)=%lf\n",x,sin(x)); printf("sin(%lf)=%lf\n", x, sin(x));
return 0; return 0;
} }
double sin(double x) double sin(double x)
{ {
int i=0; int i = 0;
double result=0,n=0; double result = 0, n = 0;
while( fabs( n=nResult(x,2*++i-1) ) > 0e-7 ) while (fabs(n = nResult(x, 2 * ++i - 1)) > 0e-7)
result+=(i%2==1)?n:-n; result += (i % 2 == 1) ? n : -n;
return result; return result;
} }
double nResult(double x,double n) double nResult(double x, double n)
{ {
return n==1?x:nResult(x,n-1)*x/n; return n == 1 ? x : nResult(x, n - 1) * x / n;
} }
``` ```
## 答案 ## 答案
```cpp ```cpp
while (fabs(n = nResult(x, 2 * ++i - 1)) > 0e-7)
result += (i % 2 == 1) ? n : -n;
``` ```
## 选项 ## 选项
...@@ -51,17 +89,20 @@ double nResult(double x,double n) ...@@ -51,17 +89,20 @@ double nResult(double x,double n)
### A ### A
```cpp ```cpp
while (fabs(n = nResult(x, 2 * i - 1)) > 0e-7)
result += (i % 2 == 1) ? n : -n;
``` ```
### B ### B
```cpp ```cpp
while (fabs(n = nResult(x, 2 * i++ - 1)) > 0e-7)
result += (i % 2 == 1) ? n : -n;
``` ```
### C ### C
```cpp ```cpp
while (fabs(n = nResult(x, 2 * ++i + 1)) > 0e-7)
result += (i % 2 == 1) ? n : -n;
``` ```
\ No newline at end of file
...@@ -2,6 +2,29 @@ ...@@ -2,6 +2,29 @@
问题:猴子第一天摘了若干个桃子,吃了一半,不过瘾,又多吃了1个。第二天早上将剩余的桃子又吃掉一半,并且又多吃了1个。此后每天都是吃掉前一天剩下的一半零一个。到第n天再想吃时,发现只剩下1个桃子,问第一天它摘了多少桃子?为了加强交互性,由用户输入不同的天数n进行递推,即假设第n天的桃子数为1。同时还要增加对用户输入数据的合法性验证(如:不允许输入的天数是0和负数) 问题:猴子第一天摘了若干个桃子,吃了一半,不过瘾,又多吃了1个。第二天早上将剩余的桃子又吃掉一半,并且又多吃了1个。此后每天都是吃掉前一天剩下的一半零一个。到第n天再想吃时,发现只剩下1个桃子,问第一天它摘了多少桃子?为了加强交互性,由用户输入不同的天数n进行递推,即假设第n天的桃子数为1。同时还要增加对用户输入数据的合法性验证(如:不允许输入的天数是0和负数)
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <stdio.h>
int main()
{
int ret, day, i = 1, sum = 1;
while (1)
{
printf("Input days:\n");
ret = scanf("%d", &day);
if ((ret != 1) || (day <= 0))
{
fflush(stdin);
continue;
}
break;
}
__________________
printf("sum=%d\n", sum);
return 0;
}
```
## template ## template
...@@ -9,32 +32,36 @@ ...@@ -9,32 +32,36 @@
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int ret,day,i=1,sum=1; int ret, day, i = 1, sum = 1;
while (1) while (1)
{ {
printf("Input days:\n"); printf("Input days:\n");
ret=scanf("%d",&day); ret = scanf("%d", &day);
if ((ret!=1)||(day<=0)) if ((ret != 1) || (day <= 0))
{ {
fflush(stdin); fflush(stdin);
continue; continue;
} }
break; break;
} }
do do
{ {
sum=(sum+1)*2; sum = (sum + 1) * 2;
i++; i++;
}while(i<day); } while (i < day);
printf("sum=%d\n",sum); printf("sum=%d\n", sum);
return 0; return 0;
} }
``` ```
## 答案 ## 答案
```cpp ```cpp
do
{
sum = (sum + 1) * 2;
i++;
} while (i < day);
``` ```
## 选项 ## 选项
...@@ -42,17 +69,29 @@ int main() ...@@ -42,17 +69,29 @@ int main()
### A ### A
```cpp ```cpp
while (i < day)
{
sum = (sum + 1) * 2;
i++;
}
``` ```
### B ### B
```cpp ```cpp
while (i <= day)
{
sum = (sum + 1) * 2;
i++;
}
``` ```
### C ### C
```cpp ```cpp
do
{
sum = (sum + 1) * 2;
i++;
} while (i <= day);
``` ```
\ No newline at end of file
...@@ -2,6 +2,46 @@ ...@@ -2,6 +2,46 @@
<p>给你二叉搜索树的根节点 <code>root</code> ,该树中的两个节点被错误地交换。请在不改变其结构的情况下,恢复这棵树。</p><p><strong>进阶:</strong>使用 O(<em>n</em>) 空间复杂度的解法很容易实现。你能想出一个只使用常数空间的解决方案吗?</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0099.Recover%20Binary%20Search%20Tree/images/recover1.jpg" style="width: 422px; height: 302px;" /><pre><strong>输入:</strong>root = [1,3,null,null,2]<strong><br />输出:</strong>[3,1,null,null,2]<strong><br />解释:</strong>3 不能是 1 左孩子,因为 3 > 1 。交换 1 和 3 使二叉搜索树有效。</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0099.Recover%20Binary%20Search%20Tree/images/recover2.jpg" style="width: 581px; height: 302px;" /><pre><strong>输入:</strong>root = [3,1,4,null,null,2]<strong><br />输出:</strong>[2,1,4,null,null,3]<strong><br />解释:</strong>2 不能在 3 的右子树中,因为 2 < 3 。交换 2 和 3 使二叉搜索树有效。</pre><p> </p><p><strong>提示:</strong></p><ul> <li>树上节点的数目在范围 <code>[2, 1000]</code> 内</li> <li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li></ul> <p>给你二叉搜索树的根节点 <code>root</code> ,该树中的两个节点被错误地交换。请在不改变其结构的情况下,恢复这棵树。</p><p><strong>进阶:</strong>使用 O(<em>n</em>) 空间复杂度的解法很容易实现。你能想出一个只使用常数空间的解决方案吗?</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0099.Recover%20Binary%20Search%20Tree/images/recover1.jpg" style="width: 422px; height: 302px;" /><pre><strong>输入:</strong>root = [1,3,null,null,2]<strong><br />输出:</strong>[3,1,null,null,2]<strong><br />解释:</strong>3 不能是 1 左孩子,因为 3 > 1 。交换 1 和 3 使二叉搜索树有效。</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0099.Recover%20Binary%20Search%20Tree/images/recover2.jpg" style="width: 581px; height: 302px;" /><pre><strong>输入:</strong>root = [3,1,4,null,null,2]<strong><br />输出:</strong>[2,1,4,null,null,3]<strong><br />解释:</strong>2 不能在 3 的右子树中,因为 2 < 3 。交换 2 和 3 使二叉搜索树有效。</pre><p> </p><p><strong>提示:</strong></p><ul> <li>树上节点的数目在范围 <code>[2, 1000]</code> 内</li> <li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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:
void recoverTree(TreeNode *root)
{
dfs(root);
int tmp = p0_->val;
p0_->val = p1_->val;
p1_->val = tmp;
}
private:
int wrong_ = 0;
TreeNode *prev_ = nullptr;
TreeNode *p0_ = nullptr;
TreeNode *p1_ = nullptr;
void dfs(TreeNode *root)
{
if (root == nullptr || wrong_ == 2)
{
return;
}
_____________________
}
};
```
## template ## template
```cpp ```cpp
...@@ -59,7 +99,21 @@ private: ...@@ -59,7 +99,21 @@ private:
## 答案 ## 答案
```cpp ```cpp
dfs(root->left);
if (prev_ != nullptr && prev_->val > root->val)
{
if (++wrong_ == 1)
{
p0_ = prev_;
p1_ = root;
}
else if (wrong_ == 2)
{
p1_ = root;
}
}
prev_ = root;
dfs(root->right);
``` ```
## 选项 ## 选项
...@@ -67,17 +121,51 @@ private: ...@@ -67,17 +121,51 @@ private:
### A ### A
```cpp ```cpp
dfs(root->right);
if (prev_ != nullptr && prev_->val > root->val)
{
if (++wrong_ == 1)
{
p0_ = prev_;
p1_ = root;
}
else if (wrong_ == 2)
{
p1_ = root;
}
}
prev_ = root;
dfs(root->left);
``` ```
### B ### B
```cpp ```cpp
dfs(root->right);
if (prev_ != nullptr && prev_->val > root->val)
{
if (++wrong_ == 1)
{
p0_ = prev_;
p1_ = root;
}
}
prev_ = root;
dfs(root->left);
``` ```
### C ### C
```cpp ```cpp
dfs(root->left);
if (prev_ != nullptr && prev_->val > root->val)
{
if (++wrong_ == 1)
{
p0_ = prev_;
p1_ = root;
}
}
prev_ = root;
dfs(root->right);
``` ```
\ No newline at end of file
...@@ -2,6 +2,33 @@ ...@@ -2,6 +2,33 @@
<p>给你一个整数数组 <code>nums</code> ,数组中的元素 <strong>互不相同</strong> 。返回该数组所有可能的子集(幂集)。</p><p>解集 <strong>不能</strong> 包含重复的子集。你可以按 <strong>任意顺序</strong> 返回解集。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,3]<strong><br />输出:</strong>[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]</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> <li><code>nums</code> 中的所有元素 <strong>互不相同</strong></li></ul> <p>给你一个整数数组 <code>nums</code> ,数组中的元素 <strong>互不相同</strong> 。返回该数组所有可能的子集(幂集)。</p><p>解集 <strong>不能</strong> 包含重复的子集。你可以按 <strong>任意顺序</strong> 返回解集。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,3]<strong><br />输出:</strong>[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]</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> <li><code>nums</code> 中的所有元素 <strong>互不相同</strong></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> subsets(vector<int> &nums)
{
vector<vector<int>> res;
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);
for (int i = start; i < nums.size(); i++)
{
______________
}
}
};
```
## template ## template
```cpp ```cpp
...@@ -34,7 +61,9 @@ private: ...@@ -34,7 +61,9 @@ private:
## 答案 ## 答案
```cpp ```cpp
stack.push_back(nums[i]);
dfs(nums, i + 1, res);
stack.pop_back();
``` ```
## 选项 ## 选项
...@@ -42,17 +71,21 @@ private: ...@@ -42,17 +71,21 @@ private:
### A ### A
```cpp ```cpp
stack.push_back(nums[i]);
dfs(nums, i, res);
stack.pop_back();
``` ```
### B ### B
```cpp ```cpp
stack.push_back(nums[i]);
dfs(nums, i + 1, res);
``` ```
### C ### C
```cpp ```cpp
stack.push_back(nums[i]);
dfs(nums, i - 1, res);
``` ```
\ No newline at end of file
...@@ -20,6 +20,44 @@ ...@@ -20,6 +20,44 @@
<li>题目数据保证答案小于等于 <code>2 * 10<sup>9</sup></code></li> <li>题目数据保证答案小于等于 <code>2 * 10<sup>9</sup></code></li>
</ul> </ul>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <stdio.h>
#include <stdlib.h>
static int uniquePaths(int m, int n)
{
int row, col;
int *grids = malloc(m * n * sizeof(int));
for (col = 0; col < m; col++)
{
grids[col] = 1;
}
for (row = 0; row < n; row++)
{
grids[row * m] = 1;
}
for (row = 1; row < n; row++)
{
for (col = 1; col < m; col++)
{
______________________________
}
}
return grids[m * n - 1];
}
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage: ./test m n\n");
exit(-1);
}
printf("%d\n", uniquePaths(atoi(argv[1]), atoi(argv[2])));
return 0;
}
```
## template ## template
```cpp ```cpp
...@@ -61,7 +99,7 @@ int main(int argc, char **argv) ...@@ -61,7 +99,7 @@ int main(int argc, char **argv)
## 答案 ## 答案
```cpp ```cpp
grids[row * m + col] = grids[row * m + col - 1] + grids[(row - 1) * m + col];
``` ```
## 选项 ## 选项
...@@ -69,17 +107,17 @@ int main(int argc, char **argv) ...@@ -69,17 +107,17 @@ int main(int argc, char **argv)
### A ### A
```cpp ```cpp
grids[row * m + col] = grids[row * m + col] + grids[row * m + col];
``` ```
### B ### B
```cpp ```cpp
grids[row * m + col] = grids[row * m + col - 1] + grids[row * m + col + 1];
``` ```
### C ### C
```cpp ```cpp
grids[row * m + col] = grids[row * m + col + 1] + grids[row * m + col];
``` ```
\ No newline at end of file
...@@ -2,67 +2,94 @@ ...@@ -2,67 +2,94 @@
<p>给定一个仅包含数字 <code>2-9</code> 的字符串,返回所有它能表示的字母组合。答案可以按 <strong>任意顺序</strong> 返回。</p><p>给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0017.Letter%20Combinations%20of%20a%20Phone%20Number/images/17_telephone_keypad.png" style="width: 200px;" /></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>digits = "23"<strong><br />输出:</strong>["ad","ae","af","bd","be","bf","cd","ce","cf"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>digits = ""<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>digits = "2"<strong><br />输出:</strong>["a","b","c"]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= digits.length <= 4</code></li> <li><code>digits[i]</code> 是范围 <code>['2', '9']</code> 的一个数字。</li></ul> <p>给定一个仅包含数字 <code>2-9</code> 的字符串,返回所有它能表示的字母组合。答案可以按 <strong>任意顺序</strong> 返回。</p><p>给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0017.Letter%20Combinations%20of%20a%20Phone%20Number/images/17_telephone_keypad.png" style="width: 200px;" /></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>digits = "23"<strong><br />输出:</strong>["ad","ae","af","bd","be","bf","cd","ce","cf"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>digits = ""<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>digits = "2"<strong><br />输出:</strong>["a","b","c"]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= digits.length <= 4</code></li> <li><code>digits[i]</code> 是范围 <code>['2', '9']</code> 的一个数字。</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
class Solution {
public:
vector<string> str;
vector<string> ret;
string getStr( int x ) {
switch( x ) {
case 2: return "abc";
case 3: return "def";
case 4: return "ghi";
case 5: return "jkl";
case 6: return "mno";
case 7: return "pqrs";
case 8: return "tuv";
case 9: return "wxyz";
default: return "";
}
}
void dfs( string& ans, int k ) {
if ( k >= str.size() ) {
ret.push_back( ans );
return;
}
for ( auto& it : str[k] ) {
ans.push_back( it );
_____________________
ans.pop_back();
}
}
vector<string> letterCombinations(string digits) {
if ( !digits.size() ) return {};
for ( auto& it : digits )
str.push_back( getStr( it & 15 ) );
string ans = "";
dfs( ans, 0 );
return ret;
}
};
```
## template ## template
```cpp ```cpp
class Solution class Solution {
{
public: public:
vector<string> letterCombinations(string digits) vector<string> str;
{ vector<string> ret;
vector<string> nummap({" ", string getStr( int x ) {
"", switch( x ) {
"abc", case 2: return "abc";
"def", case 3: return "def";
"ghi", case 4: return "ghi";
"jkl", case 5: return "jkl";
"mno", case 6: return "mno";
"pqrs", case 7: return "pqrs";
"tuv", case 8: return "tuv";
"wxyz"}); case 9: return "wxyz";
vector<string> rs; default: return "";
vector<string> empty; }
if (digits.size() == 0) }
return empty; void dfs( string& ans, int k ) {
for (auto d : digits) if ( k >= str.size() ) {
{ ret.push_back( ans );
if (d == '0') return;
return empty; }
if (d == '1') for ( auto& it : str[k] ) {
return empty; ans.push_back( it );
auto &s = nummap[d - '0']; dfs( ans, k + 1 );
if (s.size() == 0) ans.pop_back();
continue; }
if (rs.size() == 0) }
for (auto c : s) vector<string> letterCombinations(string digits) {
{ if ( !digits.size() ) return {};
string t; for ( auto& it : digits )
t.push_back(c); str.push_back( getStr( it & 15 ) );
rs.emplace_back(t); string ans = "";
} dfs( ans, 0 );
else return ret;
{ }
vector<string> rn;
for (auto c : s)
{
for (auto r : rs)
{
r.push_back(c);
rn.emplace_back(std::move(r));
}
}
std::swap(rs, rn);
}
}
return rs;
}
}; };
``` ```
## 答案 ## 答案
```cpp ```cpp
dfs( ans, k + 1 );
``` ```
## 选项 ## 选项
...@@ -70,17 +97,17 @@ public: ...@@ -70,17 +97,17 @@ public:
### A ### A
```cpp ```cpp
dfs( ans, k );
``` ```
### B ### B
```cpp ```cpp
dfs( ans, k - 1 );
``` ```
### C ### C
```cpp ```cpp
dfs( ans, k + 2 );
``` ```
\ No newline at end of file
...@@ -33,6 +33,117 @@ ...@@ -33,6 +33,117 @@
</div> </div>
</div> </div>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <stdio.h>
#include <stdlib.h>
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
static struct TreeNode *dfs(int low, int high, int *count)
{
int i, j, k;
if (low > high)
{
*count = 0;
return NULL;
}
else if (low == high)
{
struct TreeNode *node = malloc(sizeof(*node));
node->val = low;
node->left = NULL;
node->right = NULL;
*count = 1;
return node;
}
else
{
*count = 0;
int capacity = 5;
struct TreeNode *roots = malloc(capacity * sizeof(struct TreeNode));
for (i = low; i <= high; i++)
{
int left_cnt, right_cnt;
________________________________
if (left_cnt == 0)
left_cnt = 1;
if (right_cnt == 0)
right_cnt = 1;
if (*count + (left_cnt * right_cnt) >= capacity)
{
capacity *= 2;
capacity += left_cnt * right_cnt;
roots = realloc(roots, capacity * sizeof(struct TreeNode));
}
for (j = 0; j < left_cnt; j++)
{
for (k = 0; k < right_cnt; k++)
{
roots[*count].val = i;
roots[*count].left = left_subs == NULL ? NULL : &left_subs[j];
roots[*count].right = right_subs == NULL ? NULL : &right_subs[k];
(*count)++;
}
}
}
return roots;
}
}
static struct TreeNode **generateTrees(int n, int *returnSize)
{
int i, count = 0;
struct TreeNode *roots = dfs(1, n, &count);
struct TreeNode **results = malloc(count * sizeof(struct TreeNode *));
for (i = 0; i < count; i++)
{
results[i] = &roots[i];
}
*returnSize = count;
return results;
}
static void dump(struct TreeNode *node)
{
printf("%d ", node->val);
if (node->left != NULL)
{
dump(node->left);
}
else
{
printf("# ");
}
if (node->right != NULL)
{
dump(node->right);
}
else
{
printf("# ");
}
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test n\n");
exit(-1);
}
int i, count = 0;
struct TreeNode **results = generateTrees(atoi(argv[1]), &count);
for (i = 0; i < count; i++)
{
dump(results[i]);
printf("\n");
}
return 0;
}
```
## template ## template
```cpp ```cpp
...@@ -148,7 +259,8 @@ int main(int argc, char **argv) ...@@ -148,7 +259,8 @@ int main(int argc, char **argv)
## 答案 ## 答案
```cpp ```cpp
struct TreeNode *left_subs = dfs(low, i - 1, &left_cnt);
struct TreeNode *right_subs = dfs(i + 1, high, &right_cnt);
``` ```
## 选项 ## 选项
...@@ -156,17 +268,20 @@ int main(int argc, char **argv) ...@@ -156,17 +268,20 @@ int main(int argc, char **argv)
### A ### A
```cpp ```cpp
struct TreeNode *left_subs = dfs(low, i + 1, &left_cnt);
struct TreeNode *right_subs = dfs(i + 1, high, &right_cnt);
``` ```
### B ### B
```cpp ```cpp
struct TreeNode *left_subs = dfs(low, i - 1, &left_cnt);
struct TreeNode *right_subs = dfs(i - 1, high, &right_cnt);
``` ```
### C ### C
```cpp ```cpp
struct TreeNode *left_subs = dfs(high, i - 1, &left_cnt);
struct TreeNode *right_subs = dfs(i + 1, low, &right_cnt);
``` ```
\ No newline at end of file
...@@ -110,56 +110,58 @@ ...@@ -110,56 +110,58 @@
</ul> </ul>
</div> </div>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
public static int myAtoi(String str) {
if (str.isEmpty()) return 0;
int sign = 1;
int base = 0;
int i = 0;
while (str.charAt(i) == ' ')
i++;
if (str.charAt(i) == '-' || str.charAt(i) == '+')
sign = str.charAt(i++) == '-' ? -1 : 1;
while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
if (base > Integer.MAX_VALUE / 10 || (base == Integer.MAX_VALUE / 10 && str.charAt(i) - '0' > 7)) {
return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
____________________
}
return base * sign;
}
```
## template ## template
```cpp ```cpp
int myAtoi(char *str) public static int myAtoi(String str) {
{ if (str.isEmpty()) return 0;
int i = 0; int sign = 1;
int sign = 0; int base = 0;
while (str[i] && str[i] == ' ') int i = 0;
i++; while (str.charAt(i) == ' ')
if (str[i] == NULL) i++;
return 0;
if (str[i] == '-') if (str.charAt(i) == '-' || str.charAt(i) == '+')
{ sign = str.charAt(i++) == '-' ? -1 : 1;
sign = 1;
i++; while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
} if (base > Integer.MAX_VALUE / 10 || (base == Integer.MAX_VALUE / 10 && str.charAt(i) - '0' > 7)) {
else if (str[i] == '+') return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
{ }
sign = 0; base = 10 * base + (str.charAt(i++) - '0');
i++; }
} return base * sign;
else if (str[i] < '0')
return 0;
else if (str[i] > '9')
return 0;
long long int r = 0;
while (str[i])
{
if (str[i] < '0')
break;
else if (str[i] > '9')
break;
else
r = r * 10 + str[i++] - '0';
if (r > INT_MAX)
break;
}
r = sign ? -r : r;
if (r < INT_MIN)
return INT_MIN;
if (r > INT_MAX)
return INT_MAX;
return (int)r;
} }
``` ```
## 答案 ## 答案
```cpp ```cpp
base = 10 * base + (str.charAt(i++) - '0');
``` ```
## 选项 ## 选项
...@@ -167,17 +169,17 @@ int myAtoi(char *str) ...@@ -167,17 +169,17 @@ int myAtoi(char *str)
### A ### A
```cpp ```cpp
base = base + (str.charAt(i++) - '0');
``` ```
### B ### B
```cpp ```cpp
base = base + (str.charAt(i++) + '0');
``` ```
### C ### C
```cpp ```cpp
base = 10 * base + (str.charAt(i++) + '0');
``` ```
\ No newline at end of file
...@@ -28,6 +28,30 @@ ...@@ -28,6 +28,30 @@
</ul> </ul>
</div> </div>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <stdc++.h>
using namespace std;
class Solution
{
public:
int numTrees(int n)
{
vector<int> sum(n + 1);
sum[0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < i; j++)
{
___________________
}
}
return sum[n];
}
}
```
## template ## template
```cpp ```cpp
...@@ -55,7 +79,7 @@ public: ...@@ -55,7 +79,7 @@ public:
## 答案 ## 答案
```cpp ```cpp
sum[i] += sum[j] * sum[i - j - 1];
``` ```
## 选项 ## 选项
...@@ -63,17 +87,17 @@ public: ...@@ -63,17 +87,17 @@ public:
### A ### A
```cpp ```cpp
sum[i] += sum[j] * sum[i - j];
``` ```
### B ### B
```cpp ```cpp
sum[i] += sum[j] * sum[j - i - 1];
``` ```
### C ### C
```cpp ```cpp
sum[i] += sum[j] * sum[j - i];
``` ```
\ No newline at end of file
...@@ -24,6 +24,83 @@ ...@@ -24,6 +24,83 @@
<li><code>0 <newInterval[0] <= newInterval[1] <= 10<sup>5</sup></code></li> <li><code>0 <newInterval[0] <= newInterval[1] <= 10<sup>5</sup></code></li>
</ul> </ul>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <stdio.h>
#include <stdlib.h>
static int compare(const void *a, const void *b)
{
return ((int *)a)[0] - ((int *)b)[0];
}
int **insert(int **intervals, int intervalsSize, int *intervalsColSize, int *newInterval,
int newIntervalSize, int *returnSize, int **returnColumnSizes)
{
int i, len = 0;
int *tmp = malloc((intervalsSize + 1) * 2 * sizeof(int));
for (i = 0; i < intervalsSize; i++)
{
tmp[i * 2] = intervals[i][0];
tmp[i * 2 + 1] = intervals[i][1];
}
tmp[i * 2] = newInterval[0];
tmp[i * 2 + 1] = newInterval[1];
qsort(tmp, intervalsSize + 1, 2 * sizeof(int), compare);
int **results = malloc((intervalsSize + 1) * sizeof(int *));
results[0] = malloc(2 * sizeof(int));
results[0][0] = tmp[0];
results[0][1] = tmp[1];
for (i = 1; i < intervalsSize + 1; i++)
{
results[i] = malloc(2 * sizeof(int));
if (tmp[i * 2] > results[len][1])
{
len++;
________________________
}
else if (tmp[i * 2 + 1] > results[len][1])
{
results[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 results;
}
int main(int argc, char **argv)
{
if (argc < 3 || argc % 2 == 0)
{
fprintf(stderr, "Usage: ./test new_s new_e s0 e0 s1 e1...");
exit(-1);
}
int new_interv[2];
new_interv[0] = atoi(argv[1]);
new_interv[1] = atoi(argv[2]);
int i, count = 0;
int *size = malloc((argc - 3) / 2 * sizeof(int));
int **intervals = malloc((argc - 3) / 2 * sizeof(int *));
for (i = 0; i < (argc - 3) / 2; i++)
{
intervals[i] = malloc(2 * sizeof(int));
intervals[i][0] = atoi(argv[i * 2 + 3]);
intervals[i][1] = atoi(argv[i * 2 + 4]);
}
int *col_sizes;
int **results = insert(intervals, (argc - 3) / 2, size, new_interv, 2, &count, &col_sizes);
for (i = 0; i < count; i++)
{
printf("[%d,%d]\n", results[i][0], results[i][1]);
}
return 0;
}
```
## template ## template
```cpp ```cpp
...@@ -105,7 +182,8 @@ int main(int argc, char **argv) ...@@ -105,7 +182,8 @@ int main(int argc, char **argv)
## 答案 ## 答案
```cpp ```cpp
results[len][0] = tmp[i * 2];
results[len][1] = tmp[i * 2 + 1];
``` ```
## 选项 ## 选项
...@@ -113,17 +191,20 @@ int main(int argc, char **argv) ...@@ -113,17 +191,20 @@ int main(int argc, char **argv)
### A ### A
```cpp ```cpp
results[len][0] = tmp[i * 2 - 1];
results[len][1] = tmp[i * 2 + 1];
``` ```
### B ### B
```cpp ```cpp
results[len][0] = tmp[i * 2 + 1];
results[len][1] = tmp[i * 2 - 1];
``` ```
### C ### C
```cpp ```cpp
results[len][0] = tmp[i * 2 + 1];
results[len][1] = tmp[i * 2];
``` ```
\ No newline at end of file
...@@ -2,6 +2,48 @@ ...@@ -2,6 +2,48 @@
<p>给定一个包含 <em>n</em> 个整数的数组 <code>nums</code> 和一个目标值 <code>target</code>,判断 <code>nums</code> 中是否存在四个元素 <em>a,</em><em>b,c</em> 和 <em>d</em> ,使得 <em>a</em> + <em>b</em> + <em>c</em> + <em>d</em> 的值与 <code>target</code> 相等?找出所有满足条件且不重复的四元组。</p><p><strong>注意:</strong>答案中不可以包含重复的四元组。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,0,-1,0,-2,2], target = 0<strong><br />输出:</strong>[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [], target = 0<strong><br />输出:</strong>[]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 200</code></li> <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li></ul> <p>给定一个包含 <em>n</em> 个整数的数组 <code>nums</code> 和一个目标值 <code>target</code>,判断 <code>nums</code> 中是否存在四个元素 <em>a,</em><em>b,c</em> 和 <em>d</em> ,使得 <em>a</em> + <em>b</em> + <em>c</em> + <em>d</em> 的值与 <code>target</code> 相等?找出所有满足条件且不重复的四元组。</p><p><strong>注意:</strong>答案中不可以包含重复的四元组。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,0,-1,0,-2,2], target = 0<strong><br />输出:</strong>[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [], target = 0<strong><br />输出:</strong>[]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 200</code></li> <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
class Solution
{
public:
vector<vector<int>> fourSum(vector<int> &nums, int target)
{
long long l_target = target;
sort(nums.begin(), nums.end());
vector<vector<int>> results;
int N = nums.size();
for (int i = 0; i < N - 3; i++)
{
if (i > 0 && nums[i] == nums[i - 1])
continue;
for (int j = i + 1; j < N - 2; j++)
{
if (j > i + 1 && nums[j] == nums[j - 1])
continue;
for (int k = j + 1, l = N - 1; k < l; k++)
{
if (k > j + 1 && nums[k] == nums[k - 1])
continue;
_____________________________
if (k >= l)
{
break;
}
if ((target - nums[i] - nums[j] - nums[k] - nums[l]) == 0)
{
results.emplace_back(
vector<int>({nums[i], nums[j], nums[k], nums[l]}));
}
}
}
}
return results;
}
};
```
## template ## template
```cpp ```cpp
...@@ -51,7 +93,10 @@ public: ...@@ -51,7 +93,10 @@ public:
## 答案 ## 答案
```cpp ```cpp
while (k < l && (l_target - nums[i] - nums[j] - nums[k] - nums[l]) < 0)
{
l--;
}
``` ```
## 选项 ## 选项
...@@ -59,17 +104,26 @@ public: ...@@ -59,17 +104,26 @@ public:
### A ### A
```cpp ```cpp
while (k > l && (l_target - nums[i] - nums[j] - nums[k] - nums[l]) < 0)
{
l--;
}
``` ```
### B ### B
```cpp ```cpp
while (k > l && (l_target - nums[i] - nums[j] - nums[k] - nums[l]) < 0)
{
l++;
}
``` ```
### C ### C
```cpp ```cpp
while (k < l && (l_target - nums[i] - nums[j] - nums[k] - nums[l]) < 0)
{
l++;
}
``` ```
\ No newline at end of file
...@@ -66,6 +66,78 @@ countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211" ...@@ -66,6 +66,78 @@ countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
</ul> </ul>
</div> </div>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void parse(char *input, char *output)
{
char *p = input;
char *q = output;
while (*p != '\0')
{
int count = 1;
while (p[0] == p[1])
{
count++;
p++;
}
int n = 0;
while (count > 0)
{
n += count % 10;
count /= 10;
}
____________________
*q++ = p[0];
p++;
}
*q = '\0';
}
static char *countAndSay(int n)
{
if (n < 1)
{
return NULL;
}
char *result;
char *prev = malloc(10000);
char *next = malloc(10000);
strcpy(prev, "1");
if (n == 1)
{
return prev;
}
int i;
for (i = 2; i <= n; i++)
{
if (i & 0x1)
{
parse(next, prev);
result = prev;
}
else
{
parse(prev, next);
result = next;
}
}
return result;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test n\n");
exit(-1);
}
printf("%s\n", countAndSay(atoi(argv[1])));
return 0;
}
```
## template ## template
```cpp ```cpp
...@@ -145,7 +217,11 @@ int main(int argc, char **argv) ...@@ -145,7 +217,11 @@ int main(int argc, char **argv)
## 答案 ## 答案
```cpp ```cpp
while (n > 0)
{
*q++ = (n % 10) + '0';
n /= 10;
}
``` ```
## 选项 ## 选项
...@@ -153,17 +229,29 @@ int main(int argc, char **argv) ...@@ -153,17 +229,29 @@ int main(int argc, char **argv)
### A ### A
```cpp ```cpp
while (n > 0)
{
q++ = (n % 10) + '0';
n /= 10;
}
``` ```
### B ### B
```cpp ```cpp
while (n > 0)
{
*q = (n % 10) + '0';
n /= 10;
}
``` ```
### C ### C
```cpp ```cpp
while (n > 0)
{
*q += (n % 10) + '0';
n /= 10;
}
``` ```
\ No newline at end of file
...@@ -2,6 +2,52 @@ ...@@ -2,6 +2,52 @@
<p>给你一个链表的头节点 <code>head</code> ,旋转链表,将链表每个节点向右移动 <code>k</code><em> </em>个位置。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0061.Rotate%20List/images/rotate1.jpg" style="width: 600px; height: 254px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 2<strong><br />输出:</strong>[4,5,1,2,3]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0061.Rotate%20List/images/roate2.jpg" style="width: 472px; height: 542px;" /><pre><strong>输入:</strong>head = [0,1,2], k = 4<strong><br />输出:</strong>[2,0,1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点的数目在范围 <code>[0, 500]</code> 内</li> <li><code>-100 <= Node.val <= 100</code></li> <li><code>0 <= k <= 2 * 10<sup>9</sup></code></li></ul> <p>给你一个链表的头节点 <code>head</code> ,旋转链表,将链表每个节点向右移动 <code>k</code><em> </em>个位置。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0061.Rotate%20List/images/rotate1.jpg" style="width: 600px; height: 254px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 2<strong><br />输出:</strong>[4,5,1,2,3]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0061.Rotate%20List/images/roate2.jpg" style="width: 472px; height: 542px;" /><pre><strong>输入:</strong>head = [0,1,2], k = 4<strong><br />输出:</strong>[2,0,1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点的数目在范围 <code>[0, 500]</code> 内</li> <li><code>-100 <= Node.val <= 100</code></li> <li><code>0 <= k <= 2 * 10<sup>9</sup></code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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 *rotateRight(ListNode *head, int k)
{
if (head == nullptr)
{
return head;
}
int len = 0;
ListNode dummy;
dummy.next = head;
ListNode *tail = &dummy;
while (tail->next != nullptr)
{
len++;
tail = tail->next;
}
ListNode *prev = &dummy;
ListNode *p = head;
k = k % len;
_______________________
if (p != nullptr)
{
prev->next = tail->next;
tail->next = head;
head = p;
}
return head;
}
};
```
## template ## template
```cpp ```cpp
...@@ -55,7 +101,11 @@ public: ...@@ -55,7 +101,11 @@ public:
## 答案 ## 答案
```cpp ```cpp
for (int i = 0; i < len - k; i++)
{
prev = p;
p = p->next;
}
``` ```
## 选项 ## 选项
...@@ -63,17 +113,29 @@ public: ...@@ -63,17 +113,29 @@ public:
### A ### A
```cpp ```cpp
for (int i = 0; i < len; i++)
{
prev = p;
p = p->next;
}
``` ```
### B ### B
```cpp ```cpp
for (int i = 0; i < len - k; i++)
{
p = p->next;
prev = p;
}
``` ```
### C ### C
```cpp ```cpp
for (int i = 0; i < len; i++)
{
p = p->next;
prev = p;
}
``` ```
\ No newline at end of file
...@@ -4,6 +4,42 @@ ...@@ -4,6 +4,42 @@
<p><strong>示例:</strong></p> <p><strong>示例:</strong></p>
<pre><strong>输入:</strong>&nbsp;n = 4, k = 2<strong><br />输出:</strong>[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]</pre> <pre><strong>输入:</strong>&nbsp;n = 4, k = 2<strong><br />输出:</strong>[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]</pre>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> combine(int n, int k)
{
vector<vector<int>> res;
dfs(n, k, 1, res);
return res;
}
private:
vector<int> stack;
void dfs(int n, int k, int start, vector<vector<int>> &res)
{
if (stack.size() == k)
{
res.push_back(stack);
}
else
{
for (int i = start; i <= n; i++)
{
stack.push_back(i);
_____________________
stack.pop_back();
}
}
}
};
```
## template ## template
```cpp ```cpp
...@@ -42,7 +78,7 @@ private: ...@@ -42,7 +78,7 @@ private:
## 答案 ## 答案
```cpp ```cpp
dfs(n, k, i + 1, res);
``` ```
## 选项 ## 选项
...@@ -50,17 +86,17 @@ private: ...@@ -50,17 +86,17 @@ private:
### A ### A
```cpp ```cpp
dfs(n, k, i, res);
``` ```
### B ### B
```cpp ```cpp
dfs(n - 1, k, i - 1, res);
``` ```
### C ### C
```cpp ```cpp
dfs(n + 1, k, i - 1, res);
``` ```
\ No newline at end of file
...@@ -2,6 +2,42 @@ ...@@ -2,6 +2,42 @@
<p>数字 <code>n</code> 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 <strong>有效的 </strong>括号组合。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3<strong><br />输出:</strong>["((()))","(()())","(())()","()(())","()()()"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>["()"]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 8</code></li></ul> <p>数字 <code>n</code> 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 <strong>有效的 </strong>括号组合。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3<strong><br />输出:</strong>["((()))","(()())","(())()","()(())","()()()"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>["()"]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 8</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
class Solution
{
public:
void gen(string &p, int lc, int rc, vector<string> &r, int n)
{
if (lc > n)
return;
if (lc == n && rc == n)
{
r.push_back(p);
return;
}
p.push_back('(');
lc++;
gen(p, lc, rc, r, n);
p.pop_back();
lc--;
if (lc > rc)
{
_________________
}
}
vector<string> generateParenthesis(int n)
{
string p;
int lc = 0, rc = 0;
vector<string> r;
gen(p, lc, rc, r, n);
return r;
}
};
```
## template ## template
```cpp ```cpp
...@@ -45,7 +81,11 @@ public: ...@@ -45,7 +81,11 @@ public:
## 答案 ## 答案
```cpp ```cpp
p.push_back(')');
rc++;
gen(p, lc, rc, r, n);
p.pop_back();
rc--;
``` ```
## 选项 ## 选项
...@@ -53,17 +93,29 @@ public: ...@@ -53,17 +93,29 @@ public:
### A ### A
```cpp ```cpp
p.push_back(')');
rc--;
gen(p, lc, rc, r, n);
p.pop_back();
rc++;
``` ```
### B ### B
```cpp ```cpp
p.push_back('(');
rc++;
gen(p, lc, rc, r, n);
p.pop_back();
rc--;
``` ```
### C ### C
```cpp ```cpp
p.push_back('(');
rc--;
gen(p, lc, rc, r, n);
p.pop_back();
rc++;
``` ```
\ No newline at end of file
...@@ -2,6 +2,30 @@ ...@@ -2,6 +2,30 @@
<p>给定一个 <em>n </em>× <em>n</em> 的二维矩阵 <code>matrix</code> 表示一个图像。请你将图像顺时针旋转 90 度。</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> 旋转图像,这意味着你需要直接修改输入的二维矩阵。<strong>请不要 </strong>使用另一个矩阵来旋转图像。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0048.Rotate%20Image/images/mat1.jpg" style="width: 642px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<strong><br />输出:</strong>[[7,4,1],[8,5,2],[9,6,3]]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0048.Rotate%20Image/images/mat2.jpg" style="width: 800px; height: 321px;" /><pre><strong>输入:</strong>matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]<strong><br />输出:</strong>[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [[1]]<strong><br />输出:</strong>[[1]]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [[1,2],[3,4]]<strong><br />输出:</strong>[[3,1],[4,2]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>matrix.length == n</code></li> <li><code>matrix[i].length == n</code></li> <li><code>1 <= n <= 20</code></li> <li><code>-1000 <= matrix[i][j] <= 1000</code></li></ul> <p>给定一个 <em>n </em>× <em>n</em> 的二维矩阵 <code>matrix</code> 表示一个图像。请你将图像顺时针旋转 90 度。</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> 旋转图像,这意味着你需要直接修改输入的二维矩阵。<strong>请不要 </strong>使用另一个矩阵来旋转图像。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0048.Rotate%20Image/images/mat1.jpg" style="width: 642px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<strong><br />输出:</strong>[[7,4,1],[8,5,2],[9,6,3]]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0048.Rotate%20Image/images/mat2.jpg" style="width: 800px; height: 321px;" /><pre><strong>输入:</strong>matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]<strong><br />输出:</strong>[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [[1]]<strong><br />输出:</strong>[[1]]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [[1,2],[3,4]]<strong><br />输出:</strong>[[3,1],[4,2]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>matrix.length == n</code></li> <li><code>matrix[i].length == n</code></li> <li><code>1 <= n <= 20</code></li> <li><code>-1000 <= matrix[i][j] <= 1000</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void rotate(vector<vector<int>> &matrix)
{
int size = matrix.size();
for (int i = 0; i < size / 2; i++)
{
int low = i, high = size - i - 1;
for (int j = low; j < high; j++)
{
int tmp = matrix[i][j];
_________________________
}
}
}
};
```
## template ## template
```cpp ```cpp
...@@ -32,7 +56,10 @@ public: ...@@ -32,7 +56,10 @@ public:
## 答案 ## 答案
```cpp ```cpp
matrix[i][j] = matrix[size - 1 - j][i];
matrix[size - 1 - j][i] = matrix[size - 1 - i][size - 1 - j];
matrix[size - 1 - i][size - 1 - j] = matrix[j][size - 1 - i];
matrix[j][size - 1 - i] = tmp;
``` ```
## 选项 ## 选项
...@@ -40,17 +67,26 @@ public: ...@@ -40,17 +67,26 @@ public:
### A ### A
```cpp ```cpp
matrix[i][j] = matrix[size - 1 - j][i];
matrix[size - 1 - j][i] = matrix[size - i][size - j];
matrix[size - 1 - i][size - j] = matrix[j][size - i];
matrix[j][size - 1 - i] = tmp;
``` ```
### B ### B
```cpp ```cpp
matrix[i][j] = matrix[size - 1 - j][i];
matrix[size + 1 - j][i] = matrix[size + 1 - i][size + 1 - j];
matrix[size + 1 - i][size + 1 - j] = matrix[j][size + 1 - i];
matrix[j][size + 1 - i] = tmp;
``` ```
### C ### C
```cpp ```cpp
matrix[i][j] = matrix[size - 1 - j][i];
matrix[size - 1 - j][i] = matrix[size - 1 - i][size + 1 - j];
matrix[size - 1 - i][size - 1 - j] = matrix[j][size + 1 - i];
matrix[j][size - 1 - i] = tmp;
``` ```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册