参考了前令的博客。
class Solution {
public:
void sortColors(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int left=0,mid=0,right=n-1;
while(mid<=right){//最初写的是mid<right
while(A[right]==2) right--;
if(mid>right) break;//最初写的是mid>=right
swap(A,right,mid);
if(A[mid]==1) mid++;
else{
swap(A,left,mid);
left++;
mid++;
}
}
}
void swap(int A[],int i,int j){
int tmp=A[i];
A[i]=A[j];
A[j]=tmp;
}
};
[0,1]这种测试样例不过,修改程序mid<=right和mid>right我自己写了个i从-1开始的版本,仿照快排的思想:
class Solution {
public:
void sortColors(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(A==NULL||n<=0) return;
int i=-1,j=0,k=n-1;
while(j<=k){
while(A[k]==2) k--;
if(j>k) break;
swap(A,k,j);
if(A[j]==1) j++;
else if(A[j]==0){
i++;
swap(A,i,j);
j++;
}
}
}
void swap(int A[],int i,int j){
int tmp=A[i];
A[i]=A[j];
A[j]=tmp;
}
};
Recover Binary Search Tree
思路:中序遍历,使用n1,n2两个指针表示需要交换的两个节点
如何得到n1: 第一个比后续节点大的节点
如何得到n2: 最后一个前面节点大于后面节点
代码:
void proc(TreeNode *root, TreeNode *&n1, TreeNode *&n2, TreeNode *&prev)
{
if(!root)
return;
proc(root->left,n1,n2,prev);
if(prev && prev->val > root->val)
{
n2 = root;
if(!n1)
n1 = prev;
}
prev = root;
proc(root->right,n1,n2,prev);
}
void recoverTree(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
TreeNode *n1 = NULL;
TreeNode *n2 = NULL;
TreeNode *prev = NULL;
proc(root,n1,n2,prev);
if(n1 && n2)
swap(n1->val,n2->val);
}
Length of Last Word
class Solution {
public:
int lengthOfLastWord(const char *s) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(s==NULL) return 0;
int res=0,len=strlen(s);
int i=len-1;
while(i>=0&&s[i]==' '){i--;}
while(i>=0&&s[i]!=' '){res++;i--;}
return res;
}
};
search 2D Matrix
两次二分查找,时间复杂度为O(lgm)+O(lgn)
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(matrix.empty()) return false;
int row=matrix.size(),col=matrix[0].size();
vector<int> vec;
for(int i=0;i<row;i++)
vec.push_back(matrix[i][col-1]);
int pos=findFirstGreaterThan(vec,target);
if(pos>=row) return false;
else if(matrix[pos][col-1]==target)
return true;
else
return bsearch(matrix[pos],target);
}
int findFirstGreaterThan(vector<int> &vec,int target){
int l=0,r=vec.size()-1;
while(l<=r){
int mid=l+(r-l)/2;
if(vec[mid]>target) r=mid-1;
else if(vec[mid]==target) return mid;
else
l=mid+1;
}
return l;
}
bool bsearch(vector<int> &vec,int target){
int l=0,r=vec.size()-1;
while(l<=r){
int mid=l+(r-l)/2;
if(vec[mid]==target) return true;
else if(vec[mid]<target) l=mid+1;
else r=mid-1;
}
return false;
}
};
Anagrams
class Solution {
public:
map<string,int> mp;
vector<string> anagrams(vector<string> &strs) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<string> res;
if(strs.empty()) return res;
mp.clear();
for(int i=0;i<strs.size();i++){
string tmp=strs[i];
sort(tmp.begin(),tmp.end());
mp[tmp]++;
}
for(int i=0;i<strs.size();i++){
string tmp=strs[i];
sort(tmp.begin(),tmp.end());
if(mp.count(tmp)&&mp[tmp]>1){
res.push_back(strs[i]);
}
}
return res;
}
};
class Solution {
public:
bool isScramble(string s1, string s2) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(s1.length()!=s2.length()) return false;
if(s1.length()==1) return s1[0]==s2[0];
string tmp1=s1,tmp2=s2;
sort(tmp1.begin(),tmp1.end());
sort(tmp2.begin(),tmp2.end());
if(!comp(tmp1,tmp2)) return false;
for(int i=1;i<s1.length();i++){
string s11=s1.substr(0,i);
string s12=s1.substr(i);
string s21=s2.substr(0,i);
string s22=s2.substr(i);
if(isScramble(s11,s21)&&isScramble(s12,s22)) return true;
s21=s2.substr(0,s1.length()-i);
s22=s2.substr(s1.length()-i);
if(isScramble(s11,s22)&&isScramble(s12,s21)) return true;
}
return false;
}
bool comp(string s1,string s2){
for(int i=0;i<s1.length();i++)
if(s1[i]!=s2[i]) return false;
return true;
}
};
Largest Rectangle in Histogram
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(height.empty()) return 0;
int len=height.size(),res=0;
int l[len],r[len];
for(int i=0;i<len;i++){
l[i]=i;
while(l[i]-1>=0&&height[i]<=height[l[i]-1])
l[i]=l[l[i]-1];
}
for(int i=len-1;i>=0;i--){
r[i]=i;
while(r[i]+1<len&&height[i]<=height[r[i]+1])
r[i]=r[r[i]+1];
}
for(int i=0;i<len;i++)
if((r[i]-l[i]+1)*height[i]>res)
res=(r[i]-l[i]+1)*height[i];
return res;
}
};
Remove Duplicates from Sorted List II
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(head==NULL) return NULL;
ListNode *begin=head, *end=head,*pre=NULL,*res=NULL;
int cnt=0,times=0;
while(begin!=NULL){
cnt=0;
while(end!=NULL&&end->val==begin->val){
end=end->next;
cnt++;
}
if(cnt==1){
times++;
if(times==1) res=begin;
if(pre==NULL) pre=begin;
else{
pre->next=begin;
pre=begin;
}
}
begin=end;
}
if(pre==NULL) return NULL;
else{
pre->next=NULL;
return res;
}
}
};
Remove Duplicates from Sorted List
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(head==NULL) return NULL;
ListNode *begin=head, *end=head,*res=head;
while(begin!=NULL){
while(end!=NULL&&begin->val==end->val)
end=end->next;
begin->next=end;
begin=end;
}
return res;
}
};
Word Search 没想到可以一遍AC,超时的时候改了一下将flag置为了true
class Solution {
public:
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int vis[1000][1000];
bool exist(vector<vector<char> > &board, string word) {
// Note: The Solution object is instantiated only once and is reused by each test case.
bool flag=false;
memset(vis,0,sizeof(vis));
for(int i=0;i<board.size();i++)
for(int j=0;j<board[0].size();j++){
if(board[i][j]==word[0]){
flag=false;
dfs(i,j,board,word,0,flag);
vis[i][j]=0;
if(flag) return true;
}
}
return false;
}
void dfs(int x,int y,vector<vector<char> > &board,string word,int index,bool &flag){
if(flag||board[x][y]!=word[index]) return;
vis[x][y]=1;
if(index==word.length()-1){flag=true;return;}
for(int i=0;i<4;i++){
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(xx>=0&&xx<board.size()&&yy>=0&&yy<board[0].size()&&vis[xx][yy]==0){
vis[xx][yy]=1;
dfs(xx,yy,board,word,index+1,flag);
vis[xx][yy]=0;
}
}
}
};
Rotate List:
有一点需要注意,k=0的时候
class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(head==NULL) return NULL;
if(k==0) return head;
int len=getLen(head);
k=k%len;
if(k==0) return head;
ListNode *pre=NULL,*slow=head,*fast=head;
for(int i=0;i<k-1;i++)
fast=fast->next;
while(fast->next!=NULL){
fast=fast->next;
pre=slow;
slow=slow->next;
}
if(pre==NULL) return head;
else{
fast->next=head;
pre->next=NULL;
return slow;
}
}
int getLen(ListNode *head){
int res=0;
while(head){
res++;
head=head->next;
}
return res;
}
};
class Solution {
public:
int sqrt(int x) {
// Note: The Solution object is instantiated only once and is reused by each test case.
long long l=0,r=x,mid,times=0;
while(l<=r){
mid=(l+r)/2;
if(mid*mid>x) r=mid-1;
else if(mid*mid<x) l=mid+1;
else{
return mid;
}
}
return r;
}
};