当前位置:网站首页>Hash table - sum of arrays
Hash table - sum of arrays
2022-06-27 21:59:00 【Invincible Dragon Warrior】
- Sum of two numbers
Given an array of integers nums And an integer target value target, Please find... In the array And is the target value target the Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
You can return the answers in any order .
Example 1:
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
Example 2:
Input :nums = [3,2,4], target = 6
Output :[1,2]
Example 3:
Input :nums = [3,3], target = 6
Output :[0,1]
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
for(int i = 0; i < nums.size(); ++i){
if(map.find(target - nums[i]) != map.end()){
return vector<int>{i, map[target - nums[i]]};
} else {
map[nums[i]] = i;
}
}
return vector<int>{};
}
};a
- Add four numbers II
Here are four integer arrays nums1、nums2、nums3 and nums4 , The length of the array is n , Please calculate how many tuples there are (i, j, k, l) To meet the :
0 <= i, j, k, l < n
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
Example 1:
Input :nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
Output :2
explain :
Two tuples are as follows :
- (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
- (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
Example 2:
Input :nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
Output :1
class Solution {
public:
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
unordered_map<int, int> map;
for(auto a : nums1){
for(auto b : nums2){
map[a + b]++;
}
}
int cnt = 0;
for(auto c : nums3){
for(auto d : nums4){
if(map.find(0 - (c + d)) != map.end()){
cnt += map[0 - (c + d)];
}
}
}
return cnt;
}
};
- Ransom letter
Here are two strings :ransomNote and magazine , Judge ransomNote Can it be done by magazine The characters inside make up .
If possible , return true ; Otherwise return to false .
magazine Each character in can only be in ransomNote Used once in .
Example 1:
Input :ransomNote = “a”, magazine = “b”
Output :false
Example 2:
Input :ransomNote = “aa”, magazine = “ab”
Output :false
Example 3:
Input :ransomNote = “aa”, magazine = “aab”
Output :true
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
unordered_map<int, int> mag;
for(int i = 0; i < magazine.size(); ++i){
mag[magazine[i]]++;
}
for(int i = 0; i < ransomNote.size(); ++i){
if(mag.find(ransomNote[i]) != mag.end()){
mag[ransomNote[i]]--;
} else {
return false;
}
if(mag[ransomNote[i]] == 0){
mag.erase(ransomNote[i]);
}
}
return true;
}
};
- Sum of three numbers
Give you one containing n Array of integers nums, Judge nums Are there three elements in a,b,c , bring a + b + c = 0 ? Please find out all and for 0 Triple without repetition .
Be careful : Answer cannot contain duplicate triples .
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 :[]
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
int len = nums.size();
if(len < 3){
return {};
}
vector<vector<int>> res;
for(int i = 0; i < len; ++i){
if(i > 0 && nums[i] == nums[i - 1]){
continue;
}
int left = i + 1, right = len - 1;
while(left < right){
int sum = nums[i] + nums[left] + nums[right];
if(sum > 0){
--right;
} else if(sum < 0){
++left;
} else {
res.push_back(vector<int>{nums[i], nums[left], nums[right]});
while(left < right && nums[left] == nums[left + 1]){
++left;
}
while(left < right && nums[right] == nums[right - 1]){
--right;
}
++left;
--right;
}
}
}
return res;
}
};
- Sum of four numbers
Here you are n An array of integers nums , And a target value target . Please find and return the quads that meet all the following conditions and are not repeated [nums[a], nums[b], nums[c], nums[d]] ( If two Quad elements correspond to each other , It is considered that two quads repeat ):
0 <= a, b, c, d < n
a、b、c and d Different from each other
nums[a] + nums[b] + nums[c] + nums[d] == target
You can press In any order Return to the answer .
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]]
using LL = long long;
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int len = nums.size();
vector<vector<int>> res;
for(int i = 0; i < len; ++i){
if(i > 0 && nums[i] == nums[i - 1]){
continue;
}
for(int j = i + 1; j < len; ++j){
if(j > i + 1 && nums[j] == nums[j - 1]){
continue;
}
int left = j + 1, right = len - 1;
while(left < right){
LL sum = 0LL + nums[i] + nums[j] + nums[left] + nums[right];
if(sum > target){
--right;
} else if(sum < target){
++left;
} else {
res.push_back({nums[i], nums[j], nums[left], nums[right]});
while(left < right && nums[left] == nums[left + 1]){
++left;
}
while(left < right && nums[right] == nums[right - 1]){
--right;
}
++left;
--right;
}
}
}
}
return res;
}
};
边栏推荐
- ∫(0→1) ln(1+x) / (x² + 1) dx
- [MySQL] database function clearance Tutorial Part 2 (window function topic)
- Read write separation master-slave replication of MySQL
- Bean paste green protects your eyes
- [LeetCode]508. The most frequent subtree elements and
- What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
- [LeetCode]513. 找树左下角的值
- win11桌面出現“了解此圖片”如何删除
- Example of using gbase 8A OLAP function group by grouping sets
- Go from introduction to practice - polymorphism (note)
猜你喜欢

美团20k软件测试工程师的经验分享

Go from introduction to actual combat -- channel closing and broadcasting (notes)

Go from introduction to actual combat - package (notes)

畅游动态规划之区间DP

vmware虚拟机PE启动

我想我要开始写我自己的博客了。

∫(0→1) ln(1+x) / (x ² + 1) dx

GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析

Figure countdownlatch and cyclicbarrier based on AQS queue

C语言程序设计详细版 (学习笔记1) 看完不懂,我也没办法。
随机推荐
【Redis】零基础十分钟学会Redis
"Apprendre cette image" apparaît sur le Bureau win11 comment supprimer
美团20k软件测试工程师的经验分享
GBase 8a数据库用户密码安全相关参数汇总
语言弱点列表--CWE,一个值得学习的网站
GBase 8a OLAP分析函数 cume_dist的使用样例
Simulink导出FMU模型文件方法
Little known MySQL import data
神奇的POI读取excel模板文件报错
Array assignment
[LeetCode]100. Same tree
Experience sharing of meituan 20K Software Test Engineers
读写分离-Mysql的主从复制
[leetcode] dynamic programming solution split integer i[silver fox]
matlab查找某一行或者某一列在矩阵中的位置
Summary of gbase 8A database user password security related parameters
Go从入门到实战——CSP并发机制(笔记)
A method of go accessing gbase 8A database
GBase 8a OLAP函数group by grouping sets的使用样例
Software defect management - a must for testers