2sum:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

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

解答:

采用hashmap,从头遍历数组,如果hashmap不含有该元素,则将target减去这个元素的值和该元素的下标存入hashmap;如果hashmap含有该元素,则将hashmap中对应的下标和该元素的下标存入数组返回结果。

public class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> helper = new HashMap<Integer, Integer>();
int[] result = {-1, -1};
for (int i = 0; i < nums.length; i++) {
if (helper.get(nums[i]) != null) {
result[0] = helper.get(nums[i]);
result[1] = i;
return result;
} else {
helper.put(target - nums[i], i);
}
}
return result;
}
}

 此题若不是返回下标,还可以采用将数组先进行排序,然后利用左右两个指针寻找相应元素。若左右两指针元素和小于target,则左指针右移,若大于则右指针左移,若相等则返回结果。

    public int[] twoSum_pointer(int[] numbers, int target) {
if (numbers == null || numbers.length < 2) {
return null;
}
Arrays.sort(numbers);
int left = 0;
int right = numbers.length - 1;
int[] rst = new int[2]; while ( left < right) {
int sum = numbers[left] + numbers[right];
if( sum == target){
rst[0] = left;
rst[1] = right;
break;
} else if ( sum < target) {
left++;
} else {
right--;
}
}
return rst;
}

3sum:

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.

解答:

运用上题2sum的第二种方法,相当于把数组的每个元素当做target,然后往后寻找2sum为target相反数的两个元素。要注意的是避免重复结果。

public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new LinkedList<List<Integer>>();
if (nums == null || nums.length < 3) {
return result;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
} //避免重复元素
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
if (nums[i] + nums[left] + nums[right] == 0) {
List<Integer> temp = new LinkedList<Integer>();
temp.add(nums[i]);
temp.add(nums[left]);
temp.add(nums[right]);
result.add(temp);
left++;
right--;
while(left < right && nums[left] == nums[left - 1]) {
left++;
}
while(left < right && nums[right] == nums[right + 1]) {
right--;
} //避免重复元素
} else if (nums[i] + nums[left] + nums[right] > 0) {
right--;
} else {
left++;
}
}
}
return result;
}
}

3sum closest:

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

解答:同样是2sum的扩展,先将数组排序,然后遍历数组,对每个元素向后寻找两个元素并求和,和等于target直接返回target,大于则左指针右移,小于则右指针左移,同时更新result值。注意,result初始值设为Integer.MAX_VALUE的一半,而不是Integer.MAX_VALUE,否则结果可能溢出而产生错误。比如:

正确代码如下:

public class Solution {
public int threeSumClosest(int[] nums, int target) {
if (nums == null || nums.length < 3) {
return -1;
}
Arrays.sort(nums);
int result = Integer.MAX_VALUE / 2;
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int temp = nums[i] + nums[left] + nums[right];
if (temp == target) {
return temp;
} else if (temp < target) {
left++;
} else {
right--;
}
if (Math.abs(temp - target) < Math.abs(result - target)) {
result = temp;
}
}
}
return result;
}
}

4sum:

Given an array S of n integers, are there elements abc, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
  • The solution set must not contain duplicate quadruplets.

解答:

先将数组进行排序,然后两个for循环嵌套,固定两个数组元素,再用左右两指针扫另外两个元素,根据sum与target大小关系确定指针的移动。

public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new LinkedList<List<Integer>>();
if (nums == null || nums.length < 4) {
return result;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
int left = j + 1;
int right = nums.length - 1;
while (left < right) {
int temp = nums[i] + nums[j] + nums[left] + nums[right];
if (temp == target) {
List<Integer> ele = new LinkedList<Integer>();
ele.add(nums[i]);
ele.add(nums[j]);
ele.add(nums[left]);
ele.add(nums[right]);
result.add(ele);
left++;
right--;
while (left < right && nums[left] == nums[left - 1]) {
left++;
}
while (left < right && nums[right] == nums[right + 1]) {
right--;
}
} else if (temp < target) {
left++;
} else {
right--;
}
}
}
}
return result;
}
}

值得注意的是,本题中的数据结构采用的是LinkedList而不是ArrayList,原因是插入操作比较多,用LinkedList效率更高。前者运行时间击败63%,后者54%。

