《漫画算法2》源码整理-6 两数之和 三数之和
两数之和
import java.util.*;public class TwoSum {public static List<List<Integer>> twoSum(int[] nums, int target) {Map<Integer, Integer> map = new HashMap<>();List<List<Integer>> resultList = new ArrayList<>();for (int i = 1; i < nums.length; i++) {map.put(nums[i], i);}for (int i = 0; i < nums.length; i++) {int d = target - nums[i];if (map.containsKey(d) && map.get(d) != i) {resultList.add(Arrays.asList(nums[i], d));//为防止找到重复的元素对,匹配后从哈希表删除对应元素map.remove(nums[i]);}}return resultList;}public static List<List<Integer>> twoSumV2(int[] nums, int target) {Map<Integer, Integer> map = new HashMap<>();List<List<Integer>> resultList = new ArrayList<>();for (int i = 0; i < nums.length; i++) {int d = target - nums[i];if (map.containsKey(d)) {resultList.add(Arrays.asList(nums[i], d));}map.put(nums[i], i);}return resultList;}public static void main(String[] args) {int[] nums = {5,12,6,3,9,2,1,7};List<List<Integer>> resultList = twoSumV2(nums, 12);for(List<Integer> list : resultList){System.out.println(Arrays.toString(list.toArray()));}}}
三数之和
import java.util.*;public class ThreeSum {public static List<List<Integer>> threeSum(int[] nums, int target) {List<List<Integer>> resultList = new ArrayList<>();for (int i = 0; i < nums.length; i++) {Map<Integer, Integer> map = new HashMap<>();int d1 = target - nums[i];//寻找两数之和等于d1的组合for (int j = i+1; j < nums.length; j++) {int d2 = d1 - nums[j];if (map.containsKey(d2)) {resultList.add(Arrays.asList(nums[i], d2, nums[j]));}map.put(nums[j], j);}}return resultList;}public static List<List<Integer>> threeSumv2(int[] nums, int target) {Arrays.sort(nums);List<List<Integer>> resultList = new ArrayList<List<Integer>>();//大循环for (int i = 0; i < nums.length; i++) {int d = target - nums[i];// j和k双指针循环定位,j在左端,k在右端for (int j=i+1,k=nums.length-1; j<nums.length; j++) {// k指针向左移动while (j<k && (nums[j]+nums[k])>d) {k--;}//双指针重合,跳出本次循环if (j == k) {break;}if (nums[j] + nums[k] == d) {List<Integer> list = Arrays.asList(nums[i], nums[j], nums[k]);resultList.add(list);}}}return resultList;}public static void main(String[] args) {int[] nums = {5,12,6,3,9,2,1,7};List<List<Integer>> resultList = threeSumv2(nums, 12);for(List<Integer> list : resultList){System.out.println(Arrays.toString(list.toArray()));}}}
《漫画算法2》源码整理-6 两数之和 三数之和相关推荐
- 《漫画算法》源码整理-7
MyBitmap public class MyBitmap {//每一个word是一个long类型元素,对应64位二进制private long[] words;//bitmap的位数大小priva ...
- 《漫画算法》源码整理-5 排序算法
冒泡排序 import java.util.Arrays;public class BubbleSort {public static void sort(int array[]){int tmp = ...
- 《漫画算法》源码整理-6
判断链表有环 public class LinkedListCycle {/*** 判断是否有环* @param head 链表头节点*/public static boolean isCycle(N ...
- 《漫画算法》源码整理-4 大顶堆 小顶堆 优先队列
堆操作 import java.util.Arrays;public class HeapOperator {/*** 上浮调整* @param array 待调整的堆*/public static ...
- 《漫画算法》源码整理-3 二叉树遍历
二叉树前序 中序 后序 遍历 import java.util.Arrays; import java.util.LinkedList;public class BinaryTreeTraversal ...
- 《漫画算法》源码整理-2 数组 链表 队列
数组操作 public class MyArray {private int[] array;private int size;public MyArray(int capacity){this.ar ...
- 《漫画算法》源码整理-1 时间复杂度 空间复杂度
时间复杂度 public class TimeComplex {void eat1(int n){for(int i=0; i<n; i++){;System.out.println(" ...
- SURF算法与源码分析、下
FROM: http://www.cnblogs.com/ronny/p/4048213.html 上一篇文章 SURF算法与源码分析.上 中主要分析的是SURF特征点定位的算法原理与相关OpenCV ...
- bartlett 算法 matlab,GWO(灰狼优化)算法MATLAB源码逐行中文注解(转载)
以优化SVM算法的参数c和g为例,对GWO算法MATLAB源码进行了逐行中文注解. tic % 计时器 %% 清空环境变量 close all clear clc format compact %% ...
最新文章
- linux下搭建python 机器学习环境
- 区块链:游戏规则的改变者
- 怎样在Web开发中完美控制IE标题栏
- C语言函数中的参数有const的问题
- 物理DG主备库切换时遇到ORA-16139: media recovery required错误
- kubeadm部署k8s_用 kubeadm 部署生产级 k8s 集群
- python py2exe_转:py2exe 生成 python 可执行文件
- java对mysql的简单操作的综合运用——登录+注册+修改密码
- STL---常用函数用法大集合-=-更新中...
- 计算机管理员账户权限不足,用户权限不足,请使用管理员权限。怎么办啊?求高手帮忙!谢了。...
- WAP PUSH——服务信息
- Spring中注解实现原理
- 金融数据api接口记录(二)
- 菜谱分享网站微信小程序开发说明(2)-数据库
- 基于javaweb个人网站论坛的设计与实现(源码、论文、毕业设计、数据库文件)
- matlab常用函数汇总
- 【ClearCase 学习笔记 】之(4/4)CC基本操作介绍
- 文档大小超出上传限制怎么办_压缩PDF大小该怎么操作?压缩PDF的软件有哪些?...
- echarts实现省份地图
- Unity 基础开发-WebGL发布无法运行问题