454.四数相加II
题目:
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。
为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -2^28 到 2^28 - 1 之间,最终结果不会超过 2^31 - 1 。
例如:
输入:
- A = [ 1, 2]
- B = [-2,-1]
- C = [-1, 2]
- D = [ 0, 2]
代码:
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
int count = 0;
Map<Integer,Integer> map = new HashMap<> ();
for(int i : nums1){
for(int j : nums2){
int temp = i + j;
if(map.containsKey(temp)){
map.put(temp, map.get(temp)+1);
}else{
map.put(temp, 1);
}
}
}
for(int m : nums3){
for(int n : nums4 ){
int cur = 0-(m + n);
if(map.containsKey(cur)){
count += map.get(cur);
}
}
}
return count;
}
}
383. 赎金信
题目:
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。
(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)
注意:
你可以假设两个字符串均只含有小写字母。
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
代码:
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] record = new int[26];
Set<Integer> set = new HashSet<>();
for(int i : magazine.toCharArray()){
record[i - 'a'] += 1;
}
for(int j: ransomNote.toCharArray()){
record[j - 'a'] -= 1;
}
for(int k : record){
if(k < 0){
return false;
}
}
return true;
}
}
15. 三数之和
题目:
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。
注意: 答案中不可以包含重复的三元组。
示例:
给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ]
代码:
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> record = new ArrayList<> ();
Arrays.sort(nums);
for(int i = 0; i<nums.length; i ++){
if( nums[i] > 0){
return record ;
}
if(i > 0 && nums[i] == nums[i -1]){
continue;
}
int left = i + 1;
int right = nums.length - 1;
while (left < right){
int sums = nums[i] + nums[left] + nums[right];
if(sums > 0){
right --;
}else if(sums < 0){
left ++;
}else{
record.add(Arrays.asList(nums[i], nums[left], nums[right]));
while(left < right && nums[left] == nums[left + 1]){
left ++;
}
while(left < right && nums[right] == nums[right - 1]){
right --;
}
right --;
left ++;
}
}
}
return record;
}
}
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 List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
// nums[i] > target 直接返回, 剪枝操作
if (nums[i] > 0 && nums[i] > target) {
return result;
}
if (i > 0 && nums[i - 1] == nums[i]) {
continue;
}
for (int j = i + 1; j < nums.length; j++) {
if (j > i + 1 && nums[j - 1] == nums[j]) {
continue;
}
int left = j + 1;
int right = nums.length - 1;
while (right > left) {
long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
result.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
while (right>left && nums[right]==nums[right - 1])right--;
while (right>left && nums[left] == nums[left + 1]) left++;
left++;
right--;
}
}
}
}
return result;
}
}