Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:

  • 0 <= a, b, c, d < n
  • a, b, c, and d are distinct.
  • nums[a] + nums[b] + nums[c] + nums[d] == target

You may return the answer in any order.

 

Example 1:

Input: nums = [1,0,-1,0,-2,2], target = 0 Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

Example 2:

Input: nums = [2,2,2,2,2], target = 8 Output: [[2,2,2,2]]

 

Constraints:

  • 1 <= nums.length <= 200
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, long target) {
        vector<vector<int>> ans;
        sort(nums.begin(), nums.end());
        long left, right, sum;
        if (nums.size() >= 4)
        {
            for (int i = 0; i < nums.size() - 3; i++)
            {
                if (i > 0 && nums[i] == nums[i - 1]) continue;

                for (int j = i + 1; j < nums.size() - 2; j++)
                {
                    if (j > i + 1 && nums[j] == nums[j - 1]) continue;

                    left = j + 1;
                    right = nums.size() - 1;

                    while (left < right)
                    {
                        sum = (long)nums[i] + (long)nums[j] + (long)nums[left] + (long)nums[right];

                        if (sum < target) left++;
                        else if (sum > target) right--;
                        else
                        {
                            ans.push_back({ nums[i], nums[j], nums[left], nums[right] });

                            while (left < right && nums[left] == ans[ans.size() - 1][2]) left++;
                            while (left < right && nums[right] == ans[ans.size() - 1][3]) right--;
                        }
                    }
                }
            }
        }
        return ans;
    }
};

'Challenge' 카테고리의 다른 글

[Codility] GenomicRangeQuery  (0) 2021.10.09
[LeetCode] Remove Nth Node From End of List  (0) 2021.10.07
[LeetCode] Letter Combinations of a Phone Number  (0) 2021.10.06
[LeetCode] 3Sum Closest  (0) 2021.10.06
[LeetCode] 3Sum  (0) 2021.10.06

 

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

 

Example 1:

Input: digits = "23" Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]

Example 2:

Input: digits = "" Output: []

Example 3:

Input: digits = "2" Output: ["a","b","c"]

 

Constraints:

  • 0 <= digits.length <= 4
  • digits[i] is a digit in the range ['2', '9'].
class Solution {
public:
    map<char, vector<char>> al;
    string addDigits(vector<string>& ans, string s, string digits, int num)
    {
        string str = "";
        if (num < digits.length())
        {
            for (int i = 0; i < al[digits[num]].size(); i++)
            {
                str = s + al[digits[num]][i];
                addDigits(ans, str, digits, num + 1);
                if (num == digits.length() - 1)
                {
                    ans.push_back(str);
                }
            }
        }
        else if (num == 1)
        {
            ans.push_back(s);
        }
        return str;
    }
    vector<string> letterCombinations(string digits) {
        vector<string> ans;
        int start = 97;
        for (int i = 0; i < 8; i++)
        {
            if (i + 2 == 7)
            {
                al['7'].push_back('p');
                start++;
            }
            if (i + 2 == 9)
            {
                al['9'].push_back('w');
                start++;
            }
            for (int j = 0; j < 3; j++)
            {
                al[to_string(i + 2)[0]].push_back((char)(start+(3*i)+j));
            }
        }
        for (int i = 0; i < al[digits[0]].size(); i++)
        {
            string s(1, al[digits[0]][i]);
            addDigits(ans, s, digits, 1);
        }
        return ans;
    }
};

'Challenge' 카테고리의 다른 글

[LeetCode] Remove Nth Node From End of List  (0) 2021.10.07
[LeetCode] 4Sum  (0) 2021.10.07
[LeetCode] 3Sum Closest  (0) 2021.10.06
[LeetCode] 3Sum  (0) 2021.10.06
[Programmers] 이분탐색 - 징검다리  (0) 2021.10.03

Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.

Return the sum of the three integers.

You may assume that each input would have exactly one solution.

 

Example 1:

Input: nums = [-1,2,1,-4], target = 1 Output: 2 Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

Example 2:

Input: nums = [0,0,0], target = 1 Output: 0

 

Constraints:

  • 3 <= nums.length <= 1000
  • -1000 <= nums[i] <= 1000
  • -104 <= target <= 104
class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        sort(nums.begin(), nums.end());
        int ans = nums[0] + nums[1] + nums[2];
        int left, right, sum;
        for (int i = 0; i < nums.size() - 2; i++)
        {
            left = i + 1;
            right = nums.size() - 1;
            while (left < right)
            {
                sum = nums[i] + nums[left] + nums[right];
                if (abs(sum - target) < abs(ans - target)) ans = sum;
                
                if (sum > target) right--;
                else if (sum < target) left++;
                else break;
            }
        }
        return ans;
    }
};

'Challenge' 카테고리의 다른 글

[LeetCode] 4Sum  (0) 2021.10.07
[LeetCode] Letter Combinations of a Phone Number  (0) 2021.10.06
[LeetCode] 3Sum  (0) 2021.10.06
[Programmers] 이분탐색 - 징검다리  (0) 2021.10.03
[Programmers] 이분탐색 - 입국심사  (0) 2021.10.03

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.

 

Example 1:

Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]]

Example 2:

Input: nums = [] Output: []

Example 3:

Input: nums = [0] Output: []

 

Constraints:

  • 0 <= nums.length <= 3000
  • -105 <= nums[i] <= 105
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> ans;
        sort(nums.begin(), nums.end());
        int target, left, right, sum;
        for (int i = 0; i < nums.size(); i++)
        {
            if (i > 0 && nums[i - 1] == nums[i]) continue;
            
            target = -nums[i];
            left = i + 1;
            right = nums.size() - 1;
            while (left < right)
            {
                sum = nums[left] + nums[right];
                if (sum < target) left++;
                else if (sum > target) right--;
                else
                {
                    ans.push_back({ nums[i], nums[left], nums[right] });
                    while (left < right && nums[left] == ans[ans.size() - 1][1]) left++;
                    while (left < right && nums[right] == ans[ans.size() - 1][2]) right--;
                }
            }
        }
        return ans;
    }
};

'Challenge' 카테고리의 다른 글

[LeetCode] Letter Combinations of a Phone Number  (0) 2021.10.06
[LeetCode] 3Sum Closest  (0) 2021.10.06
[Programmers] 이분탐색 - 징검다리  (0) 2021.10.03
[Programmers] 이분탐색 - 입국심사  (0) 2021.10.03
[Codility] MinAvgTwoSlice  (0) 2021.10.03

+ Recent posts