当前位置:网站首页>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
边栏推荐
- 【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
- 数据孤岛是什么?为什么2022年仍然存在数据孤岛?
- Installation du compilateur croisé de la plateforme zynq
- 在AWS控制台创建VPC(无图版)
- AWS eks add cluster user or Iam role
- Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
- win10, mysql-8.0.26-winx64. Zip installation
- A heavy sword without a blade is a great skill
- Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
- IDE idea automatic compilation and configuration of on update action and on frame deactivation
猜你喜欢
STM32 MCU ADC rule group multi-channel conversion DMA mode
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
補:注解(Annotation)
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
[echart] Introduction to echart
Go反射法则
The perfect combination of collaborative process and multi process
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
随机推荐
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
Opencv -- yoact case segmentation model reasoning
MySQL queries users logged in for at least N consecutive days
Differences among electric drill, electric hammer and electric pick
Go 语言中的 logger 和 zap 日志库
IDE idea automatic compilation and configuration of on update action and on frame deactivation
Migrate from MySQL database to AWS dynamodb
IEEE Transactions on industrial information (TII)
leetcode007--判断字符串中的括号是否匹配
[echart] Introduction to echart
Jetpack 之 LifeCycle 组件使用详解
做数据可视化应该避免的8个误区
eksctl 部署AWS EKS
【Echart】echart 入门
STM32 MCU ADC rule group multi-channel conversion DMA mode
【论文阅读】【3d目标检测】point transformer
leetcode009--用二分查找在数组中搜索目标值
Redis command Encyclopedia
520.检测大写字母