Given an array of integers, return indices of the two numbers such that they add up to a specific ta
来源:互联网 发布:linux 搜索文件 编辑:程序博客网 时间:2024/05/17 23:31
import java.util.HashMap;
import java.util.Scanner;import javax.swing.text.NumberFormatter;public class Solution {public static int[] twoSum(int[] num,int target ) {final HashMap<Integer,Integer> myMap = new HashMap<Integer, Integer>();//int []result = new int[2];for (int i = 0; i < num.length; i++) {myMap.put( num[i],i);}for (int i = 0; i < num.length; i++) {int v = myMap.get( target - num[i]);if (v != 0 && v > i) {return new int[]{i,v};}}return null;}public static void main(String[] args) {int num[] = new int[]{2,7,11,15};Scanner scanner = new Scanner(System.in);int target = scanner.nextInt();int []array = twoSum(num, target);System.out.print("[");for (int i = 0; i < array.length; i++) {System.out.print(array[i]+" ");//System.out.print("");}System.out.print("]");}}
0 0
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- find two numbers such that they add up to a specific target number
- CareerCup Given an array of (unsorted) integers, arrange them such that a < b > c < d > e... etc.
- given an array of integers Find the index of values that satisfy A+B = C + D
- pointer to ref of an array, and to the func that return a ref of an array
- Given an array of integers, sort the array according to frequency of elements
- Given an array of numbers, return array of products of all other numbers (no division)
- Given a non-empty array of integers, return the k most frequent elements.
- Ruby: How to find all indices of elements that match a given condition?
- Given an array of ages (integers) sorted lowest to highest, output the number of occurrences for eac
- Given an array of integers, every element appears twice except for one. Find that single one.
- Given an array of integers, every element appears twice except for one. Find that single one.
- Given an array of integers, every element appears twice except for one. Find that single one.
- Given an array of integers, every element appears twice except for one. Find that single one.
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- How to find number of a specific integer in an array of Fixnums ruby
- CareerCup Given a binary matrix of N X N of integers , you need to return only unique rows of binary
- [LeetCode]371. Sum of Two Integers【&2. Add Two Numbers】
- 当心僵尸:过时Linux内核的安全风险
- 自己随意乱写保存着看!
- css练习2
- ssh传送文件
- Android Studio 技巧之 【Semicolon Dot Completion】
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- raid2.0详细分析
- JAVA的POI操作Excel
- 《向往的生活 》观后感
- 公共子序列
- 大数
- Python 字典(Dictionary) get()方法
- 计算机操作系统之存储管理
- 【git使用】fork后拷贝至本地;建远程仓库