LeetCode算法入门- 3Sum -day9

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;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/558879.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

XML学习(一)————XML简介

引言 作为数据传输界鼎鼎大名的扛把子&#xff0c;XML被应用于各个方面&#xff0c;但随着弱结构化标记语言如JSON、YAML等的出现&#xff0c;人们慢慢的脱离了XML的统治&#xff0c;但在互联网早期的发展当中XML是不可或缺的一部分&#xff0c;比如各种微信开发中的数据传输&…

Java核心篇之Java锁--day2

Java核心篇之Java锁–day2 乐观锁&#xff1a;乐观锁是一种乐观思想&#xff0c;即认为读多写少&#xff0c;每次去取数据的时候都认为其他人不会修改&#xff0c;所以不会上锁&#xff1b;但是在更新的时候会判断一下在此期间别人有没有去修改它&#xff0c;如果有人修改的话…

XML学习(二)————属性还是标签?

引言 xml中并没有规则要求我们什么时候使用属性&#xff0c;什么时候使用标签。 属性和标签都可以存储数据&#xff0c;但是在XML的使用中&#xff0c;我们需要探讨一下对属性和标签的选择问题。 约定规则 XML 应该避免使用属性来存储数据&#xff0c;这与HTML的推荐规则不…

LeetCode算法入门- 3Sum Closest -day10

LeetCode算法入门- 3Sum Closest -day10 Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one …

Spring Boot————Web应用启动时自动执行ApplicationListener用法

原文&#xff1a;《web服务启动spring自动执行ApplicationListener的用法》 引言 我们知道&#xff0c;一般来说一个项目启动时需要加载或者执行一些特殊的任务来初始化系统&#xff0c;通常的做法就是用servlet去初始化&#xff0c;但是servlet在使用Spring bean时不能直接注…

LeetCode算法入门- 4Sum -day11

LeetCode算法入门- 4Sum -day11 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a b c d target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution se…

Spring Boot————ApplicationListener实现逃课事件监听

引言 上一篇文章转了一篇关于ApplicationListener用于在Web项目启动时做一些初始化的用法。 但是&#xff0c;在实际生产过程中&#xff0c;当一个事件产生&#xff0c;又是如何被onApplicationEvent()方法监听到&#xff0c;并执行一系列动作呢&#xff1f;简单搜索了一下&a…

Java核心篇之Redis--day4

Java核心篇之Redis–day4 Redis有哪些数据结构&#xff1f; 字符串String、字典Hash、列表List、集合Set、有序集合SortedSet。 1.String&#xff1a;字符串&#xff0c;常用命令&#xff1a;get&#xff0c;set&#xff0c;decr&#xff0c;incr&#xff0c;mget&#xff08;查…

软件版本GA、RC、beta等含义

原文《软件版本GA、RC、beta等含义》 GA General Availability&#xff0c;正式发布的版本&#xff0c;官方开始推荐广泛使用&#xff0c;国外有的用GA来表示release版本。 RELEASE 正式发布版&#xff0c;官方推荐使用的版本&#xff0c;有的用GA来表示。比如spring。 Sta…

Java核心篇之泛型--day5

Java核心篇之泛型–day5 泛型是JDK5时引入的一个新特性&#xff0c;泛型提供了编译时类型安全检查的机制&#xff0c;该机制允许程序猿在编译时检测到非法的类型输入。 泛型的本质是参数化类型&#xff0c;也就是说操作的类型被指定为一个参数。 假定我们有一个需求&#xff…

Spring Boot————AOP入门案例及切面优先级设置

看了这篇文章&#xff0c;如果你还是不会用AOP来写程序&#xff0c;请你打我&#xff01;&#xff01; .||| 引言 Spring AOP是一个对AOP原理的一种实现方式&#xff0c;另外还有其他的AOP实现如AspectJ等。 AOP意为面向切面编程&#xff0c;是通过预编译方式和运行期动态代…

Java核心篇之HashMap--day6

Java核心篇之HashMap–day6 HashMap是一种键值对的数据结构&#xff0c;以数组与链表的形式&#xff08;key&#xff1a;value&#xff09;实现&#xff0c;查询性能和添加性能很好。他是通过将key进行hashcode&#xff08;&#xff09;映射函数来找到表中对应的位置。 HashM…

Spring Boot————Spring Data JPA简介

引言 JPA是Java 持久化API的缩写&#xff0c;是一套Java数据持久化的规范&#xff0c; Spring Data Spring Data项目的目的是为了简化构建基于Spring 框架应用的数据访问技术&#xff0c;包括对关系型数据库的访问支持。另外也包含非关系型数据库、Map-Reduce框架、云数据服…

LeetCode算法入门- Valid Parentheses -day11

LeetCode算法入门- Valid Parentheses -day11 题目描述&#xff1a; Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed …

Spring Boot————Spring Boot启动流程分析

一、引言 Spring Boot 的启动虽然仅仅是执行了一个main方法&#xff0c;但实际上&#xff0c;运行流程还是比较复杂的&#xff0c;其中包含几个非常重要的事件回调机制。在实际生产开发中&#xff0c;有时候也会利用这些启动流程中的回调机制&#xff0c;做一些项目初始化的工…

LeetCode算法入门- Longest Valid Parentheses -day12

LeetCode算法入门- Longest Valid Parentheses -day12 Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring. 题目描述&#xff1a; 题目的意思是给定一个括号序列的字符串&#xff…

Spring Boot————应用启动时的监听机制测试

引言 本文承接前面的《Spring Boot————Spring Boot启动流程分析》&#xff0c;主要测试一下ApplicationContextInitializer、SpringApplicationRunListener、ApplicationRunner、CommandLineRunner这四个接口实现之下的组件是何时在Spring Boot项目启动时创建并执行相关方…

LeetCode算法入门- Longest Common Prefix -day13

LeetCode算法入门- Longest Common Prefix -day13 题目描述&#xff1a; Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string “”. Example 1: Input: [“flower”,“flow”…

HTMLCSS————块元素与内联元素

一、块元素与div标签 div是一个块元素&#xff0c;块元素会独占一行&#xff0c;无论它的内容有多少&#xff0c;都会独占一整行。 类似的块元素还有&#xff1a;<p>、<h1>、<h2>、<h3> 然而&#xff0c;<div>标签没有任何语义&#xff0c;就…

LeetCode算法入门- Compare Version Numbers -day14

LeetCode算法入门- Compare Version Numbers -day14 题目描述&#xff1a; Compare two version numbers version1 and version2. If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0. You may assume that the version strings a…