当前位置:网站首页>Leetcode001 -- returns the subscript of the array element whose sum is target
Leetcode001 -- returns the subscript of the array element whose sum is target
2022-04-23 04:39:00 【singularityDZF】
import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class test01 {
/**
* 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 of , 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 .
*
* Example 1:
* Input :nums = [2,7,11,15], target = 9
* Output :[0,1]
* explain : because nums[0] + nums[1] == 9 , return [0, 1] .
*
* Example 2:
* Input :nums = [3,2,4], target = 6
* Output :[1,2]
*
* Example 3:
* Input :nums = [3,3], target = 6
* Output :[0,1]
*
* Tips :
* 2 <= nums.length <= 104
* -109 <= nums[i] <= 109
* -109 <= target <= 109
* There will only be one valid answer
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the size of the array :");
int size = sc.nextInt();
System.out.println(" Please enter "+size+" Number :");
int[] nums = new int[size];
for( int i = 0; i < size; i++){
nums[i] = sc.nextInt();
}
System.out.println(" Please enter the target value :");
int target = sc.nextInt();
int[] result = twoSumHashmap(nums,target);
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i=0; i< result.length-1; i++){
sb.append(result[i]);
sb.append(",");
}
sb.append(result[result.length-1]);
sb.append("]");
System.out.println(sb);
}
public static int[] twoSum(int[] nums, int target){
int length = nums.length;
int[] result = new int[2];
for(int i=0; i<length-1; i++){
for (int j=i+1; j<length; j++){
if( nums[i] + nums[j] == target ){
result[0] = i;
result[1] = j;
return result;
}
}
}
return new int[0];
}
/**
* use static The method of decoration , This method can be called without generating an instance object of the class .
* No, static The method of decoration , You need to generate an instance object of a class before you can call this method .
* @param nums
* @param target
* @return
*/
public static int[] twoSumHashmap(int[] nums, int target){
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for( int i=0; i<nums.length; i++){
if(map.containsKey(target-nums[i])){
map.put(nums[i],i);
return new int[]{map.get(target-nums[i]),i};
}
map.put(nums[i],i);
}
return new int[0];
}
}
版权声明
本文为[singularityDZF]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230438035572.html
边栏推荐
- AWS eks add cluster user or Iam role
- Eksctl deploying AWS eks
- Mysql---数据读写分离、多实例
- Brushless motor drive scheme based on Infineon MCU GTM module
- 補:注解(Annotation)
- Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
- shell wc (统计字符数量)的基本使用
- 383. Ransom letter
- 2020 is coming to an end, special and unforgettable.
- 华为机试--高精度整数加法
猜你喜欢

Understand the gut organ axis, good gut and good health

基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦

Installation du compilateur croisé de la plateforme zynq

单片机串口数据处理(2)——uCOSIII+循环队列接收数据

Installation of zynq platform cross compiler

test

Phishing for NFT

AWS EKS 部署要点以及控制台与eksctl创建的差异

Recursive call -- Enumeration of permutations

补充番外14:cmake实践项目笔记(未完待续4/22)
随机推荐
无线键盘全国产化电子元件推荐方案
io. Platform. packageRoot; // ignore: deprecated_ Member_ use
Supplement 14: cmake practice project notes (to be continued 4 / 22)
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
thymeleaf th:value 为null时报错问题
383. 赎金信
FAQ of foreign lead and alliance Manager
重剑无锋,大巧不工
STM32 MCU ADC rule group multi-channel conversion DMA mode
leetcode006--查找字符串数组中的最长公共前缀
【Pytorch基础】torch.split()用法
Phishing for NFT
Recursive call -- Enumeration of permutations
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
Installation du compilateur croisé de la plateforme zynq
MYSQL查询至少连续n天登录的用户
[paper reading] [3D object detection] voxel transformer for 3D object detection
leetcode008--实现strStr()函数
电钻、电锤、电镐的区别