ksum问题的更多相关文章

  1. 2016 一中培训 day 5 ksum

    又是一天的爆零!!!!! 原本第一题 很容易做 竟然优化过度 丢了答案 1693: ksum Time Limit 1000 ms Memory Limit 524288 KBytes Judge S ...

  2. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列

    1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...

  3. kSum问题总结

    1.2Sum 题目: 方法一:两次迭代 public class TwoSum { public static int[] twoSum(int[] nums, int target) { int[] ...

  4. 2sum,3sum,4sum,ksum

    1. 2sum 题目:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你不能重复利 ...

  5. 【数组】kSum问题

    一.2Sum 思路1: 首先对数组排序.不过由于最后返回两个数字的索引,所以需要事先对数据进行备份.然后采用2个指针l和r,分别从左端和右端向中间运动:当l和r位置的两个数字之和小于目标数字targe ...

  6. k-sum 问题

    问题描述 给定一个数组及数字 k ,从数组中找出所有相加结果为 k 的组合. 示例: 给定数组 [1,1,1] 令 k=2,输出: [[1,1]] 给定数组 [10, 1, 2, 7, 6, 1, 5 ...

  7. [算法]K-SUM problem

    一.Two Sum Given an array of integers, find two numbers such that they add up to a specific target nu ...

  8. 【JZOJ4815】【NOIP2016提高A组五校联考4】ksum

    题目描述 输入 输出 样例输入 3 4 1 3 4 样例输出 8 7 4 4 数据范围 样例解释 解法 二分做法 考虑到可以二分第k大的值mid,如果比mid大的区间和数小于或等于mid,那么mid就 ...

  9. k-sum问题

    给定一个数组,里面的是任意整数,可能有重复,再给定一个目标T,从数组中找出所有和为T的K个数,要求结果中没有重复. Note: Elements in a quadruplet (a,b,c,d) m ...

随机推荐

  1. 纪中集训 Day 2

    今天(其实是昨天= =)早上起来发现好冷好冷啊= = 吃完饭就准备比赛了,好吧B组难度的题总有一道不知到怎么写QAQ 太弱了啊!!! 蒟蒻没人权啊QAQ 今天第4题不会写,在这里说说吧 题目的意思就是 ...

  2. nodemailer中的几个坑

    nodemailer是什么 nodemailer是一个nodejs的邮件服务模块 如何用nodemailer发邮件 1.先安装nodemailer npm i --save nodemailer 2. ...

  3. POJ 2396 Budget 有上下界的网络流

    POJ 2396  Budget 题意简述:给定矩阵(每个元素都是非负整数)各行各列的和,并且限制其中的某些元素,给出一个可行解,特殊评测.矩阵规模小于200*20. 网络流的模型是显而易见的,不过对 ...

  4. 整理 - .Net系统预定义的委托们

    大部分情况下,我们可以使用系统预定义的委托类型,而不需要自己再来手动定义. 以下委托都位于System命名空间下. 传入参数.返回值的类型,大都被声明为泛型. 1.Action系列 有0-16个参数, ...

  5. Python入门教程(2)

    人生苦短,我玩蛇0.0! Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991 ...

  6. 极光推送助推视频App,打造最活跃手机新媒体平台

    移动应用能够帮助吸引更多的新用户,增加用户互动和对话.但你得让用户想起你,如何在一部手机上数十个App中脱颖而出,是考验App运营的关键之处.为了打造一个成功的App,开发者需要着眼长远,不应局限于其 ...

  7. Ajax页面的加载数据与删除

    1.数据库找一张表: 颜色表2.主页面主页面的代码用到tbody:TBODY作用是:可以控制表格分行下载,从而提高下载速度.(网页的打开是先表格的的内容全部下载完毕后,才显示出来,分行下载可以先显示部 ...

  8. 从数据库读取二进制图片,img标签显示图片

    引自 http://www.w3dev.cn/article/20110214/asp-net-csharp-image-base64-change.aspx      <img src=&qu ...

  9. 算法模板——线段树6(二维线段树:区域加法+区域求和)(求助phile)

    实现功能——对于一个N×M的方格,1:输入一个区域,将此区域全部值作加法:2:输入一个区域,求此区域全部值的和 其实和一维线段树同理,只是不知道为什么速度比想象的慢那么多,求解释...@acphile ...

  10. IIS HTTP 错误 500.19 - Internal Server Error HTTP 错误 401.3 - Unauthorized 解决办法

    前言:IIS是一个强大的服务器管理器,当遇到 IIS HTTP 错误 500.19 - Internal Server Error  HTTP 错误 401.3 - Unauthorized 的解决办 ...