27. 移除元素
int removeElement(vector<int>& nums, int val) {
if(nums.size()==0) return 0;
sort(nums.begin(),nums.end());
int res=0;
for(int i=0,j=nums.size()-1;i<nums.size();i++){
while(i<j&&nums[i]!=val) i++;
while(i<j&&nums[j]==val) j--;
int c;
c=nums[i];
nums[i]=nums[j];
nums[j]=c;
}
while(res<nums.size()&&nums[res]!=val) res++;
return res;
}
344. 反转字符串
void reverseString(vector<char>& s) {
// cout<<s.size()<<endl;
for(int i=0;i<s.size()/2;i++){
char c;
c=s[i];
s[i]=s[s.size()-i-1];
s[s.size()-i-1]=c;
}
}
剑指 Offer 05. 替换空格
string replaceSpace(string s) {
string res;
for(int i=0;i<s.size();i++){
if(s[i]==' ') res+="%20";
else res+=s[i];
}
return res;
}
151. 反转字符串中的单词
string reverseWords(string s) {
string res;
vector<string> ss;
string xx[1010];
int len=0;
// for(int i=0;i<test.size();i++)
for(int i=0,j=0;i<s.size();i++,j++){
if(s[i]==' ')continue;
string temp;
while(s[j]!=' '&&j<s.size()) {
temp+=s[j];
j++;
}
ss.push_back(temp);
len++;
i=j;
}
for(int i=0;i<=ss.size()/2;i++){
int j=ss.size()-i-1;
xx[i]=ss[j];
xx[j]=ss[i];
}
for(int i=0;i<len;i++){
// cout<<xx[i].length()<<endl;
for(int j=0;j<xx[i].length();j++){
res+=xx[i][j];
}
if(i!=len-1) res+=" ";
}
return res;
}
206. 反转链表
ListNode* reverseList(ListNode* head) {
if(!head) return NULL;
auto a=head,b=a->next;
while(b){
auto c=b->next;
b->next=a;
a=b,
b=c;
}
head->next=NULL;
return a;
}
19. 删除链表的倒数第 N 个结点
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(!head) return head;
auto a=head,b=a->next;
while(b){
auto c =b->next;
b->next=a;
a=b;
b=c;
}
head->next=NULL;
if(n==1){
a=a->next;
}else{
auto aa=a;
for(int i=1;i<=n;i++){
if(i==n-1){
aa->next=aa->next->next;
break;
}
aa=aa->next;
}
}
if(a!=NULL){
auto A=a,B=A->next;
while(B){
auto C =B->next;
B->next=A;
A=B;
B=C;
}
a->next=NULL;
return A;
}
return a;
}
面试题 02.07. 链表相交
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_map<int,ListNode*> map1;
auto aa=headA;
while(aa){
map1[aa->val]=aa;
aa=aa->next;
}
auto bb=headB;
while(bb){
if(map1[bb->val]==bb) return bb;
bb=bb->next;
}
return NULL;
}
142. 环形链表 II
ListNode *detectCycle(ListNode *head) {
unordered_map<ListNode*,int> map1;
auto aa=head;
while(aa){
// cout<<map1[aa]<<endl;
if(map1[aa]==1) return aa;
map1[aa]=1;
aa=aa->next;
}
return NULL;
}
15. 三数之和
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(),nums.end());
if(nums[0]>0) return res;
for(int i=0;i<nums.size();i++){
//-4 -1 -1 0 1 2
if(i>0&&nums[i]==nums[i-1]) continue;
int j=i+1,k=nums.size()-1;
while(j<k){
// printf("i=%d \n",i);
if(nums[i]+nums[j]+nums[k]>0) k--;
else if (nums[i]+nums[j]+nums[k]<0) j++;
else if(nums[i]+nums[j]+nums[k]==0){
vector<int> temp;
temp.push_back(nums[i]); temp.push_back(nums[j]); temp.push_back(nums[k]);
//printf("%d %d %d\n" ,i,j,k);
res.push_back(temp);
while (k > j && nums[k] == nums[k - 1]) k--;
while (k > j && nums[j] == nums[j + 1]) j++;
j++;
k--;
}
}
}
return res;
}
18. 四数之和
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(),nums.end());
vector<vector<int>> res;
if(nums[0]>target&&nums[0]>0) return res;
for(int i=0;i<nums.size();i++){
if(i>0&&nums[i]==nums[i-1]) continue;
for(int j=i+1;j<nums.size();j++){
if(j>i+1&&nums[j]==nums[j-1]) continue;
for(int n=j+1,m=nums.size()-1;n<m;){
if(nums[i]+nums[j]>target-nums[n]-nums[m]) m--;
else if(nums[i]+nums[j]<target-nums[n]-nums[m]) n++;
else {
vector<int> temp;
temp.push_back(nums[i]); temp.push_back(nums[j]);
temp.push_back(nums[n]); temp.push_back(nums[m]);
res.push_back(temp);
while(n<m&&nums[n]==nums[n+1]) n++;
while(n<m&&nums[m]==nums[m-1]) m--;
n++;m--;
}
}
}
}
return res;
}