一、完成find函数---在一个二维数组(vector对象)中查找有无一个数字,难点在于我不知道如何获取该二维数组的行数和列数html
二、补充:关于C++中vector<vector<int>> A的使用 ****node
三、空格替换为其余字符串(多于一个字符的字符串)的问题ios
四、将字符串或数组元素反转(使用reverse()函数)c++
七、数组的简单操做(使用vector将一个数组中的奇数和偶数分开)数组
八、回文数组服务器
九、斐波那契数列基本实现与优化&(数组实现斐波那契数列)app
十、n级台阶问题:每次只能上一个台阶或上两个台阶,此类问题试剂上就是斐波那契数列问题ide
十一、删除相同的字符串,string变量是不能像数组那样赋值的!!!
1三、已排序好的队列移动几个数字,求移动数字的个数(队列最小修改)
1 class Solution { 2 public: 3 bool Find(int target, vector<vector<int> > array) { 4 //用rowCount、colCount分别表明数组的行数和列数 5 int rowCount = array.size(); 6 int colCount = array[0].size(); 7 /*判断是否比数组最小的值小,或者比最大的值大,这样在数组很大时能够提升效率。 8 数组长度为0也是合法的,因此为了不越界加上列长为0的判断*/ 9 if(colCount == 0 || target < array[0][0] || target > array[rowCount-1][colCount-1]){ 10 return false; 11 } 12 //当row大等于0且col小于列长colCount时,查找数组中是否有target 13 for(int row = rowCount-1, col = 0; row >= 0 && col < colCount;){ 14 if(target == array[row][col]){ 15 return true; 16 }else if(target < array[row][col]){ 17 row--; 18 }else if(target > array[row][col]){ 19 col++; 20 } 21 } 22 return false; 23 } 24 };
方法二(遍历法):
1 public class Solution { 2 public static boolean Find(int target, int [][] array) { 3 if(array == null || array.length == 0 || array[0].length == 0) 4 return false; 5 6 int len1 = array.length; 7 int len2 = array[0].length; 8 for(int i = 0; i < len1; i++) { 9 for(int j = 0; j < len2; j++){ 10 if(array[i][j] == target) 11 return true; 12 } 13 } 14 return false; 15 }
方法三:
1 连接:https://www.nowcoder.com/questionTerminal/abc3fe2ce8e146608e868a70efebf62e?answerType=1&f=discussion 2 来源:牛客网 3 4 public class Solution { 5 public static boolean Find(int target, int [][] array) { 6 if(array == null || array.length == 0 || array[0].length == 0) 7 return false; 8 9 int len1 = array.length; 10 int len2 = array[0].length; 11 for(int i = 0; i < len1; i++) { 12 if(target == array[i][0] || target == array[i][len2 - 1]) 13 return true; 14 else if(target < array[i][0]) 15 return false; 16 else if(target > array[i][len2 - 1]) 17 continue; 18 else { 19 if(binaryFind(array[i], target)) 20 return true; 21 } 22 } 23 24 return false; 25 } 26 27 public static boolean binaryFind(int[] arr, int target) { 28 int l = 0; 29 int r = arr.length - 1; 30 while(l <= r) { 31 int mid = l + (r - l) / 2; 32 if(arr[mid] == target) 33 return true; 34 else if(arr[mid] > target) 35 r = mid - 1; 36 else 37 l = mid + 1; 38 } 39 return false; 40 } 41 }
如下代码包含了如何向A中插入数据和如何获取A矩阵的行数和列数
1 #include <iostream> 2 #include <vector> 3 4 using std::vector; 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 vector<vector<int>> A; 11 //若想定义A = [[0,1,2],[3,4]],有两种方法 12 //方法一:定义vector B分别为[0,1,2]和[3,4],而后放入vector A。 13 //vector<int> B; 14 //B.push_back(0); 15 //B.push_back(1); 16 //B.push_back(2); 17 //A.push_back(B); //将B放入A中 18 19 //B.clear(); //清除B中的元素 20 //B.push_back(3); 21 //B.push_back(4); 22 //A.push_back(B); //将B放入A中 23 24 //方法二 25 for (int i = 0; i < 2; ++i) A.push_back(vector<int>()); //至关于告诉编译器有两行(或分配两个vector对象的空间) 26 A[0].push_back(0); 27 A[0].push_back(1); 28 A[0].push_back(2); 29 A[1].push_back(3); 30 A[1].push_back(4); 31 32 int rowCount = A.size(); //获取A的行数 33 for (int i = 0; i < rowCount; i++) 34 { 35 for (int j = 0; j < A[i].size(); j++) //A[i].size()为获取第i行的列数 36 cout << A[i][j] << "\t"; 37 cout << endl; 38 } 39 40 41 system("pause"); 42 return 0; 43 }
执行结果:
假如char* str = "we are happy"; 那么须要将两个空格都替换为%20,由于自己空格就占一个字符了,而一个空格须要增长三个字符,因此一个空格增长另个字符便可
计算出str中全部的空格数count,须要增长的字符数即:2*coumt;
加入要将空格替换为%200,须要增长的字符数即:(4-1)*count; %200一共占4个字符,减去空格自己的就占的一个字符
1 #include <iostream> 2 #include <vector> 3 4 using std::vector; 5 using std::cout; 6 using std::endl; 7 using std::cin; 8 9 void replaceSpace(char* str, int length); 10 11 int main() 12 { 13 char str[100]; 14 //str[99] = '\0'; //不用添加,添加以后会致使最后报错 15 cout << "请输入一个字符串: "; 16 cin.getline(str, 99); 17 replaceSpace(str, sizeof(str)); 18 cout << str << endl; 19 20 21 system("pause"); 22 return 0; 23 } 24 25 void replaceSpace(char* str, int length) 26 { 27 int count = 0; 28 int i; 29 for (i = 0; i < length; i++) 30 if (str[i] == ' ') 31 count++; 32 for (i = length - 1; i >= 0; i--) 33 { 34 if (str[i] != ' ') 35 str[i + 2 * count] = str[i]; 36 else 37 { 38 count--; 39 str[i + 2 * count] = '%'; 40 str[i + 2 * count + 1] = '2'; 41 str[i + 2 * count + 2] = '0'; 42 } 43 } 44 }
注意:getline()函数会将最后键盘输入的换行符替换为\0,故不须要本身手动给char str[]数组添加空字符!!
运行结果:
若是想要将空格替换为%200, 那么只须要将上述代码中的全部2*count替换为3*count便可
01)使用reverse()函数方法
1 /** 2 * struct ListNode { 3 * int val; 4 * struct ListNode *next; 5 * ListNode(int x) : 6 * val(x), next(NULL) { 7 * } 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> printListFromTailToHead(ListNode* head) { 13 vector<int> A; 14 while (head) { 15 A.push_back(head->val); 16 head = head->next; 17 } 18 reverse(A.begin(), A.end()); 19 return A; 20 } 21 };
02)使用栈
1 public ArrayList printListFromTailToHead(ListNode listNode) { 2 ArrayList list = new ArrayList(); 3 stack<int> mystack; //stack须要包含头文件#include <stack> 4 while (listNode != null) { 5 mystack.push(listNode.val); 6 listNode = listNode.next; 7 } 8 while (!mystack.empty()) { 9 list.add(mystack.pop()); 10 } 11 return list; 12 }
栈容器用法介绍:https://blog.csdn.net/lyj2014211626/article/details/66967761
03)递归方法
1 public class Solution { 2 ArrayList list = new ArrayList(); 3 public ArrayList printListFromTailToHead(ListNode listNode) { 4 if(listNode!=null){ 5 printListFromTailToHead(listNode.next); 6 list.add(listNode.val); 7 } 8 return list; 9 } 10 }
递归调用笔记:https://www.cnblogs.com/YiYA-blog/p/10525031.html#m7
c++中字符串反转的3种方法:https://blog.csdn.net/Szu_AKer/article/details/52422191
01)不须要主函数版本
1 class Solution { 2 public: 3 int minNumberInRotateArray(vector<int> rotateArray) { 4 if(rotateArray.empty()) 5 return 0; 6 for(int i=0; i<rotateArray.size()-1;i++) 7 { 8 if(rotateArray[i]>rotateArray[i+1]) 9 return rotateArray[i+1]; 10 } 11 return rotateArray[0]; 12 } 13 };
02)须要主函数,而且要写上输入输出(小米笔试题)
1 #include <iostream> 2 #include <vector> 3 4 using std::vector; 5 using std::cin; 6 using std::cout; 7 using std::endl; 8 9 int find(vector<int> vec) 10 { 11 if (vec.size() == 0) 12 return 0; 13 for (int i = 0; i < vec.size() - 1; i++) 14 { 15 if (vec[i] > vec[i + 1]) 16 return vec[i + 1]; 17 } 18 return vec[0]; 19 } 20 21 int main() 22 { 23 vector<int> vec; 24 int n; 25 int Min = 0; 26 while (cin >> n) 27 vec.push_back(n); 28 Min = find(vec); 29 cout << Min << endl; 30 31 system("pause"); 32 return 0; 33 }
作了第二遍(2019.10.29):
1 int main() 2 { 3 int c[] = { 2,3,4,5 }; 4 int j, *p = c, *q = c; 5 for (j = 0; j < 4; j++) 6 { 7 printf(" %d", *c); //打印2 2 2 2 8 ++q; 9 } 10 for (j = 0; j < 4; j++) 11 { 12 printf(" %d", *p); //打印2 3 4 5 13 ++p; 14 } 15 }
运行结果:
1 #include <iostream> 2 #include <vector> 3 4 using std::cin; 5 using std::cout; 6 using std::vector; 7 8 int main() 9 { 10 vector<int> num; 11 vector<int> num2; 12 int n,m; 13 while(cin>>n) 14 num.push_back(n); 15 m = num.size(); 16 for(int i=0;i<m;i++) 17 { 18 if(num[i]%2 == 0) 19 num2.push_back(num[i]); 20 } 21 for(int i=0;i<m;i++) 22 { 23 if(num[i]%2 == 1) 24 num2.push_back(num[i]); 25 } 26 for(int i=0;i<m;i++) 27 cout<<num2[i]<<" "; 28 }
题目描述
1 输入描述: 2 输入数据由两行组成: 第一行包含一个正整数 L ,表示数组 a 的长度。 第二行包含 L 个正整数,表示数组 a 。 对于 40% 的数据: 1 < L <= 100 达成条件时须要插入的数字数量很少于 2 个。 对于 100% 的数据: 1 < L <= 1,000 0 < a[i] <= 1,000,000 达成条件时须要插入的数字数量没有限制。 3 输出描述: 4 输出一个整数,表示经过插入若干个正整数使数组 a 回文后,数组 a 的数字和的最小值。
题目链接:https://www.nowcoder.com/practice/00fccaa8e30d414ab86b9bb229bd8e68
斐波那契数列:从第三项开始,每一项等于前两项之和,如:1 1 2 3 5 8 13 21 34...
递推关系为:f(n) = f(n-1) + f(n-2)
1 /* 2 01)斐波那契数列:从第三项开始,每一项等于前两项之和,如:1 1 2 3 5 8 13 21 34... 3 02)使用递归方法实现 4 */ 5 #include "iostream" 6 7 using namespace std; 8 9 int fibon(int n); 10 11 int main() 12 { 13 cout << "请输入斐波那契数列的项数: "; 14 int n, Result; 15 cin >> n; 16 Result = fibon(n); 17 cout << n << "项斐波那契数列,第" << n << "个值为: " << Result << endl; 18 19 system("pause"); 20 return 0; 21 } 22 23 int fibon(int n) 24 { 25 if (n == 1) 26 n = 1; 27 else if (n == 2) 28 n = 1; 29 else 30 n = fibon(n - 1) + fibon(n - 2); 31 return n; 32 }
运行结果:
2、斐波那契数列的优化
1 #include "iostream" 2 3 using namespace std; 4 5 int fibon(int first,int second,int n); 6 7 int main() 8 { 9 cout << "请输入斐波那契数列的项数: "; 10 int first = 1; //数列第一项 11 int second = 1; //数列第二项 12 int n, Result; 13 cin >> n; 14 Result = fibon(first, second, n); 15 cout << n << "项斐波那契数列,第" << n << "个值为: " << Result << endl; 16 17 system("pause"); 18 return 0; 19 } 20 21 int fibon(int a, int b, int n) 22 { 23 if (n > 2) 24 return fibon(a + b, a, n - 1); 25 return a; 26 }
运行结果:
参考博客:https://blog.csdn.net/qq_35256722/article/details/52728739
数组实现斐波那契数列
第n阶只能是从第n-1阶台阶上来或者是从第n-2阶台阶上来,假如上n阶台阶有f(n)种方法,那么f(n) = f(n-1) + f(n-2),即斐波那契数列问题
实现代码和斐波那契数列实现方法是同样的。
参考博客:https://blog.csdn.net/zhang__shuang_/article/details/86768352
题目网址:https://www.nowcoder.com/practice/f0db4c36573d459cae44ac90b90c6212
1、错误的实:使用了string变量str1和str3赋值,str3[k++]= str1[i],这样作的话,str3始终只有一个值 请参考本文博客 遇到的坑(1)
1 #include "iostream" 2 #include "string" 3 4 using namespace std; 5 6 int main() 7 { 8 string str1,str2,str3; 9 getline(cin,str1); //输入第一个字符串 10 getline(cin,str2); //输入第二个字符串 11 int k=0; 12 for(int i=0;i<str1.size();i++) 13 { 14 for(int j=0;j<str2.size();j++) 15 { 16 if(str1[i] == str2[j]) 17 str3[k++] = str1[i+1]; //string变量不能像数组那样直接赋值,这样str3只有一个值 18 else 19 str3[k++] = str1[i]; 20 } 21 } 22 for(int i=0;i<str3.size();i++) 23 cout<<str3[i]; 24 return 0; 25 }
2、使用string类中的find()方法和erase()方法
(1)string类中的find()方法
string::npos是字符串可储存的最大字符数,一般是无符号int或无符号long的最大取值。
(2)string类中的erease()方法
erase(pos,n); 删除从pos开始的n个字符,好比erase(0,1)就是删除第一个字符
erase(position); 删除position处的一个字符(position是个string类型的迭代器)
erase(first,last);删除从first到last之间的字符(first和last都是迭代器)
以上参考博客:https://blog.csdn.net/qq_42376204/article/details/88955774
https://blog.csdn.net/shujh_sysu/article/details/52026108
正确能够实现的代码:
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 int main() 6 { 7 string s1, s2; 8 getline(cin, s1); 9 getline(cin, s2); 10 for (int i = 0; i < s2.size(); i++) 11 { 12 int index; 13 while((index=s1.find(s2[i]))!=-1) //在s1中开始的位置查找有没有字符s2[i],如有则返回s2[i]在s1中首次出现的位置,不然返回-1 14 { 15 s1.erase(index, 1); //删除s1中从index开始的1个字符 16 } 17 } 18 cout << s1 << endl; 19 return 0; 20 }
(1)首先要了解的是列的特性是先进先出,栈的特性是先进后出;
(2)在进队列的方法里咱们只要有容器能装元素就好了,因此直接往栈1里压;
(3)在出队列方法里,要保证出队列的是最早进入的元素:
最直观的想法就是把栈1的元素挨个出栈,而后往栈2里压。
(4)可是仍是要思考一下真的这么简单吗?不是的,栈2为空时才容许栈1往外弹元素并压到栈2里。
题目中的代码实现:
1 class Solution 2 { 3 public: 4 void push(int node) { 5 stack1.push(node); 6 } 7 8 int pop() { 9 if(stack2.empty()) 10 { 11 while(!stack1.empty()) 12 { 13 stack2.push(stack1.top()); 14 stack1.pop(); 15 } 16 } 17 int num = stack2.top(); 18 stack2.pop(); 19 return num; 20 } 21 22 private: 23 stack<int> stack1; 24 stack<int> stack2; 25 };
题目描述
已知一个奇怪的队列,这个队列中有n个数,初始状态时,顺序是1,2,3,4,…n,是1-n按顺序排列。这个队列只支持一种操做,就是把队列中的第i号元素提早到队首(1<i<=n),若有4个元素,初始为1,2,3,4,能够将3提早到队首,获得3,1,2,4 。 如今给出一个通过若干次操做以后的序列,请你找出这个序列至少是由原序列操做了多少次获得的。
输入描述:
第一行是一个整数n(1<=n<=10^5),表示序列中数字的数量。 接下来一行有n个数,是1-n的一个全排列。数字之间用空格隔开。
输出描述:
输出仅包含一个数字,表示至少操做了几回
解析:
1 输入: 2 5 3 5 2 1 3 4 4 输出: 5 2
代码:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 vector<int> array; 6 int main() 7 { 8 int n,num; 9 int count=0; 10 cin>>n; 11 for(int i=0;i<n;i++) 12 { 13 cin>>num; 14 array.push_back(num); 15 } 16 int res = n-1; //最多移动n-1次,好比输入5个数字:3 4 5 2 1能够用1 2 3 4 5通过4次移动获得 17 for(int i=n-1;i>0;i--) 18 { 19 if((array[i] > array[i-1])) //没有平移过的确定是排序好了的 20 res--; 21 else 22 break; //出现一个array[i] < array[i-1]则退出for循环 23 } 24 cout<<res<<endl; 25 26 return 0; 27 }
一、假定x和y为double型,则表达式x=2,y=x+3/2的值是3.00000
3/2是表示int型,int型自动取整变为1,x表示是浮点型,浮点型加整形转换为y 浮点型
二、数组的定义方法
三、计算机操做系统的功能是:管理计算机资源并提供用户接口
四、服务器相关
五、不仔细
六、软件调试技术:
七、如下值不等于3的表达式是_____________()
八、如下代码的输出结果是:
1 #include <stdio.h> 2 main() 3 { 4 char a[10]={ ‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,0},*p; 5 int i; 6 i=8; 7 p=a+i; 8 printf("%s\n",p-3); 9 }
1 输出会把剩下的所有输出!!!参数是头地址! 2 最后一个数字0对应转义字符 ‘\0’,为C语言中字符串的结尾。 3 由于输出%s,结果是6789; 4 若是输出%c,结果是6; 5 字符串的结束标志是0和'\0',注意'0'不是结束标志.strlen是以0和'\0'计算长度的 不包含0和'\0'。sizeof计算占用字节大小时包含0和'\0'所占的字节
%s是输出字符串,这个数组是字符串数组,`/0’是在最后,因此会将/0以前的全部数据所有输出
九、直接递归调用和间接递归调用
直接递归调用就是在函数a(或过程)中直接引用(调用)函数a自己
间接递归调用就是在函数a(或过程)中调用另一个函数b,而该函数b又引用(调用)了函数a
十、从编程的角度考虑,是否存在 i+1<i ?
存在,加入计算机系统是32位的,int i = 4294967296 - 1; i = i+1; 此时i = 0,即i+1<i (注4294967296是2的32次方)
十一、如何判断一个操做系统是16位的仍是32位的?
十二、字符串“alibaba”有 () 个不一样的排列?
方法一:
7个字符的全排列是7!=5040,又3个a和2个b有重复,因此总的是7!/2!/3!= 420
方法二:
1 ... 2 string str1 = "Hello-67aa"; 3 string str2; 4 for(int i=0; i<str1.size();i++) 5 str2[i] = str1[i]; 6 7 这样的赋值操做是不对的,str将始终为空!! 8 直接使用str2 = str`就行了