当前位置:网站首页>力扣-两数之和
力扣-两数之和
2022-07-01 03:02:00 【好奇的菜鸟】
一、题目
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]
示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]
二、解法1
public int[] twoSum1(int[] nums, int target) {
int[] indexs = new int[2];
// 双重循环 循环极限为(n^2-n)/2
for (int i = 0; i < nums.length; i++) {
for (int j = nums.length - 1; j > i; j--) {
if (nums[i] + nums[j] == target) {
indexs[0] = i;
indexs[1] = j;
System.out.println(Arrays.toString(indexs));
return indexs;
}
}
}
return indexs;
}三、解法2
public int[] twoSum3(int[] nums, int target) {
int[] indexs = new int[2];
// 建立k-v ,一一对应的哈希表
HashMap<Integer, Integer> hash = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (hash.containsKey(nums[i])) {
indexs[0] = i;
indexs[1] = hash.get(nums[i]);
System.out.println(Arrays.toString(indexs));
return indexs;
}
// 将数据存入 key为补数 ,value为下标
hash.put(target - nums[i], i);
}
return indexs;
}边栏推荐
- 手把手带你了解一块电路板,从设计到制作(干货)
- How to determine the progress bar loaded in the loading interface when opening the game
- MySQL knowledge points
- How to open a stock account? Also, is it safe to open an account online?
- An article explaining the publisher subscriber model and the observer model
- 实战 ELK 优雅管理服务器日志
- EtherCAT原理概述
- MySQL index --01--- design principle of index
- Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
- [applet project development -- Jingdong Mall] classified navigation area of uni app
猜你喜欢

Densenet network paper learning notes

php批量excel转word

EtherCAT原理概述
![[applet project development -- JD mall] uni app commodity classification page (first)](/img/6c/5b92fc1f18d58e0fdf6f1896188fcd.png)
[applet project development -- JD mall] uni app commodity classification page (first)

How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet

Dell server restart Idrac method

Restcloud ETL practice to realize incremental data synchronization without identification bit
Common interview questions for performance test

VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license

Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend
随机推荐
Classic programming problem: finding the number of daffodils
An article explaining the publisher subscriber model and the observer model
Design practice of current limiting components
Redis 教程
Borrowing constructor inheritance and composite inheritance
Multithreaded printing
STM32——一线协议之DS18B20温度采样
EtherCAT原理概述
Is it safe to open a stock account? Shanghai stock account opening procedures.
单片机 MCU 固件打包脚本软件
JS to find duplicate elements in two arrays
[linear DP] longest common subsequence
So easy 将程序部署到服务器
mybati sql 语句打印
Mouse over effect II
Mysql知识点
Od modify DLL and exe pop-up contents [OllyDbg]
Mouse over effect V
HTB-Lame
彻底解决Lost connection to MySQL server at ‘reading initial communication packet