双指针

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 must be less than index2.

Note:

Your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution and you may not use the same element twice.
Example:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        int idx1 = 0, idx2 = numbers.size()-1;
        while(idx1 <= idx2){
            if(numbers[idx1]+numbers[idx2]==target) return {idx1+1,idx2+1};
            else if(numbers[idx1]+numbers[idx2] > target) idx2--;
            else idx1++;
        }
        return {};
    }
};

633. Sum of Square Numbers

Given a non-negative integer c, your task is to decide whether there’re two integers a and b such that a2 + b2 = c.

Example 1:
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: 3
Output: False

class Solution {
public:
    bool judgeSquareSum(int c) {
        int i=0, j=int(sqrt(c));
        while(i<=j){
            int PowSum = i*i + j*j;
            if(PowSum == c) return true;
            else if(PowSum < c) ++i;
            else --j;
        }
        return false;
    }
};

345. Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = “hello”, return “holle”.

Example 2:
Given s = “leetcode”, return “leotcede”.

Note:
The vowels does not include the letter “y”.

class Solution {
public:
    string reverseVowels(string s) {
        int left = 0, right = s.size() - 1;
        string vowels = "aeiouAEIOU";
        while(left<=right){
            if(vowels.find(s[left]) == string::npos) ++left;
            else if(vowels.find(s[right]) == string::npos) --right;
            else swap(s[left++],s[right--]);
        }
        return s;
    }
};

680. Valid Palindrome II

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.

Example 1:
Input: “aba”
Output: True

class Solution {
public:
    bool validPalindrome(string s) {
        int left = 0, right = s.size() - 1;
        while(left < right){
            if(s[left] != s[right]) return isPalindrome(s,left,right-1) || isPalindrome(s,left+1,right);
            ++left;
            --right;
        }
        return true;
    }
    bool isPalindrome(string s, int left, int right){
        while(left < right){
            if(s[left] != s[right]) return false;
            ++left;
            --right;
        }
        return true;
    }
};

88. Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:

The number of elements initialized in nums1 and nums2 are m and n respectively.
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
Example:

Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3

Output: [1,2,2,3,5,6]

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int i1 = m - 1, i2 = n - 1, j = m + n - 1;
        while(i1 >= 0 && i2 >= 0){
            nums1[j--] = nums1[i1]>=nums2[i2] ? nums1[i1--] : nums2[i2--];
        }
        while(i2 >= 0){
            nums1[j--] = nums2[i2--];
        }
    }
};

141. Linked List Cycle

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode *slow = head, *fast = head;
        while(fast && fast->next){
            slow = slow->next;
            fast = fast->next->next;
            if (slow == fast) return true;
        }
        return false;
    }
};

524. Longest Word in Dictionary through Deleting

Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.

Example 1:
Input:
s = “abpcplea”, d = [“ale”,”apple”,”monkey”,”plea”]

Output:
“apple”

class Solution {
public:
    string findLongestWord(string s, vector<string>& d) {
        std::sort(d.begin(),d.end(),comp);
        for(string t : d){
            int ti=0;
            for(char c : s){
                if(ti<t.size() && c==t[ti]) ++ti;
            }
            if(ti==t.size()) return t;
        }
        return "";
    }
private:
    static bool comp(string a, string b){
        if(a.size() == b.size()) return a < b;
        else return a.size() > b.size();
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值