167 Two Sum II - Input array is sorted
给定一个有序整型数组和一个整数target,在其中寻找两个元素,使其和为target。返回这两个数的索引。
-如numbers = [2,7,11,15],target = 9;
-返回数字2,7的索引1,2(索引从1开始计算)
-如果没有解怎么样?保证有解
-如果有多个解怎样?返回任意解
#include
#include
#include
#include
using namespace std;
class Solution{
public:
vector twoSum(vector& numbers,int target){
assert(numbers.size() >= 2);
int l = 0;
int r = numbers.size() - 1;
while(l < r){
if(numbers[l] + numbers[r] == target){
int res[] = {l+1,r+1};
return vector(res,res+2);
}
else if(numbers[l] + numbers[r] < target){
l++;
}
else{
r--;
}
}
throw ("The input has no solution");
}
};
int main(){
int arr[] = {2,3,5,7,8,2};
vector vec(arr,arr+sizeof(arr)/sizeof(int));
vector ret = Solution().twoSum(vec,10);
for(int i = 0; i < ret.size(); i++){
cout << ret[i] << " ";
}
cout << endl;
return 0;
}
125.Valid Palindrome
给定一个字符串,只看其中的数字和字母,忽略大小写,判断这个字符串是否为回文串?
·“A man,a plan,a canal;Panama”是回文串
·“race a car”不是回文串
· 空字符串如何看?
· 字符的定义
· 大小写的问题
#include
#include
#include
using namespace std;
class Solution{
public:
bool validPalindrome(vector& vec){
assert(vec.size() > 0);
int l = 0;
int r = vec.size() -2;
while(l <= r)
{
if((vec[l] <= '9' && vec[l] >= '0') || (vec[l] >= 'a' && vec[l] <= 'z') || (vec[l] >= 'A' && vec[l] <= 'Z')){
if((vec[l] <= '9' && vec[r] >= '0') || (vec[r] >= 'a' && vec[r] <= 'z') ||( vec[r] >= 'A' && vec[r] <= 'Z')){
if(vec[l] == vec[r] || vec[l] - 32 == vec[r] || vec[l] + 32 == vec[r]){
l++;
r--;
}
else
{
return false;
}
}
else{
r--;
}
}
else{
l++;
}
}
return true;
}
};
int main(){
//char arr[] = "AmannamA";
char arr[] = "A man, a plan, a canal; Panama";
char arr2[] = "a run rice";
vector vec(arr,arr+sizeof(arr)/sizeof(char));
vector vec2(arr2,arr2+sizeof(arr2)/sizeof(char));
bool ret = Solution().validPalindrome(vec);
bool ret2 = Solution().validPalindrome(vec2);
if(ret == true){
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
}
if(ret2 == true){
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
}
return 0;
}
344.Reverse String
给定一个字符串,返回这个字符串的倒序字符串
#include
#include
#include
using namespace std;
class Solution{
public:
void ReverseString(vector& vec){
int l = 0;
int r = vec.size() -2;
while(l <= r){
swap(vec[l],vec[r]);
l++;
r--;
}
}
};
int main(){
char arr[] = "helloworld";
vector vec(arr,arr+sizeof(arr)/sizeof(char));
Solution().ReverseString(vec);
for(int i = 0; i < vec.size() - 1; i++){
cout << vec[i];
}
cout << endl;
return 0;
}
345.Reverse Vowels of a string
给定一个字符串,将该字符串中的元音字母翻转 a e i o u
·如:给出“hello”返回“holle”
·如:给出“leetcode”,返回“leotcede”
·元音字母不包含y
#include
#include
#include
using namespace std;
class Solution{
public:
void ReverseVowels(vector& vec){
int l = 0;
int r = vec.size() -2;
while(l <= r){
if(vec[l] == 'a' || vec[l] == 'e' || vec[l] == 'i' || vec[l] == 'o' || vec[l] == 'u'){
if(vec[r] == 'a' || vec[r] == 'e' || vec[r] == 'i' || vec[r] == 'o' || vec[r] == 'u'){
swap(vec[l],vec[r]);
}
l++;
r--;
}
else{
l++;
r--;
}
}
}
};
int main(){
char arr[] = "leetcode";
vector vec(arr,arr+sizeof(arr)/sizeof(char));
Solution().ReverseVowels(vec);
for(int i = 0; i < vec.size() - 1; i++){
cout << vec[i];
}
cout << endl;
return 0;
}
11.Container With Most Water
给出一个非负整数数组,a1,a2,a3,……,an;每个整数表示一个竖立在坐标轴x位置的一堵高度为ai的“墙”,选择两堵墙,和x轴构成的容器可以容纳最多的水
#include
#include
#include
using namespace std;
class Solution{
public:
int Min(int a, int b){
if(a <= b){
return a;
}
else{
return b;
}
}
vector MosrWater(vector& vec){
int l = 0;
int r = vec.size() - 1;
int result = 0;
int temp[3];
vector ret(temp,temp+sizeof(temp)/sizeof(int)) ;
result = Min(vec[l],vec[r]) * (r - l);
ret.clear();
ret.push_back(result);
ret.push_back(l);
ret.push_back(r);
while(l <= r){
if((Min(vec[l],vec[r]) * (r - l)) > result){
result = Min(vec[l],vec[r]) * (r - l);
ret.clear();
ret.push_back(result);
ret.push_back(l);
ret.push_back(r);
}
l++;
r--;
}
return ret;
}
};
int main(){
int arr[] = {4,8,4,7,8,9,10};
vector vec(arr,arr+sizeof(arr)/sizeof(int));
vector ret = Solution().MosrWater(vec);
for(int i = 0; i < ret.size(); i++){
cout << ret[i] << endl;
}
return 0;
}