167 Two Sum-Input array is sorted, 125 Valid Palindrome,344
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的更多相关文章
- 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 ...
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 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 ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 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 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 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 ...
- 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] ...
- 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], ...
随机推荐
- 2014蓝桥杯B组初赛试题《李白打酒》
题目描述: 话说大诗人李白,一生好饮.幸好他从不开车. 一天,他提着酒壶,从家里出来,酒壶中有酒2斗.他边走边唱: 无事街上走,提壶去打酒. 逢店加一倍,遇花喝一斗. 这一路上 ...
- 682. Baseball Game 棒球游戏 按字母处理
[抄题]: You're now a baseball game point recorder. Given a list of strings, each string can be one of ...
- Luogu 3206 [HNOI2010]城市建设
BZOJ 2001 很神仙的cdq分治 先放论文的链接 顾昱洲_浅谈一类分治算法 我们考虑分治询问,用$solve(l, r)$表示询问编号在$[l, r]$时的情况,那么当$l == r$的时候 ...
- Entity Framework edmx(mapping文件)
<?xml version="1.0" encoding="utf-8"?><edmx:Edmx Version="2.0" ...
- hdu 4741 Save Labman No.004 (异面直线的距离)
转载学习: #include <cstdio> #include <cstdlib> #include <cstring> #include <algorit ...
- select样式调整
如果select样式如下图:是因为添加了 border-color:#adb7d6; border-width:1px; 样式 删除上面两个样式属性,效果如下图:
- (转)基于 WPF + Modern UI 的 公司OA小助手 开发总结
原文地址:http://www.cnblogs.com/rainlam163/p/3365181.html 前言: 距离上一篇博客,整整一个月的时间了.人不能懒下来,必须有个阶段性的总结,算是对我这个 ...
- poj2299 Ultra-QuickSort(线段树求逆序对)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- 团队项目第六周-Alpha阶段项目复审(深海划水队)
经小组讨论后得出以下排名: 队名 优点 缺点 排名 大猪蹄子队 界面优美,功能简洁易懂,单词解释较为完善 互动方式.操作简易性有待优化,有部分功能尚未完成 1 Running Duck队 基本功能已经 ...
- 从头开始学eShopOnContainers——Visual Studio 2017环境配置
一.安装和配置Docker环境 1.安装Docker CE for Windows 从官方网站下载并安装,https://docs.docker.com/docker-for-windows/inst ...