class Solution {
public:
void moveZeroes(vector<int>& nums) {
int n=nums.size();
int i=0;
while(find(nums.begin(),nums.end(),0)!=nums.end())
{
auto pos=find(nums.begin(),nums.end(),0);
nums.erase(pos);
}
nums.resize(n);
}
};
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int n=nums.size();
int i=-1,j=0;
while(j<n)
{
if(nums[j]!=val)
{
i++;
nums[i]=nums[j];
}
j++;
}
return i+1;
}
};
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n=nums.size();
int i=0,j=0;
if(n==0)return {};
while(j<n)
{
if(nums[j]!=nums[i])
{
i++;
nums[i]=nums[j];
}
j++;
}
return i+1;
}
};
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n=nums.size();
if(n<2)return n;
int i=1,j=2;
while(j<n)
{
if(nums[i-1]!=nums[j])
{
i++;
nums[i]=nums[j];
}
j++;
}
return i+1;
}
};
point:只有m个元素都比这n个元素都大的情况才可能拷贝不完整。若n都比m大则已经完成了排序。
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int k=m+n-1;
int i=m-1;
int j=n-1;
while(i>=0&&j>=0)
{
nums1[k--]=nums1[i]>nums2[j]?nums1[i--]:nums2[j--];
}
if(i<0)
{
while(j>=0)
{
nums1[k--]=nums2[j--];
}
}
}
};
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int n=numbers.size();
vector<int> ans;
int i=0,j=n-1;
while(i<j)
{
if(numbers[i]+numbers[j]==target)
{
ans.push_back(i+1);
ans.push_back(j+1);
break;
}
else if(numbers[i]+numbers[j]<target)
{
i++;
}
else
{
j--;
}
}
return ans;
}
};
islower(char c) 是否为小写字母
isupper(char c) 是否为大写字母
isdigit(char c) 是否为数字
isalpha(char c) 是否为字母
isalnum(char c) 是否为字母或者数字
toupper(char c) 字母小转大
tolower(char c) 字母大转小
class Solution {
public:
bool isPalindrome(string s) {
int n=s.length();
int i=0,j=n-1;
while(i<j)
{
while(i<j&&!isalnum(s[i]))i++;
while(i<j&&!isalnum(s[j]))j--;
cout<<s[i]<<' '<<s[j]<<endl;
if(tolower(s[i])!=tolower(s[j]))
{
return false;}
i++;
j--;
}
return true;
}
};
class Solution {
public:
bool isPalindrome(string s) {
string tmp;
for(char c:s)
{
if(islower(c)||isdigit(c))tmp+=c;
else if(isupper(c))tmp+=tolower(c);
}
cout<<endl;
int n=tmp.length();
int i=0,j=n-1;
while(i<j)
{
if(tmp[i]!=tmp[j])return false;
i++;
j--;
}
return true;
}
};
class Solution {
public:
string reverseVowels(string s) {
unordered_set<char> alpha={'A','E','I','O','U','a','e','i','o','u'};
int n=s.length();
int i=0,j=n-1;
while(i<j)
{
while(i<j&&!alpha.count(s[i]))i++;
while(i<j&&!alpha.count(s[j]))j--;
if(i<j)
{
swap(s[i],s[j]);
i++;
j--;
}
}
return s;
}
};
两个for循环超时,原则是每次移动较短边。(若i<j)
因为移动较短边相当于不考虑h[i]+h[j-1],h[j-2]…h[i+1]这些面积一定小于现在的h[i]+h[j]
class Solution {
public:
int maxArea(vector<int>& height) {
int n=height.size();
int ans=0;
int i=0,j=n-1;
while(i<j)
{
int sum=min(height[i],height[j])*(j-i);
ans=max(sum,ans);
if(height[i]<height[j])i++;
else j--;
}
return ans;
}
};
暴力法
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int n=nums.size();
int sum=0,len=n+1;
int m=0;
for(int i=0;i<n;i++)
{
int sum=0;
for(int j=i;j<n;j++)
{
sum+=nums[j];
if(sum>=target)
{
len=min(len,j-i+1);
break;
}
}
cout<<len<<' ';
}
return len==n+1?0:len;
}
};
双指针
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int n=nums.size();
int i=0,j=0,sum=0,len=n+1;
while(j<n)
{
sum+=nums[j];
while(sum>=target)
{
len=min(len,j-i+1);
sum-=nums[i];
i++;
}
j++;
}
return len==n+1?0:len;
}
};
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int n=nums.size();
vector<int> ans(n,0);
int i=0,j=n-1,k=n-1;
while(i<=j)
{
if(abs(nums[i])<abs(nums[j]))
{
ans[k]=nums[j]*nums[j];
j--;
}
else
{
ans[k]=nums[i]*nums[i];
i++;
}
k--;
}
return ans;
}
};
class Solution {
public:
bool backspaceCompare(string S, string T) {
return bulid(S)==bulid(T);
}
string bulid(string str)
{
string ans;
for(auto ch:str)
{
if(ch!='#')
{
ans.push_back(ch);
}
else if(!ans.empty())
{
ans.pop_back();
}
}
return ans;
}
};
双指针
class Solution {
public:
bool backspaceCompare(string S, string T) {
int n=S.length(),m=T.length();
int i=n-1,j=m-1;
int counti=0,countj=0;
while(i>=0||j>=0)
{
while(i>=0)
{
if(S[i]=='#')
{
counti++;
i--;
}
else if(counti>0)
{
counti--;
i--;
}
else
{
break;
}
}
while(j>=0)
{
if(T[j]=='#')
{
countj++;
j--;
}
else if(countj>0)
{
countj--;
j--;
}
else
{
break;
}
}
if(i>=0&&j>=0)
{
if(S[i]!=T[j])return false;
}
else if(i>=0||j>=0)
{
return false;
}
i--;
j--;
}
return true;
}
};