Two sum:

哈希表解法;

  1. class Solution {
  2. public:
  3. vector<int> twoSum(vector<int>& nums, int target) {
  4. //只有唯一的一个解,且nums里的数不能重复使用
  5. //hash
  6. unordered_map<int, int> index;
  7. for(int i=; i<nums.size(); i++)
  8. index[nums[i]] = i;
  9.  
  10. for(int i=; i<nums.size(); i++){
  11. int left = target - nums[i];
  12. if(index.count(left) && index[left]!=i)
  13. //能在index中找到另一个数与nums[i]相加为target
  14. return {i, index[left]}; //返回索引
  15. }
  16. return {}; //找不到解,返回空vector
  17. }
  18. };

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

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

时间复杂度:O(nlongn)

  1. class Solution {
  2. public:
  3. vector<int> twoSum(vector<int>& numbers, int target) {
  4. //二分查找
  5. vector<int> result;
  6. int n = numbers.size();
  7. for(int i=; i<n;i++){
  8. int second = target - numbers[i];
  9. int l = i+, r = n-;
  10. while(l<=r){
  11. int mid = (l+r)/;
  12. if(second < numbers[mid]){
  13. //在左半部分
  14. r = mid-;
  15. }
  16. else if(second > numbers[mid]){
  17. //在右半部分
  18. l = mid+;
  19. }
  20. else{
  21. //返回索引,从1开始
  22. result.push_back(i+);
  23. result.push_back(mid+);
  24. break;
  25. }
  26. }
  27. if(result.size()==) break;
  28. }
  29. return result;
  30. }
  31. };

解法三:对撞指针

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

时间复杂度:O(n)

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

Two sum's follow up :

  1. //
  2. // main.cpp
  3. // Two sum 的follow up
  4. // 在数组中找两个元素使它们的和大于9,返回元素对的个数
  5. // sort + 双指针
  6. // ans = ans + (j-i); 当前有j-i个元素对大于target
  7. //
  8. #include<bits/stdc++.h>
  9. using namespace std;
  10. int main() {
  11. // insert code here...
  12. vector<int> nums{,,,,,,};
  13. int target = ;
  14. sort(nums.begin(), nums.end());
  15. int left = , right = nums.size()-;
  16. int ans = ;
  17. while(left < right){
  18. if(nums[left] + nums[right] > target){
  19. ans += (right - left);
  20. right--;
  21. }
  22. else{
  23. left++;
  24. }
  25. }
  26. cout << "ans: "<<ans<<endl;;
  27. return ;
  28. }

对撞指针的另一个题目:

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

  1. #include <ctype.h>
  2. class Solution {
  3. public:
  4. bool isPalindrome(string s) {
  5. int l = , r = s.size()-;
  6. while(l<r){
  7. //跳过非字母和数字的字符
  8. while(!isalnum(s[l]) && l<r)
  9. l++;
  10. while(!isalnum(s[r]) && l<r)
  11. r--;
  12. //将字母或数字都转化为大写来比较是否相同
  13. if(toupper(s[l]) != toupper(s[r]))
  14. return false;
  15. l++;
  16. r--;
  17. }
  18. return true;
  19. }
  20. };

344 Reverse String

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

  1. class Solution {
  2. public:
  3. string reverseString(string s) {
  4. int l = , r = s.size()-;
  5. int mid = (l+r)/;
  6. for(int i=;i<=mid;i++){
  7. swap(s[l], s[r]);
  8. l++;
  9. r--;
  10. }
  11. return s;
  12. }
  13. };

345

翻转元音字母:aeiouAEIOU

  1. class Solution {
  2. public:
  3. bool is_vowel(char c){
  4. if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u')||(c=='A')||(c=='E')||(c=='I')||(c=='O')||(c=='U'))
  5. return true;
  6. else
  7. return false;
  8. }
  9. string reverseVowels(string s) {
  10. int n = s.size();
  11. int l = , r = n-;
  12.  
  13. while(l<r){
  14. while(!is_vowel(s[l]) && l<r)
  15. l++;
  16. while(!is_vowel(s[r]) && l<r)
  17. r--;
  18. swap(s[l], s[r]);
  19. l++;
  20. r--;
  21. }
  22. return s;
  23. }
  24. };

11

  1. class Solution {
  2. public:
  3. int maxArea(vector<int> &height) {
  4. int m = ;
  5. int i = , j = height.size() - ;
  6. while (i < j) {
  7. //m = max(m, (j - i) * min(height[i], height[j]));
  8. //height[i] < height[j] ? i++ : j--;
  9. if(height[i] < height[j]){
  10. m = max(m, (j - i) * height[i]);
  11. i++;
  12. }
  13. else{
  14. m = max(m, (j - i) * height[j]);
  15. j--;
  16. }
  17. }
  18. return m;
  19. }
  20. };

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. Python04 range()方法的使用、turtle.textinput()方法和write()的使用、turtle.numinput()的使用

    1 range() 方法的使用 1.1 range方法介绍 range方法会返回一个range类型的对象,该对象会根据range方法的参数产生一些列整型数据 技巧01:range方法有三个参数,第一个 ...

  2. Auto Control 001 自动控制的一般概念

    自动控制的基本概念 一 . 自动控制系统的组成 自动控制装置:自动控制装置的组成当中涉及到了这样这样一些东西: 第1,需要有被控对象,那么这些被控对象需要有谁来控制呢?一定要有控制器,这些控制器,我们 ...

  3. CMake代码示例

    CMake代码示例(注:此文只贴了部分示例代码,详细文章见最后参考文章): 1.为工程和可执行文件指定一个版本号. 虽然可以在源代码中唯一指定它,但是在CMakeLists文件中指定它可以提供更好的灵 ...

  4. Map-making Robots: A Review of the Occupancy Grid Map Algorithm

    栅格地图算法:http://www.ikaros-project.org/articles/2008/gridmaps/

  5. MVC5应用程序生命周期lifecycle

  6. xen创建pvm和hvm的过程

    these are the basic steps of installing domU with xen-tools in ubuntu13.04 64bit in xen4.3 you can a ...

  7. 编写高质量代码改善C#程序的157个建议——建议25:谨慎集合属性的可写操作

    建议25:谨慎集合属性的可写操作 如果类型的属性中有集合属性,那么应该保证属性对象是由类型本身产生的.如果将属性设置为可写,则会增加抛出异常的几率.一般情况下,如果集合属性没有值,则它返回的Count ...

  8. wpf使用truetype字体ttf

    查了半天都是语焉不详,这篇算是稍微详细点的:http://www.cnblogs.com/junhengml/p/6878933.html 要先查找到字体的字库名称,才能使用: <Window. ...

  9. [转]xe6 android 使用距离传感器(Proximiry)

    The first step it's a run sample from RAD Studio that named SensorInfo on your device. On the tab Bi ...

  10. Java简单实现AOP,Java通用异常拦截,Java与Lamada

    直接看代码不废话.不懂Lamada直接百度... package test; /** * QQ:1448376744 * @author 花间岛 * */ //控制器 public class Con ...