广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java实现LeetCode(1.两数之和)
  • 272
分享到

Java实现LeetCode(1.两数之和)

2024-04-02 19:04:59 272人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。 示例: 给定 nums = [2, 7, 11, 1

给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9

所以返回[0, 1]

思路一:最直接的思维,两次遍历查询,时间复杂度O(N*N)。

代码:


public static int[] twoSum1(int[] nums, int target) {
		int[] label = new int[2];
		for(int i=0;i<nums.length-1;i++) {
			int tmp = target - nums[i];
			for(int j=i+1;j<nums.length;j++) {
				if(tmp == nums[j]) {
					label[0] = i;
					label[1] = j;
				}
			}
		}
		return label;
	}

思路二:先排序,然后两个指针i,j,i从前开始,j从后开始查找,当nums[i]+nums[j]>target时,j--;当nums[i]+nums[j]<target时,i++;注意排序后,之前的下标数字已经变化了。时间复杂度O(N*Log2N)

代码:


public static int[] twoSum2(int[] nums, int target) {
		int[] label = new int[2];
		int[] tmpArr = new int[nums.length];
		for(int i=0;i<nums.length;i++) {
			tmpArr[i]=nums[i];
		}
		Arrays.sort(nums);
		int i=0;
		int j=nums.length-1;
		while (i<j) {
			if(nums[i]+nums[j]==target) {
				label[0] = nums[i];
				label[1] = nums[j];
				break;
			}else if(nums[i]+nums[j]>target){
				j--;
			}else {
				i++;
			}
		}
		for(int k=0;k<tmpArr.length;k++) {
			if(tmpArr[k]==label[0]) {
				label[0]=k;
			}
			if(tmpArr[k]==label[1]) {
				label[1]=k;
			}
		}
		return label;
	}

思路三:利用空间换时间方式,用HashMap存储数组结构,key为值,value为下标。时间复杂度O(N)。

代码:


public static int[] twoSum3(int[] nums, int target) {
		int[] label = new int[2];
		HashMap<Integer, Integer> hashMap = new HashMap<>();
		for(int i=0;i<nums.length;i++) {
			hashMap.put(nums[i], i);
		}
		for(int i=0;i<nums.length;i++) {
			if(hashMap.containsKey(target-nums[i])&&hashMap.get(target-nums[i])!=i) {
				label[0] = i;
				label[1] = hashMap.get(target-nums[i]);
				break;
			}
		}
		return label;
	}

GitHub地址:https://github.com/xckNull/AlGorithms-introduction

到此这篇关于Java实现LeetCode(两数之和)的文章就介绍到这了,更多相关Java实现两数之和内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java实现LeetCode(1.两数之和)

本文链接: https://www.lsjlt.com/news/129472.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java实现LeetCode(1.两数之和)
    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。 示例: 给定 nums = [2, 7, 11, 1...
    99+
    2022-11-12
  • Leetcode 1:两数之和
    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = ...
    99+
    2023-01-31
    之和 Leetcode
  • LeetCode如何实现两数之和
    小编给大家分享一下LeetCode如何实现两数之和,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!题目:给定一个整数数组 nums 和一个目标值 target,请你...
    99+
    2023-06-04
  • C++实现LeetCode(167.两数之和之二 - 输入数组有序)
    [LeetCode] 167.Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序 Given an array of integer...
    99+
    2022-11-12
  • C++实现LeetCode(170.两数之和之三 - 数据结构设计)
    [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计 Design and implement a ...
    99+
    2022-11-12
  • C++实现LeetCode(15.三数之和)
    [LeetCode] 15. 3Sum 三数之和 Given an array S of n integers, are there elem...
    99+
    2022-11-12
  • C++实现LeetCode(18.四数之和)
    [LeetCode] 18. 4Sum 四数之和 Given an array S of n integers, are there elements a, b, c, and d ...
    99+
    2022-11-12
  • C++实现LeetCode(191.位1的个数)
    [LeetCode] 191.Number of 1 Bits 位1的个数 Write a function that takes an unsigned integer and r...
    99+
    2022-11-12
  • C++实现LeetCode(29.两数相除)
    [LeetCode] 29. Divide Two Integers 两数相除 Given two integers dividend and divi...
    99+
    2022-11-12
  • C++实现LeetCode(16.最近三数之和)
    [LeetCode] 16. 3Sum Closest 最近三数之和 Given an array nums of n integers an...
    99+
    2022-11-12
  • Java实现LeetCode(报数)
    题目如下: public String countAndSay(int n) { if(n == 1){ return "1"; ...
    99+
    2022-11-12
  • LeetCode如何实现两个数字相加
    小编给大家分享一下LeetCode如何实现两个数字相加,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!题目给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,...
    99+
    2023-06-19
  • C++实现LeetCode(2.两个数字相加)
    [LeetCode] 2. Add Two Numbers 两个数字相加 You are given two non-empty linked lists rep...
    99+
    2022-11-12
  • C#算法如何实现两数之和
    小编给大家分享一下C#算法如何实现两数之和,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!题目给定一个整数数组 nums和一个目标值 targe...
    99+
    2023-06-26
  • c语言如何实现两数之和
    目录c语言实现两数之和c语言中比较两数大小题目要求分析一下c语言实现两数之和 int *twoSum(int *nums , int numsSize , int target , ...
    99+
    2022-11-13
  • C++实现LeetCode(209.最短子数组之和)
    [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和 Given an array of n positive in...
    99+
    2022-11-12
  • C++实现LeetCode(40.组合之和之二)
    [LeetCode] 40. Combination Sum II 组合之和之二 Given a collection of candidate numbers (candidate...
    99+
    2022-11-12
  • C++如何实现LeetCode两个数字相加
    这篇文章将为大家详细讲解有关C++如何实现LeetCode两个数字相加,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。[LeetCode] 2. Add Two Numbers 两个数字相加You are ...
    99+
    2023-06-20
  • C++实现LeetCode(39.组合之和)
    [LeetCode] 39. Combination Sum 组合之和 Given a set of candidate numbers (candidates)...
    99+
    2022-11-12
  • Java C++题解leetcode消失的两个数字实例
    目录题目要求思路:数学推导JavaC++Rust总结题目要求 思路:数学推导 不重复的数组序列可以根据高斯公式计算所有元素的总和:用当前数组长度加上两个缺失的数字可以得到所有数字长...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作