1、2Sum

题目:

方法一:两次迭代

public class TwoSum {

    public static int[] twoSum(int[] nums, int target) {
int[] indices = {-1,-1};
for(int i=0; i<nums.length-1; i++ ){
if(target>=nums[i]){
for(int k=i+1; k<=nums.length-1; k++){
if(nums[k] == (target-nums[i])){
indices[0]=i;
indices[1]=k;
return indices;
}
} }
}
return indices; }
}

方法二:利用HashMap,减少一次迭代

import java.util.HashMap;

public class Two_Sum1 {
public static int[] twosum(int[] nums, int target){
int[] result = new int[2];
if(nums.length < 2) return result; HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i=0; i<nums.length; i++){
if(!map.containsKey(target-nums[i])){
map.put(nums[i],i);
}else{
result[0]=map.get(target-nums[i]);
result[1]=i;
break;
}
}
return result;
}
}

2、3Sum

思路分析:数组排序 + twoPointers

public class _3Sum {
public static List<List<Integer>> threeSum(int[] num){
Arrays.sort(num);
List<List<Integer>> res = new LinkedList<>();
for(int i=0; i<num.length-2; i++){
if(i==0 || (i>0 && num[i]!= num[i-1])){
int lo=i+1, hi=num.length-1, sum=0-num[i];
while(lo<hi){
if(num[lo]+num[hi]==sum){
res.add(Arrays.asList(num[i],num[lo],num[hi]));
while(lo<hi && num[lo] == num[lo+1]) lo++;
while(lo<hi && num[hi] == num[hi-1]) hi--;
lo++; hi--;
}else if(num[lo]+num[hi]<sum) lo++;
else hi--;
}
}
}
return res;
}
}

3、3Sum Cloest

思路分析:数组排序 + twoPointers

public class _3SumClosest {
public static int threeSumClosest(int[] nums, int target){
Arrays.sort(nums);
int diff = Integer.MAX_VALUE, closest=0;
for(int i=0;i<nums.length-2;i++){
int lo=i+1, hi=nums.length-1;
while(lo<hi){
int sum = nums[i]+nums[lo]+nums[hi];
if(sum == target) return target;
else if(sum > target){
if(sum-target<diff){
diff = sum - target;
closest = sum;
}
hi--;
}else{
if(target-sum<diff){
diff = target - sum;
closest = sum;
}
lo++;
}
}
}
return closest;
}
}

4、4Sum

思路分析:数组排序+转化问题为3Sum + 2Sum

public class FourSum {
public static List<List<Integer>> fourSum(int[] nums, int target){ LinkedList<List<Integer>> res = new LinkedList<List<Integer>>();
if(nums==null || nums.length<4) return res; Arrays.sort(nums);
int len = nums.length;
int max = nums[len-1];
if(4*nums[0]>target || 4*max<target) return res; for(int i=0; i<len-3; i++){
int z=nums[i];
if(i>0 && z==nums[i-1]) continue;
if(z+3*max<target) continue;
if(4*z>target) break;
if(4*z==target) {
if(i+3<len && nums[i+3]==z) res.add(Arrays.asList(z,z,z,z));
break;
}
threeSum(nums,target-z,i+1,len-1,res,z);
}
return res;
} public static void threeSum(int[] nums, int target, int lo, int hi, LinkedList<List<Integer>> fourSumList, int z1){ if(lo+1>=hi) return; int max = nums[hi];
if(3*nums[lo]>target || 3*max<target) return; for(int i=lo; i<hi-1; i++){
int z=nums[i];
if(i>lo && z==nums[i-1]) continue;
if(z+2*max<target) continue;
if(3*z>target) break;
if(3*z == target){
if(i+1<hi && nums[i+2]==z) fourSumList.add(Arrays.asList(z1,z,z,z));
break;
}
twoSum(nums,target-z,i+1,hi,fourSumList,z1,z);
}
} public static void twoSum(int[] nums, int target, int lo, int hi, LinkedList<List<Integer>> fourSumList, int z1, int z2){
if(lo>=hi) return;
if(2*nums[lo]>target || 2*nums[hi]<target) return; while(lo<hi){
int sum = nums[lo]+nums[hi];
if(sum == target){
fourSumList.add(Arrays.asList(z1,z2,nums[lo],nums[hi])); while(lo<hi && nums[lo]==nums[lo+1]) lo++;
while(lo<hi && nums[hi]==nums[hi-1]) hi--;
lo++;hi--;
}
if(sum<target) lo++;
if(sum>target) hi--;
}
return;
}
}

