这道题的思路是使用双指针:
i代表从数组最左边的元素进行遍历,如果该字母不是元音字母的话,继续遍历(i++),直到遇到元音字母为止,然后进行交换。
j代表从数组最右边的元素进行遍历,同样的若数组中的尾字母不是元音字母的话(j–),继续遍历,直到遇到元音字母为止,进行交换。 代码:
class Solution { public: string reverseVowels(string s) { int i=0,j=s.length()-1; while(i<j) { if(!Discri(s[i])) { i++;continue; } if(!Discri(s[j])) { j--;continue; } swap(s[i],s[j]); } return s; } bool Discri(char c) { return (c == 'a' || c == 'A' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U' || c == 'e' || c == 'E'); } };同样的,这道题的思路也是使用双指针:
很明显,使用i和j指针分别从数组的两边开始进行遍历。 容器中的装水量的计算其实很简单,装水量=(j-i)×(数组[i]或数组[j]中的最小的那个值)因为装水的体积做多的限制是木桶最短的那个。
so code is:
class Solution { public: int maxArea(vector<int>& height) { int res=0; int i=0; int j=height.size()-1; while(i<j) { int max1=(j-i)*min(height[i],height[j]); res=max(max1,res); if(height[i]<height[j]) { i++; } else { j--; } } return res; } };首先先介绍一下c++的一些特殊的函数:
1、isalnum(char c) 是否为字母或者数字 2、toupper(char c) 字母小转大 3、tolower(char c) 字母大转小
方案一:判断字符串是否为回文串,首先我们将数组中的空格进行空格和符号去掉,得到一个新的字符串d。我们对此字符串进行反转处理得到反转过后的字符串ds,最后d和ds进行对比,判断它们是否相同。
class Solution { public: bool isPalindrome(string s) { string d; for(char c:s) { if(isalnum(c)) { d+=tolower(c); } } string sd(d.rbegin(),d.rend()); return sd==d; } };方案二:
class Solution { public: bool isPalindrome(string s) { string d; for(char c:s) { if(isalnum(c)) { d+=tolower(c); } } int n=d.length()-1; int l=0; int r=n; while(l<r) { if(d[l]!=d[r]) return false; else { l++; r--; } } return true; } };方案三:
class Solution { public: bool isPalindrome(string s) { int l=0; int r=s.length()-1; while(l<r) { if(l<r&&!isalnum(s[l])) { l++; continue; } if(l<r&&!isalnum(s[r])) { r--; continue; } if(tolower(s[l])!=tolower(s[r])) { return false; } else { l++; r--; } } return true; } }; class Solution { public: vector<int> twoSum(vector<int>& numbers, int target) { // if (numbers.size()<3) //return {1,2}; int l=0; int r=numbers.size()-1; vector<int> res; while(l<r) { int sum=numbers[l]+numbers[r]; if(sum==target) { res.push_back(++l); res.push_back(++r); return res; } else if(sum<target) { l++; } else { r--; } } return{}; } };