# 手撕代码1

### 一、排序

1.快速排序

int partition(vector<int>&arr, int low, int high){
int pivot = arr[low];
while(low < high){
while(low < high && arr[high] >= pivot)
--high;
arr[low] = arr[high];
while(low < high && arr[low] <= pivot)
++low;
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}

void qsort(vector<int>&arr, int low, int high)
{
while(low < high){
int pivot = partition(arr, low, high);
qsort(arr, low, pivot-1);
low = pivot + 1;
}
}


2.堆排序

void HeapAdjust(vector<int>&arr, int root, int end){
int temp = arr[root];
for(int i = 2*root+1; i < end; i*=2+1){
if(i<end-1 && arr[i] < arr[i+1])
i++;
if(temp < arr[i]){
arr[root] = arr[i];
root = i;
}
else break;
}
arr[root] = temp;
}

void HeapSort(vector<int>&arr){
int len = arr.size();
for(int i = len/2-1; i >= 0; i--)
for(int i = len-1; i >= 1; i--){
swap(arr[0], arr[i]);
}
}


3.归并排序

void Merge(int*arr, int left, int mid, int right){
int i = left, j = mid + 1, k = 0;
int *temp = new int[right - left];
while(i <= mid && j <= right){
if(arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while(i <= mid)
temp[k++] = arr[i++];
while(j <= right)
temp[k++] = arr[j++];
for(i = 0; i < k; i++)
arr[left+i] = temp[i];
delete[] temp;
}

void MergeSort(int* arr, int left, int right){
if(left < right){
int mid = (left + right)/2;
MergeSort(arr, left, mid);
MergeSort(arr, mid+1, right);
Merge(arr, left, mid, right);
}
}


4.冒泡排序

void BubbleSort(vector<int>&arr){
int len = arr.size();
bool flag = true;
for(int i = 0; i < len && flag; i++){
flag = false;
for(int j = len - 1; j > i; j--){
if(arr[j] < arr[j-1]){
swap(arr[j], arr[j-1]);
flag = true;
}
}
}
}


### 二、字符串

1.字符串反转

string reverseString(string s) {
if(s.size() == 0) return s;
int len = s.size();
for(int i = 0; i < len/2; i++){
swap(s[i], s[len - i -1]);
}
return s;
}


2.判断是否为回文串

bool isPalindrome(string s) {
int len = s.size();
if(s.empty()) return true;
int i = 0, j = len-1;
while(i < j){
while(i < j && !isAlpNum(s[i])) ++i;
while(i < j && !isAlpNum(s[j])) --j;
if(i >= j) break;
if(s[i] != s[j]) return false;
++i;
--j;
}
return true;
}
bool isAlpNum(char &c){
c = lowerCase(c);
if(c >= '0' && c <= '9' || c >= 'a' && c <= 'z')
return true;
else
return false;
}
char lowerCase(char c){
if(c >='A' && c <= 'Z')
return 'a' + c - 'A';
else
return c;
}


3.拆分所有回文串

vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<string> cur;
dfs(s,cur,res);
return res;
}

bool isPalindrome(string s){
return s==string(s.rbegin(),s.rend());
}

void dfs(string s,vector<string> &cur,vector<vector<string>> &res){
if (s==""){
res.push_back(cur);
return;
}

for (int i = 1; i <= s.length(); ++i) {
string sub=s.substr(0,i);
if (isPalindrome(sub)){
cur.push_back(sub);
dfs(s.substr(i,s.length()-i),cur,res);
cur.pop_back();
}
}
}


4.最长公共子串

5.实现strStr()，子串第一次出现的位置

int strStr(string haystack, string needle) {
int len2=needle.size();
int len1=haystack.size();
int n=0;
if(len2==0)return 0;
if(len1<len2)return -1;
for(int i=0;i<len1-len2+1;i++){
int j=0;
for(;j<len2;j++){
if(haystack[i+j]!=needle[j]) break;
}
if(j==len2) return i;
}
return -1;
}


6.字符串转换为整数

1.链表公共节点

2.链表反转

3.是否有环

4.