leetcode(16-20)

16. 最接近的三数之和

给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.

与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).
class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        int n = nums.size();
        int ret = 100000;
        sort(nums.begin(), nums.end());
        for(int i=0; i<n-2; i++){
            for (int j=i+1; j<n-1; j++) {
                for (int k=j+1; k<n; k++) {
                    if (abs(ret-target) > abs(nums[i]+nums[j]+nums[k]-target)){
                        ret = nums[i]+nums[j]+nums[k];
                    }
                }
            }

        }
        return ret;
    }
};

17. 电话号码的字母组合

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例:

输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。

class Solution {
public:
    vector<string> letterCombinations(string& digits) {
        vector<string> res;
        int len = res.size();
        map<char,string> dic;
        string temp="";
        dic.insert(pair<char,string>('2',"abc"));
        dic.insert(pair<char,string>('3',"def"));
        dic.insert(pair<char,string>('4',"ghi"));
        dic.insert(pair<char,string>('5',"jkl"));
        dic.insert(pair<char,string>('6',"mno"));
        dic.insert(pair<char,string>('7',"pqrs"));
        dic.insert(pair<char,string>('8',"tuv"));
        dic.insert(pair<char,string>('9',"wxyz"));
        process(res,dic,digits,temp);
        return res;
    }
    void process(vector<string> &res, map<char,string> &dic, string digits, string temp) {
        if (digits.size() == 0) {
            if(temp != "")
                res.push_back(temp);
            return;
        }
        int len = dic[digits[0]].size();
        for (int i=0; i<len; i++) {
            process(res,dic,digits.substr(1),temp+dic[digits[0]][i]);
        }
    }
};

18. 四数之和

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:

答案中不可以包含重复的四元组。

示例:

给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]
class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        set<vector<int>> res;
        sort(nums.begin(), nums.end());
        for (int i = 0; i < int(nums.size() - 3); ++i)
        {
            for (int j = i + 1; j < int(nums.size() - 2); ++j) 
            {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;
                int left = j + 1, right = nums.size() - 1;
                while (left < right) 
                {
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum == target) 
                    {
                        vector<int> out{nums[i], nums[j], nums[left], nums[right]};
                        res.insert(out);
                        ++left; 
                        --right;
                    } else if (sum < target) ++left;
                    else --right;
                }
            }
        }
        return vector<vector<int>>(res.begin(), res.end());
 
    }
};

19. 删除链表的倒数第N个节点

给定一个链表,删除链表的倒数第 个节点,并且返回链表的头结点。

示例:

给定一个链表: 1->2->3->4->5, 和 n = 2.

当删除了倒数第二个节点后,链表变为 1->2->3->5.

说明:

给定的 n 保证是有效的。

进阶:

你能尝试使用一趟扫描实现吗?

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if (n==0) return head;
        ListNode* delLoc=head;
        ListNode* delTail=head;
        for (int i=0;i<n;i++){
            delTail = delTail->next;
        }
        if (delTail == NULL) {
            head = delLoc->next;
            delete delLoc;
            return head;
        }
        while (delTail->next != NULL) {
            delTail = delTail->next;
            delLoc = delLoc->next;
        }
        delLoc->next=delLoc->next->next;
        return head;
    }
}; 

给定一个只包括 '('')''{''}''['']' 的字符串,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

示例 1:

输入: "()"
输出: true

示例 2:

输入: "()[]{}"
输出: true

示例 3:

输入: "(]"
输出: false

示例 4:

输入: "([)]"
输出: false

示例 5:

输入: "{[]}"
输出: true
class Solution {
public:
    bool isValid(string s) {
        stack<char> result;  
        int n=s.size();  
        if(n==0) return true;  
     for(int i=0;i<n;i++)  
            {  
                if(result.empty())  
                    result.push(s[i]);  
                else if(result.top()=='('&&s[i]==')'||  
                      result.top()=='['&&s[i]==']'||  
                      result.top()=='{'&&s[i]=='}')   
                        result.pop();  
                else  
                    result.push(s[i]);  
                  
            }  
            return result.empty();  
    }
};

(以上题目均摘自leetcode)

猜你喜欢

转载自blog.csdn.net/github_37002236/article/details/82290392