167 Two Sum-Input array is sorted, 125 Valid Palindrome,344

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

解法一:二分查找法,逐一取数组中的值,然后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=0; i<n;i++){
            int second = target - numbers[i];
            int l = i+1, r = n-1;
            while(l<=r){
                int mid = (l+r)/2;
                if(second < numbers[mid]){
                    //在左半部分
                    r = mid-1;
                }
                else if(second > numbers[mid]){
                    //在右半部分
                    l = mid+1;
                }
                else{
                    //返回索引,从1开始
                    result.push_back(i+1);
                    result.push_back(mid+1);
                    break;
                }
            }
            if(result.size()==2) 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 = 0, r = n-1;
        while(l<r){
            if(numbers[l] + numbers[r] == target){
                int res[2] = {l+1, r+1};
                return vector<int>(res, res+2);
            }
            else if(numbers[l] + numbers[r] < target)
                l++;
            else
                r--;
        }        throw invalid_argument("The input has no solution.");
    }
};

对撞指针的另一个题目:

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

#include <ctype.h>
class Solution {
public:
    bool isPalindrome(string s) {
        int l = 0, r = s.size()-1;
        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 = 0, r = s.size()-1;
        int mid = (l+r)/2;
        for(int i=0;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 = 0, r = n-1;

        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 = 0;
    int i = 0, j = height.size() - 1;
    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;
}
};

原文地址:https://www.cnblogs.com/Bella2017/p/10146882.html

时间: 2024-10-09 07:11:26

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

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

Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they a

leetcode2 Two Sum II – Input array is sorted

Two Sum II – Input array is sorted [email protected] Question: Similar to Question [1. Two Sum], except that the input array is already sorted in ascending order. 同上题:先排序,然后从开头和结尾同时向中间查找,原理也比较简单.O(nlogn) runtime, O(1) space vector<int> twoSumSored(v

LeetCode_167. Two Sum II - Input array is sorted

fsaf 167. Two Sum II - Input array is sorted Easy Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers s

Two Sum II - Input array is sorted(leetcode167) - Solution2

Q: Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index

Two Sum II - Input array is sorted(leetcode167) - Solution1

Q: Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index

LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 m

LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where i

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

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值(index1 和 index2)不是从零开始的. 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 = 1

[LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where index1 mu