LeetCode算法入门- 3Sum -day9

  1. 题目描述:
    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]

  1. 思路分析:
    题目的意思是找到数组中所有3个和为0的数,并且不能重复。

该题可以转化成Two Sum的思路去解决先固定一个数,然后从数组中剩下的数中查找和为该数负值(target)得2个数,则转化成了Two Sum问题:1. 先排序数组,使两个指针分别指向首尾的两个数,2. 如果这两个数和等于target,则找到,3. 如果小于target则右移左指针,如果大于target则左移右指针。

  1. 关键是题目要求去重,所以每次移动指针的时候要判断一下是否和上一个数相同,如果相同则继续移动。
    代码如下:
class Solution {public List<List<Integer>> threeSum(int[] nums) {int len = nums.length;List<List<Integer>> result = new ArrayList<>();//记得先排序,这样才能够排除重复的答案Arrays.sort(nums);for(int i = 0; i < len; i++){//这里的i != 0的判断目的是为了i-1不越界if(i != 0 && nums[i] == nums[i - 1])//continue语法很少用,若条件满足,则不执行当次循环的代码,i要继续+1continue;int target = -nums[i];int left = i + 1;int right = len - 1;while(left < right){if(nums[left] + nums[right] == target){//Arrays.asList()这个方法是直接将元素添加到temp中去List<Integer> temp = Arrays.asList(nums[i],nums[left],nums[right]);result.add(temp);left++;right--;//去重同时记得判断left<rightwhile(left < right && nums[left] == nums[left-1])left++;while(left < right && nums[right] == nums[right+1])right--;}else if(nums[left] + nums[right] < target){left++;}else{right--;}}}return result;}
}

LeetCode算法入门- 3Sum -day9相关推荐

  1. LeetCode算法入门- 3Sum Closest -day10

    LeetCode算法入门- 3Sum Closest -day10 Given an array nums of n integers and an integer target, find thre ...

  2. LeetCode算法入门- Implement strStr() -day22

    LeetCode算法入门- Implement strStr() -day22 题目描述 Implement strStr(). Return the index of the first occur ...

  3. LeetCode算法入门- Remove Duplicates from Sorted Array -day21

    LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...

  4. LeetCode算法入门- Remove Element -day20

    LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instance ...

  5. LeetCode算法入门- Search Insert Position -day19

    LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...

  6. LeetCode算法入门- Multiply Strings -day18

    LeetCode算法入门- Multiply Strings -day18 题目介绍 Given two non-negative integers num1 and num2 represented ...

  7. LeetCode算法入门- Remove Nth Node From End of List -day17

    LeetCode算法入门- Remove Nth Node From End of List -day17 题目解释: Given a linked list, remove the n-th nod ...

  8. LeetCode算法入门- Generate Parentheses -day16

    LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...

  9. LeetCode算法入门- Merge Two Sorted Lists -day15

    LeetCode算法入门- Merge Two Sorted Lists -day15 题目描述: Merge two sorted linked lists and return it as a n ...

最新文章

  1. 除了计算机中的二进制还有哪些进位制,除了十进位制还有什么进位制
  2. matlab画三维心,matlab动态三维心形(最新整理)
  3. 柴油发电机组常见故障及处理方法
  4. 【Python基础】Python 流程控制专题总结
  5. Springboot——HelloWorld
  6. 并发编程——进程池与线程池
  7. easyui增删改查前段代码
  8. docker容器的标准使用过程_phpStorm中使用xdebug工具调试docker容器中的程序
  9. make *** 没有指明目标并且找不到 makefile。 停止。_Makefile目标文件搜索(VPATH和vpath)...
  10. Windows Mobile 数独游戏及全部源码
  11. pytorch 中的数据类型,tensor的创建
  12. JVM篇2:[-加载器ClassLoader-]
  13. 软帝java培训实习日志,在软帝学习的第一个星期的小总结
  14. 计算机网络暗地里范围,《计算机网络应用技术教程》期中试题.doc
  15. idea导入导出 settings 设置文件
  16. 华硕电脑键盘灯不亮怎么办
  17. javascript设计模式--设计原则
  18. 基于vue3 实现页面自动配色、颜色选择器组件
  19. java中的this是什么意思
  20. Excel里面自动获取当前时间

热门文章

  1. WebApi开启CORS支持跨域POST
  2. 浏览器播放rtsp视频流方案(ffmpeg + nginx转m3u8)
  3. 计算机ppt文字1是什么原因,ppt让答案一个个出现,ppt让文字一个个出现
  4. Mysql的MVCC是什么
  5. python io操作有什么_Python笔记:文件IO操作
  6. dos 改某个目录下所有文件的时间_go语言入门学习笔记(2)-DOS操作及go语言变量学习...
  7. 微型计算机的系统组成图,微型计算机系统结构图.doc
  8. 猜数字游戏python程序用函数guesssecret_Python-三、函数
  9. eclipse不进入断点_Eclipse 调试中不支持运行到断点问题
  10. spring(java,js,html) 截图上传