Two sum:

哈希表解法;

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
//只有唯一的一个解,且nums里的数不能重复使用
//hash
unordered_map<int, int> index;
for(int i=; i<nums.size(); i++)
index[nums[i]] = i; for(int i=; i<nums.size(); i++){
int left = target - nums[i];
if(index.count(left) && index[left]!=i)
//能在index中找到另一个数与nums[i]相加为target
return {i, index[left]}; //返回索引
}
return {}; //找不到解,返回空vector
}
};

注意这两个元素不能是相同的。

解法一:二分查找法,逐一取数组中的值,然后second = target - numbers[i] , 用二分查找法求第二个值。

时间复杂度:O(nlongn)

class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
//二分查找
vector<int> result;
int n = numbers.size();
for(int i=; i<n;i++){
int second = target - numbers[i];
int l = i+, r = n-;
while(l<=r){
int mid = (l+r)/;
if(second < numbers[mid]){
//在左半部分
r = mid-;
}
else if(second > numbers[mid]){
//在右半部分
l = mid+;
}
else{
//返回索引,从1开始
result.push_back(i+);
result.push_back(mid+);
break;
}
}
if(result.size()==) break;
}
return result;
}
};

解法三:对撞指针

使用两个指针,若nums[i] + nums[j] > target 时,i++; 若nums[i] + nums[j] < target 时,j -- 。

时间复杂度:O(n)

class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int n = numbers.size();
int l = , r = n-;
while(l<r){
if(numbers[l] + numbers[r] == target){
int res[] = {l+, r+};
return vector<int>(res, res+);
}
else if(numbers[l] + numbers[r] < target)
l++;
else
r--;
}
throw invalid_argument("The input has no solution.");
}
};
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
//只有唯一的一个解,且nums里的数不能重复使用
//hash
unordered_map<int, int> index;
for(int i=; i<nums.size(); i++)
index[nums[i]] = i; for(int i=; i<nums.size(); i++){
int left = target - nums[i];
if(index.count(left) && index[left]!=i)
//能在index中找到另一个数与nums[i]相加为target
return {i, index[left]}; //返回索引
}
return {}; //找不到解,返回空vector
}
};

Two sum's follow up :

//
// main.cpp
// Two sum 的follow up
// 在数组中找两个元素使它们的和大于9,返回元素对的个数
// sort + 双指针
// ans = ans + (j-i); 当前有j-i个元素对大于target
//
#include<bits/stdc++.h>
using namespace std;
int main() {
// insert code here...
vector<int> nums{,,,,,,};
int target = ;
sort(nums.begin(), nums.end());
int left = , right = nums.size()-;
int ans = ;
while(left < right){
if(nums[left] + nums[right] > target){
ans += (right - left);
right--;
}
else{
left++;
}
}
cout << "ans: "<<ans<<endl;;
return ;
}

对撞指针的另一个题目:

空串也认为是回文串。若 isalnum() == true,则为字母或数字;使用toupper()将其转换为大写。

#include <ctype.h>
class Solution {
public:
bool isPalindrome(string s) {
int l = , r = s.size()-;
while(l<r){
//跳过非字母和数字的字符
while(!isalnum(s[l]) && l<r)
l++;
while(!isalnum(s[r]) && l<r)
r--;
//将字母或数字都转化为大写来比较是否相同
if(toupper(s[l]) != toupper(s[r]))
return false;
l++;
r--;
}
return true;
}
};

344 Reverse String

还蛮简单的,用了对撞指针的思想,交换首尾对应指针所指的元素的值。

class Solution {
public:
string reverseString(string s) {
int l = , r = s.size()-;
int mid = (l+r)/;
for(int i=;i<=mid;i++){
swap(s[l], s[r]);
l++;
r--;
}
return s;
}
};

345

翻转元音字母:aeiouAEIOU

