LeetCode 第454题.四数相加II
📖 考察点
哈希表
📖 题意理解
给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:
0 <= i, j, k, l < nnums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
💡 解题思路
思路一:循环
思路二: 两两循环,用哈希表找值
🔑 关键点总结
两个for循环,用map存一下两个值的和或者差作为键,值为和为这个键值的数量
💻 代码实现
JavaScript
var fourSumCount = function(nums1, nums2, nums3, nums4) {
let map = new Map();
let n = nums1.length;
let res = 0;
for(let i = 0;i<n;i++){
for(let j = 0;j<n;j++){
const sum = nums1[i]+nums2[j];
if(map.has(sum)){
let arr = map.get(sum);
arr.push([i,j]);
}else{
map.set(sum,[[i,j]]);
}
}
}
for(let a = 0;a<n;a++){
for(let b =0;b<n;b++){
const difference = -(nums3[a]+nums4[b]);
if(map.has(difference)){
let arr = map.get(difference);
res+=arr.length;
}
}
}
return res;
};
Rust
pub fn four_sum_count(
nums1: Vec<i32>,
nums2: Vec<i32>,
nums3: Vec<i32>,
nums4: Vec<i32>,
) -> i32 {
let mut map = HashMap::new();
let mut res = 0;
for &a in &nums1 {
for &b in &nums2 {
*map.entry(a + b).or_insert(0) += 1;
}
}
for &c in &nums3 {
for &d in &nums4 {
let target = -(c + d);
if let Some(&count) = map.get(&target) {
res += count;
}
}
}
res as i32
}
LeetCode 83. 赎金信
📖 考察点
哈希表
📖 题意理解
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次
💡 解题思路
思路:
便历mangazine使用数组存一下的字母的数量, 便历ransomNote,如果字母的数量小于0,返回false 否则返回true
🔑 关键点总结
💻 代码实现
JavaScript
var canConstruct = function (ransomNote, magazine) {
let set = new Array(26).fill(0);
magazine.split("").forEach((char) => {
set[char.charCodeAt(0) - 97]++;
});
const charList = ransomNote.split("");
for (let i = 0; i < charList.length; i++) {
set[charList[i].charCodeAt(0) - 97]--;
if (set[charList[i].charCodeAt(0) - 97] < 0) {
return false;
}
}
return true;
};
Rust
pub fn can_construct(ransom_note: String, magazine: String) -> bool {
let mut set = [0; 26];
magazine
.chars()
.for_each(|ch| set[ch as usize - 'a' as usize] += 1);
for ch in ransom_note.chars() {
set[ch as usize - 'a' as usize] -= 1;
if set[ch as usize - 'a' as usize] < 0 {
return false;
}
}
true
}
⏱️ 复杂度分析
📚 总结与反思
LeetCode 15.三数之和
📖 考察点
双指针
📖 题意理解
给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请你返回所有和为 0 且不重复的三元组。
注意: 答案中不可以包含重复的三元组。
💡 解题思路
便历数组,取左值和右值,如果和小于0左值右移,大于0右值左移,注意去重
思路一:
思路二:
🔑 关键点总结
💻 代码实现
JavaScript
var threeSum = function (nums) {
const temp = [...nums].sort((a, b) => a - b);
let res = [];
for (let i = 0; i < temp.length; i++) {
if (temp[i] > 0) {
return res;
}
if (i > 0 && temp[i] == temp[i - 1]) {
continue;
}
let left = i + 1;
let right = temp.length - 1;
while (left < right) {
const sum = temp[i] + temp[left] + temp[right];
if (sum > 0) {
right--;
} else if (sum < 0) {
left++;
} else {
res.push([temp[i], temp[left], temp[right]]);
while (right > left && temp[right] == temp[right - 1]) right--;
while (right > left && temp[left] == temp[left + 1]) left++;
right--;
left++;
}
}
}
return res;
};
Rust
pub fn three_sum(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut temp = nums.clone();
temp.sort();
let mut res = Vec::new();
let n = temp.len();
for i in 0..n {
if temp[i] > 0 {
break;
}
if i > 0 && temp[i] == temp[i - 1] {
continue;
}
let mut left = i + 1;
let mut right = n - 1;
while left < right {
let sum = temp[i] + temp[left] + temp[right];
if sum < 0 {
left += 1
} else if sum > 0 {
right -= 1;
} else {
res.push(vec![temp[i], temp[left], temp[right]]);
while left < right && temp[left] == temp[left + 1] {
left += 1;
}
while left < right && temp[right] == temp[right - 1] {
right -= 1
}
left += 1;
right -= 1
}
}
}
res
}
⏱️ 复杂度分析
📚 总结与反思
LeetCode 18题. 四数之和
📖 考察点
📖 题意理解
给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):
0 <= a, b, c, d < na、b、c和d互不相同nums[a] + nums[b] + nums[c] + nums[d] == target
你可以按 任意顺序 返回答案 。
💡 解题思路
思路一:
思路二:
🔑 关键点总结
💻 代码实现
JavaScript
var fourSum = function(nums, target) {
const len = nums.length;
if(len < 4) return [];
nums.sort((a, b) => a - b);
const res = [];
for(let i = 0; i < len - 3; i++) {
// 去重i
if(i > 0 && nums[i] === nums[i - 1]) continue;
for(let j = i + 1; j < len - 2; j++) {
// 去重j
if(j > i + 1 && nums[j] === nums[j - 1]) continue;
let l = j + 1, r = len - 1;
while(l < r) {
const sum = nums[i] + nums[j] + nums[l] + nums[r];
if(sum < target) { l++; continue}
if(sum > target) { r--; continue}
res.push([nums[i], nums[j], nums[l], nums[r]]);
// 对nums[left]和nums[right]去重
while(l < r && nums[l] === nums[++l]);
while(l < r && nums[r] === nums[--r]);
}
}
}
return res;
};
Rust
pub fn four_sum(nums: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
if (nums.len() < 4) {
return vec![];
}
let mut nums = nums.clone();
nums.sort();
let mut res = Vec::new();
let n = nums.len();
for i in 0..n - 3 {
if i > 0 && nums[i] == nums[i - 1] {
continue;
}
for j in i + 1..n - 2 {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
let mut l = j + 1;
let mut r = n - 1;
while l < r {
let sum = nums[i] as i64 + nums[j] as i64 + nums[l] as i64 + nums[r] as i64;
let target = target as i64;
if sum > target {
r -= 1;
} else if sum < target {
l += 1
} else {
res.push(vec![nums[i], nums[j], nums[l], nums[r]]);
while l < r && nums[r] == nums[r - 1] {
r -= 1;
}
while l < r && nums[l] == nums[l + 1] {
l += 1;
}
r -= 1;
l += 1;
}
}
}
}
res
}
⏱️ 复杂度分析
📚 总结与反思
rust 要注意整数位数是否满足条件