classSolution{public:voidmerge(vector<int>& nums1,int m, vector<int>& nums2,int n){//1. 扩大数组为m+n,指针p从后往前遍历//2. 分别从后往前遍历俩数组,大的替换p//3. 循环条件,p>= 0;int p = m + n -1;int p1 = m -1;int p2 = n -1;while(p >=0){if(p1 <0){
nums1[p]= nums2[p2];
p2--;}elseif(p2 <0){
nums1[p]== nums1[p1];
p1--;}elseif(nums1[p1]<= nums2[p2]){
nums1[p]= nums2[p2];
p2--;}else{
nums1[p]= nums1[p1];
p1--;}
p--;}}};
5. 移除元素
classSolution{public:intremoveElement(vector<int>& nums,int val){if(nums.size()==0){return0;}int right = nums.size()-1;for(int i =0; i <= right; i++){while(right >=0&& nums[right]== val){
right--;}if(nums[i]== val && i < right){
nums[i]= nums[right];
nums[right]= val;}}return right +1;}};
6. 数组的交集
classSolution{public:
vector<int>intersection(vector<int>& nums1, vector<int>& nums2){
map <int,int> m;
vector<int> v;for(auto e : nums2){
m[e]++;}for(auto e : nums1){if(m[e]>0){
m[e]=0;
v.push_back(e);}}return v;}};
7. 旋转数组的最小数字
classSolution{public:intminNumberInRotateArray(vector<int> rotateArray){if(rotateArray.size()==0){return0;}int first =0, last = rotateArray.size()-1;while(first < last){int mid = first +((last - first)>>1);if(rotateArray[first]< rotateArray[last]){return rotateArray[first];}else{if(rotateArray[mid]> rotateArray[last]){
first = mid +1;}else{if(rotateArray[mid]<= rotateArray[last]){
last = mid;}}}}return rotateArray[first];}};
8.变态跳台阶
classSolution{public:intjumpFloorII(int number){if(number ==0|| number ==1)return number;int a =1,b;for(int i =2;i <= number; i++){
b = a <<1;
a = b;}return b;}};
9. 二进制1的个数
classSolution{public:intNumberOf1(int n){int count =0;while(n!=0){
count++;
n = n &(n-1);}return count;}};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/classSolution{public:
ListNode*rotateRight(ListNode* head,int k){if(head ==NULL){returnNULL;}
ListNode* left = head,*right = head;int n =0;
ListNode* p = head;while(p){
p = p -> next;
n++;}
k %= n;while(k--){if(right !=NULL){
right = right -> next;}}while(right->next !=NULL){
right = right -> next;
left = left -> next;}
right -> next = head;
head = left -> next;
left -> next =NULL;return head;}};
5. 移除链表元素
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/classSolution{public:
ListNode*removeElements(ListNode* head,int val){
ListNode *dummy =newListNode(0);
dummy->next = head;
ListNode *pre = dummy,*cur = head;while(cur !=NULL){if(cur -> val == val){
pre -> next = cur -> next;
cur = cur -> next;}else{
cur = cur -> next;
pre = pre -> next;}}return dummy -> next;}};
字符串
1. 有效括号
classSolution{public:boolisValid(string s){
stack<char> st;for(int i =0; i < s.size(); i++){switch(s[i]){case'(':
st.push('(');break;case'[':
st.push('[');break;case'{':
st.push('{');break;case')':if(st.empty())returnfalse;elseif(st.top()=='('){
st.pop();}else{returnfalse;}break;case']':if(st.empty())returnfalse;elseif(st.top()=='['){
st.pop();}else{returnfalse;}break;case'}':if(st.empty())returnfalse;elseif(st.top()=='{'){
st.pop();}else{returnfalse;}break;}}if(st.empty())returntrue;elsereturnfalse;}};
2. 字符串相加
classSolution{public:
string addStrings(string num1, string num2){int str1 = num1.size()-1;int str2 = num2.size()-1;
string ans;int result =0, add =0;int x =0, y =0;while(str1 >=0|| str2 >=0|| add !=0){if(str1 >=0){
x = num1[str1]-'0';}else{
x =0;}if(str2 >=0){
y = num2[str2]-'0';}else{
y =0;}
result = x + y + add;
ans.push_back('0'+ result %10);
add = result /10;
str1--;
str2--;}reverse(ans.begin(), ans.end());return ans;}};
3.只反转单词
classSolution{public:
string reverseWords(string s){if(s.size()==0){return s;}int n = s.size();int front =0;int back =0;for(int i =0; i < n -1; i++){if(s[back]!=' '){
back++;}else{reverse(s.begin()+ front, s.begin()+ back);
front = back +1;
back = front;}}
back++;reverse(s.begin()+ front, s.begin()+ back);return s;}};
4. 倒置字符串
#include<iostream>#include<string>#include<algorithm>usingnamespace std;intmain(){
string s;getline(cin,s);reverse(s.begin(),s.end());int len = s.size();int front =0, back =0;for(int i =0; i < len -1; i++){if(s[i]!=' '){
back++;}else{reverse(s.begin()+front,s.begin()+back);
front = back +1;
back = front;}}
back++;reverse(s.begin()+front,s.begin()+back);
cout<<s<<endl;return0;}
5.替换空格
classSolution{public:voidreplaceSpace(char*str,int length){if(str ==nullptr|| length <0){return;}int count =0;for(int i =0; i < length; i++){if(str[i]==' '){
count++;}}if(count ==0){return;}int new_length = length + count*2;for(int i = length; i >=0; i--){if(str[i]!=' '){
str[new_length]= str[i];
new_length--;}else{
str[new_length--]='0';
str[new_length--]='2';
str[new_length--]='%';}}}};
6. 最长不重复子串
classSolution{public:intlengthOfLongestSubstring(string s){//滑动窗口思想//哈希表存放字符出现的次数,//左右双指针,右指针增加找到符合不重复字符的窗口,左指针增加改变窗口大小来回//循环条件:出现重复元素
unordered_map<char,int> window;int left =0, right =0;int res =0;int n = s.size();while(right < n){char c1 = s[right];
window[c1]++;
right++;while(window[c1]>1){char c2 = s[left];
window[c2]--;
left++;}
res =max(res,right - left);}return res;}};