当前位置:网站首页>【LeetCode】two num·两数之和
【LeetCode】two num·两数之和
2022-06-25 06:38:00 【AQin1012】
题目描述
英文版描述
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.
英文版地址
leetcode
https://leetcode.com/problems/two-sum/leetcode.com/problems/two-sum/
中文版描述
给定一个整数数组 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]
提示
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
只会存在一个有效答案
中文版地址
力扣
https://leetcode.cn/problems/two-sum/
解题思路
遍历数组,每获取到一个值,再去遍历他后面的值,看是否有值等于目标值减去当前值
解题方法
俺这版

class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
for(int i=0; i<nums.length; i++){
int cur = nums[i];
int needed = target-cur;
for(int j=(nums.length-1); j>i; j--){
if(nums[j] == needed){
result[0] = i;
result[1] = j;
return result;
}
}
}
return result;
}
}官方版
查找表法

class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>(nums.length);
map.put(nums[0], 0);
for(int i=1;i<nums.length;i++){
int cur = nums[i];
int needed = target - cur;
if(map.containsKey(needed)){
return new int[]{map.get(needed),i};
}else{
map.put(cur,i);
}
}
throw new IllegalArgumentException("Can not find!");
}
}边栏推荐
- The significance and proof of weak large number theorem
- 48 张图 | 手摸手教你微服务的性能监控、压测和调优
- StreamNative Platform 1.5 版本发布,集成 Istio、支持 OpenShift 部署
- [learn FPGA programming from scratch -43]: vision chapter - technology evolution of chip design in the post Moore era -2- evolution direction
- MySQL(十二)——更改表的备注
- 【C语言】给字符串增加分隔符
- Icon already includes gloss effects
- In depth analysis of Apache bookkeeper series: Part 3 - reading principle
- Loopholes in the missed scanning system of Lvmeng and its repair scheme
- [XXL job] the pond is green and the wind is warm. I remember that Yu Zhen first met
猜你喜欢
![[2022 dark horse programmer] SQL optimization](/img/68/92ee1ff47428e17aca0feb0e33382e.png)
[2022 dark horse programmer] SQL optimization
![Analysis on the trend of the number of national cinemas, film viewers and average ticket prices in 2021 [figure]](/img/01/594990789cbc1817dbbf61b7dd0c4c.jpg)
Analysis on the trend of the number of national cinemas, film viewers and average ticket prices in 2021 [figure]

有了 MySQL 为什么要用 NoSQL?

Uncaught TypeError: Cannot read properties of undefined (reading ‘prototype‘)

Keil debug view variable prompt not in scope

Are you still doing the dishes yourself? Teach you how to make dishwasher controller with single chip microcomputer

48 张图 | 手摸手教你微服务的性能监控、压测和调优

Love Terminator

What is the real future of hardware engineers?

Event registration | Apache pulsar x kubesphere online meetup is coming
随机推荐
MySQL - definition and assignment of variables
Finally, when you open source the applet ~
Event registration Apache pulsar x kubesphere online meetup hot registration
Rotation vector (rotation matrix) and Euler angle
Ctfhub web - divulgation d'informations - traversée du Répertoire
单片机IO详解(上拉 下拉 准双向 输入 输出 推挽 开漏)
父爱的表达方式
Atomic alpha development board -- SD card and EMMC burning tool
Jameswebb Space Telescope goes into operation to help study interstellar objects
How do I get red green blue (RGB) and alpha back from a UIColor object?
Design of PWM breathing lamp based on FPGA
活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中
Common cluster scripts
Navicat prevent new query from being deleted by mistake
What is the new business model of Taishan crowdfunding in 2022?
StreamNative Platform 1.5 版本发布,集成 Istio、支持 OpenShift 部署
Fastadmin cascade clear data
Blue Bridge Cup SCM module code (matrix key) (code + comments)
破万,我用了六年!
Esp8266 & sg90 steering gear & Lighting Technology & Arduino