leetcode: two sum, hash search solution, java. O(n)

来源:互联网 发布:新野美工设计培训中心 编辑:程序博客网 时间:2024/06/06 16:56

Two Sum

https://leetcode.com/submissions/detail/25788318/

Result:


Solution: O(n)

/***Input: numbers={0,4,3,0}, target=0*Output: index1=1, index2=2*/import java.util.*;public class Solution {//numbers= 5,75,25  target=100    public int[] twoSum(int[] numbers, int target) {    Map<Integer, Integer> map=new HashMap<>();        int length=numbers.length; //length=3        for(int i=0;i<length;i++){        int x=numbers[i]; //95        //search for index2        if(map.containsKey(target-x)){        return new int[] {map.get(target-x)+1,i+1 };        }        map.put(x,i);    }       throw new IllegalArgumentException("No Solution");}}



0 0
原创粉丝点击