Leecode 1.两数之和(Java 哈希表)

1 class Solution {
2 public int[] twoSum(int[] nums, int target) {
3 Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
4 for (int i = 0; i < nums.length; ++i) {
5 if (hashtable.containsKey(target - nums[i])) {
6 return new int[]{hashtable.get(target - nums[i]), i};
7 }
8 hashtable.put(nums[i], i);
9 }
10 return new int[0];
11 }
12 }
13
14
15 作者:LeetCode-Solution
16 链接:https://leetcode-cn.com/problems/two-sum/solution/liang-shu-zhi-he-by-leetcode-solution/
17 来源:力扣(LeetCode)
18 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
我写的:
1 class Solution {
2 public int[] twoSum(int[] nums, int target) {
3 Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
4 int x = 0;
5 //从第一个元素x下标i开始
6 for(int i = 0;i<nums.length;i++){
7 //直至left=数组nums的长度-1
8 x = target-nums[i];
9 if(hashtable.containsKey(x)){
10 //若哈希表中无target-x的元素,则将key-x,value-left的键值对放入hashmap中
11 return new int[]{hashtable.get(x), i};
12 //期间若找到元素,则返回left和right。
13 }else{
14 hashtable.put(nums[i],i);
15 }
16 }
17 return new int[0];
18 }
19 }
Leecode 1.两数之和(Java 哈希表)的更多相关文章
- leetcode 刷题(数组篇)1题 两数之和(哈希表)
题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元 ...
- LeeCode:两数之和【1】
LeeCode:两数之和[1] 题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2 ...
- 【Leecode】两数之和
学习使用标准模板库(STL)中的map,hash_map.涉及数据结构知识:哈希表,红黑树. map的使用方法 https://www.cnblogs.com/fnlingnzb-learner/p/ ...
- 1. 两数之和 Java解法
这题属于Leetcode的签到题,基本上每个人一进来就是这题. 用哈希思想来做就是最好的解答. 如果一个target - num[i] 存在那么就返回那个数字对应的下标和当前元素的下标. public ...
- leecode刷题(8)-- 两数之和
leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输 ...
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- [Java]1.两数之和 - LeetCode
1 题目 2 思路与代码 思路一:暴力法(两层For循环) 时间复杂度:O(n^2) 对于每个元素,我们试图通过遍历数组的其余部分来寻找它所对应的目标元素,这将耗费 O(n) 的时间.因此时间复杂度为 ...
- LeetCode01 - 两数之和(Java 实现)
LeetCode01 - 两数之和(Java 实现) 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/two-sum 题目描述 给定一个整数数组 ...
- LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现
1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...
- leetCode:twoSum 两数之和 【JAVA实现】
LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...
随机推荐
- List集合转换成数组
我现在有个需求:将File集合转换成MultipartFile数组结构 然后我就开始在网上开启了List转换到数组之旅. 首先来看一个例子 ArrayList<String> list=n ...
- docker方式安装awvs和nessus渗透工具
docker-compose.yaml文件 version: '2' services: awvsnessus: image: leishianquan/awvs-nessus:v4 environm ...
- vue clickoutside 点击元素以外的区域隐藏该元素
一.什么是VueUseVueUse不是Vue.use !!!它是一个基于 Composition API 的实用函数集合,下面是具体的一些用法二.如何引入import { 具体方法 } from '@ ...
- vue provide inject 方法
上级组件: provide() { return { changeSelectOptions: this.changeSelectOptions, switchTabs: () => this. ...
- Hide-and-Seek: Forcing a Network to be Meticulous for Weakly-Supervised Object and Action Localization概述
0.前言 相关资料: paper 网站 论文解读(知乎,CSDN) 论文基本信息: 领域:弱监督动作定位 发表时间:ICCV2017 1.针对的问题 大多数网络只识别图像最具有鉴别力的部分,不是所有相 ...
- IE 浏览器element-ui table: show-overflow-tooltip 临界值时失效
问题: 在使用el-table表格时,有时会用到show-overflow-tooltip属性,即当单元格内容不能完全显示时显示tooltip文字提示,并且单元格里的内容显示省略号. 但是在ie浏览器 ...
- window.onerror的总结
// a.js window.onerror = function (message, url, line, column, error) { console.log('log---onerror:: ...
- JAVA根据时间增加1天
String time = "2021-12-1"; //指定时间 int day = 30;//指定增加天数 SimpleDateFormat sf = new SimpleDa ...
- 攻防世界Web篇——PHP2
可以从index.phps中找到网站源码 从源码中得出,要满足admin!=$_GET[id],urldecode($_GET[id]) == admin,两个条件才能得到flag 所以就将admin ...
- 为知笔记快速隐藏左侧 快捷键 Esc
为知笔记快速隐藏左侧 快捷键 Esc