5、kSum

public class KSum {
public ArrayList<List<Integer>> kSum(int[] nums, int target, int k, int index){
ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();
if(nums==null || nums.length<k) return res;
Arrays.sort(nums);
int len = nums.length;
int max = nums[len-1];
if(k*nums[0]>target || k*max<target) {
return res;
}
if(index >= len) {
return res;
}
if(k==2){
int i=index, j=len-1;
while(i<j){
if(nums[i]+nums[j] == target){
res.add(Arrays.asList(nums[i],nums[j]));
while(i<j && nums[i]==nums[i+1]) i++;
while(i<j && nums[j]==nums[j-1]) j--;
i++;j--;
} else if(nums[i]+nums[j]<target) i++;
else j--;
} //while循环结束
}else{
for (int i = index; i < len - k + 1; i++) {
ArrayList<List<Integer>> temp = kSum(nums,target-nums[i],k-1,i+1);
if(temp!=null && temp.size()!=0){
for(List<Integer> t : temp){
t.add(0,nums[i]);
}
res.addAll(temp);
}
while(i<len-1 && nums[i] == nums[i+1]){
i++;
}
}//for循环结束
}
return res;
}
}

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. 2sum,3sum,4sum,ksum

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

  4. 【数组】kSum问题

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

  5. k-sum 问题

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

  6. [算法]K-SUM problem

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

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

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

  8. k-sum问题

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

  9. ksum问题

    2sum: Given an array of integers, return indices of the two numbers such that they add up to a speci ...

随机推荐

  1. mpvue前期准备

    一.配置环境: 1.下载node.js,去官网上下载相应的版本.http://nodejs.cn 2.安装就是下一步下一步,检查是否安装成功,打开cmd.输入  node -v 会出现版本号. 3.推 ...

  2. git 免密码push

    git版本2.14.1 windows系统 用户根目录 .gitconfig 文件添加配置 [credential] helper = store[push] default = simple 用户根 ...

  3. A Boring Problem UVALive - 7676

    16年北京现场赛的题,全场过的队30+. 初看只知道 O(N^2logK)的暴力,以为是什么变换. 仔细发现活用 二项式定理 就行. #include <bits/stdc++.h> us ...

  4. python—列表生成式

    #原始写法 l=[] for i in range(1,11): l.append(str(i).zfill(2)) print(l) #结果:['01', '02', '03', '04', '05 ...

  5. rancher的微服务运维

    1.安装rancher: rancher官网:https://rancher.com rancher中文官网:https://www.cnrancher.com rancher 2.0 文档:http ...

  6. SPA

    为什么用SPA 1. 减少服务器压力  如果不用spa  那么每次切换页面的时候,就会向服务器发送一个请求 服务器返回一个html文件   如果使用了SPA  在切换时,不需要请求服务器,只要通过本地 ...

  7. 20175234 2018-2019-2 《Java程序设计》第八周学习总结

    目录 20175234 2018-2019-2 <Java程序设计>第八周学习总结 教材学习内容总结 15.1泛型 15.2链表 15.3堆栈 15.4散列映射 15.5树集 15.6树映 ...

  8. Linux 学习笔记 1

    1.  名词解释 GNU: 目标是创建一套完全自由的操作系统:包含了可自由使用的软件,如Emacs,GCC,Tex,X Window:制定了3个自由软件协议:GPL,LGPL,GFDL GPL(Gen ...

  9. C运算符

    运算符是一种告诉编译器执行特定的数学或逻辑操作的符号. C 语言内置了丰富的运算符,并提供了以下类型的运算符: 算术运算符.关系运算符.逻辑运算符.位运算符.赋值运算符.杂项运算符 C 中的运算符优先 ...

  10. 详解键盘事件(keydown,keypress,keyup)

    一.键盘事件基础 1.定义 keydown:按下键盘键 keypress:紧接着keydown事件触发(只有按下字符键时触发) keyup:释放键盘键 顺序为:keydown -> keypre ...