class Solution {
public:
bool is_vowel(char c){
if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u')||(c=='A')||(c=='E')||(c=='I')||(c=='O')||(c=='U'))
return true;
else
return false;
}
string reverseVowels(string s) {
int n = s.size();
int l = , r = n-; while(l<r){
while(!is_vowel(s[l]) && l<r)
l++;
while(!is_vowel(s[r]) && l<r)
r--;
swap(s[l], s[r]);
l++;
r--;
}
return s;
}
};

11

class Solution {
public:
int maxArea(vector<int> &height) {
int m = ;
int i = , j = height.size() - ;
while (i < j) {
//m = max(m, (j - i) * min(height[i], height[j]));
//height[i] < height[j] ? i++ : j--;
if(height[i] < height[j]){
m = max(m, (j - i) * height[i]);
i++;
}
else{
m = max(m, (j - i) * height[j]);
j--;
}
}
return m;
}
};

167 Two Sum-Input array is sorted, 125 Valid Palindrome,344的更多相关文章

  1. 608. Two Sum - Input array is sorted【medium】

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  2. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  3. 29. leetcode 167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...

  4. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  5. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  6. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  7. 167. Two Sum II - Input array is sorted@python

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  8. 1 & 167. Two Sum I & II ( Input array is sorted )

    Input array is sorted: Use binary search or two pointers Unsorted: Use hash map, key = target - a[i] ...

  9. leetcode2 Two Sum II – Input array is sorted

    Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...

随机推荐

  1. ./run.sh --indir examples/demo/ --outdir examples/results/ --vis

    (AlphaPose20180911) luo@luo-ThinkPad-W540:AlphaPose$ ./run.sh --indir examples/demo/ --outdir exampl ...

  2. while循环 for循环的理解

    不管是while循环还是for循环都隐含着一个if else的结构,就是说,if 条件满足,那么就执行循环体内部的语句,else就做循环体外部的事情. 有一个例子我觉得特别典型,程序内部定义了一个特定 ...

  3. Smarty3——变量修饰器

    变量修饰器可以用于变量, 自定义函数或者字符串. 使用修饰器,需要在变量的后面加上|(竖线)并且跟着修饰器名称. 修饰器可能还会有附加的参数以便达到效果. 参数会跟着修饰器名称,用:(冒号)分开. 同 ...

  4. Django rest_framework----序列化组件

    生成hypermedialink serializer.pclass BookModelSerializers(serializers.ModelSerializer): class Meta: mo ...

  5. SQL Server 本地时间和UTC时间的相互转换

    SET @UTCDate = DATEADD(hour, DATEDIFF(hour,GETDATE(),GETUTCDATE()), @LocalDate) SET @LocalDate2 = DA ...

  6. 何为软件的Alpha、Beta、RC和GA发布版本?

    简介 一个软件或者一个功能在发布时,通常会有Beta版这么一说.我很熟悉,差不多知道是什么意思,但没去深究,感觉上就是一个可以用但不保证功能稳定的版本. 直到昨天我看到了 MariaDB 数据库发布标 ...

  7. 《the art of software testing》第六章

    更高级别的测试 模块测试的目的是发现程序模块与其接口规格说明之间的不一致 功能测试的目的是为了证明程序未能符合其外部规格说明 系统测试目的是为了证明软件产品与其初始目标不一致 功能测试,作者从三个方面 ...

  8. Ubuntu16.04修改静态ip地址

    https://blog.csdn.net/mdw5521/article/details/79270035

  9. ComicEnhancerPro 系列教程二十:用“文件比较”看有损、无损

    作者:马健邮箱:stronghorse_mj@hotmail.com 主页:http://www.comicer.com/stronghorse/ 发布:2017.07.23 教程二十:用“文件比较” ...

  10. linux学习之路(4)

    用户身份与文件权限 通过uid来区分:  管理员 UID 为 0:系统的管理员用户. 系统用户 UID 为 1-999: Linux 系统为了避免因某个服务程序出现漏洞而被黑客提 权至整台服务器,默认 ...