当前位置:网站首页>Leetcode top 100 questions 1 Sum of two numbers
Leetcode top 100 questions 1 Sum of two numbers
2022-07-01 05:25:00 【weixin_ forty-one million two hundred and sixty-two thousand on】
Given an array of integers nums And an integer target value target, Please find... In the array And is the target value target the Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
You can return the answers in any order .

class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i]+nums[j]==target) {
retrun new int[]{
i,j};
}
}
}
return new int[0];
}
}
This method is very simple , Direct two nested traversal , You can make a judgment every time you traverse , But the time complexity is high ,O(n^2)
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hasmap= new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; ++i) {
if (hashtable.containsKey(target - nums[i])) {
return new int[]{
hashtable.get(target - nums[i]), i};
}
hashtable.put(nums[i], i);
}
return new int[0];
}
}
You can also use hash Do it with a watch , use hashMap.containsKey() Methods to determine , Whether it conforms to key stay hash In the table , among key Is to use the value of the array ,value Use array subscripts .
边栏推荐
- Dynamic verification of new form items in El form; El form verifies that the dynamic form V-IF does not take effect;
- Daily question -leetcode1175- permutation of prime numbers - Mathematics
- Flutter 实现每次进来界面都刷新数据
- Ebpf cilium practice (2) - underlying network observability
- Implementation of distributed lock
- Leetcode316- remove duplicate letters - stack - greedy - string
- 3D建模與處理軟件簡介 劉利剛 中國科技大學
- Cockroachdb: the resistant geo distributed SQL database paper reading notes
- 提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
- LevelDB源码分析之memtable
猜你喜欢

Copy baby prompt: material cannot be empty. How to solve it?

El cascade echo failed; El cascader does not echo

LRU cache for leveldb source code analysis

busybox生成的东西

Rainbow combines neuvector to practice container safety management

How to traverse massive data in redis

Actual combat: gateway api-2022.2.13

el-form表单新增表单项动态校验;el-form校验动态表单v-if不生效;

了解 JVM 中几个相关问题 — JVM 内存布局、类加载机制、垃圾回收

Use and principle of AQS related implementation classes
随机推荐
担心侵权?必备无版权素材网站分享,不用担心视频剪辑缺素材
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
複制寶貝提示材質不能為空,如何解决?
液压滑环的特点讲解
Design and application of immutable classes
Application and principle of ThreadPoolExecutor thread pool
AcWing 887. Finding combinatorial number III (Lucas theorem)
Introduction of 3D Modeling and Processing Software Liu Ligang, Chinese University of Science and Technology
Leetcode316- remove duplicate letters - stack - greedy - string
SSGSSRCSR区别
Spanner 论文小结
AcWing 888. Finding combinatorial number IV (the problem of finding combinatorial number with high precision)
What can the points mall Games bring to businesses? How to build a points mall?
数字金额加逗号;js给数字加三位一逗号间隔的两种方法;js数据格式化
3D建模与处理软件简介 刘利刚 中国科技大学
Vmware workstation network card settings and three common network modes
新手在挖财开通证券账户安全吗?
Global and Chinese markets of gps/gnss receiver modules 2022-2028: Research Report on technology, participants, trends, market size and share
Using nocalhost to develop microservice application on rainbow
Unity project experience summary