一、完成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`就行了