
数据结构与算法
刷题
stitchshaw
这个作者很懒,什么都没留下…
展开
-
从尾到头打印链表
文章目录1.调用库函数reverse实现vector内部元素的翻转.2.递归法3.堆栈法4.反转链表(改变链表结构)5.利用vector的insert特性原题:https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035?tpId=13&tqId=23278&ru=/practice/75e878df47f24fdc9dc3e400ec6058ca&qru=/ta/coding-interviews/ques原创 2022-02-14 14:23:29 · 727 阅读 · 0 评论 -
C++统计字符串中各类型字符的个数
#include <iostream>#include <cstring>using namespace std;int main() { int letter = 0; int digit = 0; int space = 0; int other = 0; char buf[1024] = {0}; cin.getline(buf, sizeof(buf)); // write your code he原创 2022-02-08 17:47:15 · 7806 阅读 · 0 评论 -
C++迭代器遍历容器
详见:https://www.nowcoder.com/practice/0f7ab22e60ee4574a9d9c81412b26595?tpId=225&tqId=2193295&ru=/practice/f5e0b2ea13ee40308fcc275c0d06053f&qru=/ta/primary-grammar-cpp/question-ranking具体做法:对于迭代器,我们可以看成C++中的指针,它指向容器的某个位置,使用*可以访问该位置的值。我们输入数据以后,转载 2022-02-07 21:07:35 · 5245 阅读 · 0 评论 -
C++点和圆的关系
#include <iostream>using namespace std;// 点类class Pointer {private: int x; // x 坐标 int y; // y 坐标public: void setX(int x) { this->x = x; } int getX() { return x; } void setY(int y) {原创 2022-02-06 14:42:18 · 849 阅读 · 0 评论 -
C++设计立方体类
#include <iostream>using namespace std;class Cube { //定义属性private: int length, width, height; //成员变量//定义方法public: void setLength(int l){ //设置长 length = l; } void setWidth(int w){ //设置宽 width = w; }原创 2022-02-06 13:55:42 · 998 阅读 · 0 评论 -
C++编写函数实现两数交换(引用方式)
需要注意新建的函数名不能为swap,因为C++自带的两个整数转换的函数就为swap,因此需要换一个名字。观察上图,若swap()函数代码如下????,则意味着swap函数开辟了新内存地址,利用新的变量来存储这两个值。因此,只是在swap函数中交换m与n的值,在函数外面的main()主函数的m和n还是老样子。也就是说此m、n非彼m、n,因为内存地址不同。void swap(int m, int n){ int temp = m; m = n; n = temp;}但是如原创 2022-02-05 22:09:59 · 4454 阅读 · 0 评论 -
C++不死神兔问题
详见:https://www.nowcoder.com/practice/9fecec9c776c436b8a03ba0684ac76a7?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=0实际上是斐波那契数列问题:f(n)=f(n−1)+f(n−2)f(n) = f(n-1) + f(n-2)f(n)=f(n−1)+f(n−2)递归法时间复杂度:O(2n)O(2^n)O(2n),如图所示,树型递归,转载 2022-02-05 17:06:49 · 271 阅读 · 0 评论 -
C++函数实现计算一个数的阶乘
递归#include <iostream>using namespace std;long long factorial(int n);int main() { int n; cin >> n; cout << factorial(n) << endl; return 0;}long long factorial(int n) { // write your code here......原创 2022-02-04 13:18:29 · 5380 阅读 · 0 评论 -
C++使用字符函数统计字符串中各类型字符的个数
详见:https://www.nowcoder.com/practice/31bdbc70188f48e995fa3cbef36613c8?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=0本题不是用字符数组读入的字符串,而是直接用了string类。我们可以用length函数直接判断字符串的长度,然后遍历字符串,对于遍历到的字符依次用函数检查每个字符属于哪一类,相应类的变量加1.使用前三个函数检查字符即可转载 2022-02-04 12:11:11 · 3014 阅读 · 0 评论 -
C++统计字符串中子串出现的次数
详见:https://www.nowcoder.com/practice/9eb684f845a446f3b121472de2ea75cd?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=0利用find函数首先将字符数组转化为字符串str1和str2。从str1下标i开始查找str2,如果找得到,计数加1,并且i从找到的位置,后移一位。#include <iostream>#inclu原创 2022-02-04 11:57:07 · 8613 阅读 · 1 评论 -
C++编写函数实现两数交换(指针方式)
文章目录常规方式用swap不用swap引用变量方式(常规方式的函数形式)指针变量方式的非函数形式指针变量方式的函数形式常规方式用swap#include <iostream>#include <bits/stdc++.h>using namespace std;// write your code here......int main() { int m, n; cin >> m; cin >> n; /原创 2022-02-03 16:40:30 · 1650 阅读 · 0 评论 -
C++比较字符串大小(自己实现strcmp()函数)
详见:https://www.nowcoder.com/practice/963e455fdf7c4a4a997160abedc1951b?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=01.移动指针法但这种方法只适用于二个字符数组中从头开始,有几个相同字符的字符数组,不适用于其它情况。时间复杂度:O(n),n为较短的字符串的长度空间复杂度:O(1),只有指针,无额外空间输入mystrcmp函数的是原创 2022-02-03 16:08:52 · 7702 阅读 · 0 评论 -
C++数组元素处理--将数组 为 0 的元素都移至数组末尾,将非 0 的元素移至开始(保持原来的顺序不变)。
把数组中为0的元素都移至数组末尾,将非 0 的元素移至开始(保持原来的顺序不变)。原创 2022-02-02 17:31:49 · 1284 阅读 · 0 评论 -
C++创建动态数组
文章目录用指针-第一种用指针-第二种以下不是通过创建动态数组的方法用vector不用指针-第一种错误写法:正确写法:不用指针-第二种详见:https://www.nowcoder.com/practice/218b577112a24c23a41bdc01f28c18ac?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=0用指针-第一种C++申请动态数组是依靠指针,数组是int型的我们就新开一个int型的指针原创 2022-02-02 11:41:40 · 1347 阅读 · 0 评论 -
C++复制部分字符串
指针详见:https://www.nowcoder.com/practice/8f5b923683b94e549880e3c8370e3e55?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=0#include <iostream>using namespace std;int main() { char str[30] = { 0 }; // 开辟一块最大长度为30的字符串空间原创 2022-02-01 23:02:49 · 2052 阅读 · 0 评论 -
C++获取字符串长度
for#include <iostream>using namespace std;int main() { char str[100] = { 0 }; cin.getline(str, sizeof(str)); // write your code here...... char* ptr = str; int len = sizeof(str) / sizeof(char); int length = 0; for (i原创 2022-02-01 10:24:53 · 1106 阅读 · 0 评论 -
C++ 利用指针遍历数组
while#include <iostream>using namespace std;int main() { int arr[6] = { 0 }; int* ptr = arr; int len = sizeof(arr) / sizeof(int); for (int i = 0; i < len; i++) { cin >> arr[i]; } // write your code her原创 2022-02-01 10:07:08 · 2147 阅读 · 0 评论 -
C++结构体简单使用
#include <iostream>#include <string>using namespace std;struct student { // write your code here...... string name; int age; double height; };int main() { student s; cin >> s.name >> s.age >&g原创 2022-01-31 18:35:12 · 101 阅读 · 0 评论 -
C++字符串拼接
直接字符串相加即拼接主要注意的是输入字符串的方式,采用getline(cin,str)#include <iostream>#include <string>using namespace std;int main() { string s1, s2; getline(cin, s1); getline(cin, s2); // write your code here...... s1 += s2;原创 2022-01-31 18:23:27 · 9962 阅读 · 1 评论 -
C++构建二维数组
#include <iostream>using namespace std;int main() { int arr[4][3] = { // write your code here..... {22, 66, 44}, {77, 33, 88}, {25, 45, 65}, {11, 66, 99} }; int sum = 0; f原创 2022-01-31 18:11:18 · 161 阅读 · 0 评论 -
C++数组元素反转
详见及参考:https://www.nowcoder.com/practice/8c9793ae96974a9ebb153d90ef31d357?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=0双指针时间复杂度:循环最多执行n/2次,所以时间复杂度为O(n)。空间复杂度:需要额外常数级别的空间,所以空间复杂度为O(1)。#include <iostream>using namespace原创 2022-01-31 13:13:26 · 1260 阅读 · 0 评论 -
C++获取数组最值
时间复杂度:O(n),n为数组长度,遍历一次数组空间复杂度:O(1),无额外空间利用三目运算符#include <iostream>using namespace std;int main() { int arr[6] = { 0 }; int len = sizeof(arr) / sizeof(int); // sizeof(int)为4,sizeof(arr)为24 for (int i = 0; i < len; i++) {原创 2022-01-31 11:24:38 · 442 阅读 · 0 评论 -
C++判断一个数是不是质数
详见:https://www.nowcoder.com/practice/b8bb5e7703da4a83ac7754c0f3d45a82?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=0质数是只能被1及其本身整除的正整数。例如2是质数:2÷1=2,2÷2=1,即2被1整除是2,2被其本身整除是1。2只能被1及其本身整除,因此它是质数。判断一个数n是不是质数,1.首先,任何正整数都能被1及其本身n整除原创 2022-01-30 21:22:10 · 9089 阅读 · 2 评论 -
C++规律数列求和
详见https://www.nowcoder.com/practice/e05d1c142b3d4898be7183289a00ce5f?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=0法1 数学规律时间复杂度:循环总共执行10次,所以时间复杂度为O(1)。空间复杂度:需要额外常数级别的空间,所以空间复杂度为O(1)。#include <iostream>using namespace原创 2022-01-30 14:28:29 · 976 阅读 · 0 评论 -
C++打印乘法表
题:https://www.nowcoder.com/practice/4c1ec287030d4ca1be63b258f88ebcc1?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=0时间复杂度: O(n2)\ O(n^2) O(n2),两层循环遍历。但本题的n默认在1 - 9之间,因此最多执行9∗(9+1)/2=459*(9+1)/2=459∗(9+1)/2=45次,所以时间复杂度为原创 2022-01-29 17:32:23 · 803 阅读 · 1 评论 -
C++输出水仙花数
详见:https://www.nowcoder.com/practice/dabaf13009ef4d0cbf22302fd6a530a6?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=0解法1时间复杂度:O(1),循环从100到999属于常数次,判断是否是水仙花数(isNarcissus)也是常数级空间复杂度:O(1)O(1)O(1),常数个变量#include <iostream>u原创 2022-01-29 17:04:16 · 778 阅读 · 0 评论 -
C++计算一个数的阶乘
迭代时间复杂度:O(n),一次遍历,一共循环n次空间复杂度:O(1),无额外空间使用#include <iostream>using namespace std;int main() { int n; cin >> n; long long factorial = 1; for(int i=2; i<=n; i++){ //迭代2到n,1就不用了 factorial *= i; }原创 2022-01-29 15:52:22 · 427 阅读 · 0 评论 -
C++ 求 1 - n 之间偶数的和
for但此法的缺陷是当n很大时,会循环很多次,影响运行时间。时间复杂度为O(n/2)=O(n)#include <iostream>using namespace std;int main() { int n; cin >> n; int sum = 0; for (int i=1; i<=n; i++) if (i%2 == 0) sum += i; cout原创 2022-01-29 15:25:45 · 5624 阅读 · 1 评论 -
C++判断季节
#include <iostream>using namespace std;int main() { int month; cin >> month; if (month>=3 && month<=5) cout << "春季"; else if (month>=6 &&原创 2022-01-28 17:39:05 · 964 阅读 · 0 评论 -
C++判断成绩等级
else if#include <iostream>using namespace std;int main() { int score; cin >> score; if (score>=90 && score<=100) cout << "优秀"; else if (score>=80 && score<=89) cout原创 2022-01-28 17:23:50 · 5237 阅读 · 1 评论 -
C++判断身材状态
用else if#include <iostream>using namespace std;int main() { double weight; double height; cin >> weight; cin >> height; double BMI = weight / (height*height); if(BMI<18.5){ cout << "偏瘦";原创 2022-01-28 16:11:02 · 1039 阅读 · 1 评论 -
C++获取三个数中的最大值(三元表达式实现)
#include <iostream>using namespace std;int main() { int a, b, c; cin >> a; cin >> b; cin >> c; int max; max = a>b ? a:b; max = max>c ? max:c; cout << max << endl; retur原创 2022-01-28 15:32:33 · 1556 阅读 · 0 评论 -
C++简单运算(和,差,积,商,模)
#include <iostream>using namespace std;int main() { int a,b; cin >> a >> b; if (a>b) cout << a+b << " " << a-b << " " << a*b << " " << a/b << " " << a%b <&原创 2022-01-28 15:14:21 · 1196 阅读 · 0 评论 -
c++获取两数中的较大值
#include <iostream>using namespace std;int main() { int a, b; cin >> a >> b; if (a>b) cout << a << endl; else cout << b << endl; return 0;}int main() {原创 2022-01-28 10:58:58 · 4546 阅读 · 0 评论 -
C++两数求和
#include <iostream>using namespace std;int main() { int a,b; cin >> a; // 注意cin和cout的箭头方向 cin >> b; int sum = a + b; cout << sum << endl; return 0;}#include <iostream>using namespace原创 2022-01-27 22:17:08 · 4011 阅读 · 0 评论 -
1. C++实现四舍五入
文章目录1.加头文件使用round2.利用强制类型转换1.加头文件使用round#include <cmath>或#include <math.h>#include <iostream>// #include <cmath>#include <math.h>using namespace std;int main() { double d; cin >> d; cout <<原创 2022-01-27 22:14:41 · 635 阅读 · 0 评论 -
ACM格式构造二叉树输入用例
原文在这儿,由于原作者大佬的Latex公式没有渲染,且有点小错误,看起来难受,我就转写到这儿,顺便改了改。其输入用例,就是用一个数组来表述二叉树(即用二叉树的顺序存储来表述输入用例),如下:看图就易知: 如果(一个)父节点的数组下标是i,那么它的左孩子下标就是i * 2 + 1,右孩子下标就是 i * 2 + 2。如果(一个)父节点在第kkk层的第mmm个节点,(m∈[1,2k−1])(m \in [1,2^{k-1}])(m∈[1,2k−1])。则其左子节点是第k+1k+1k+1层的第2m−12原创 2022-01-05 15:45:34 · 2663 阅读 · 0 评论