twoSum Algorithm : How to improve this?

前端 未结 22 2133
礼貌的吻别
礼貌的吻别 2021-02-06 01:38

I felt like doing an algorithm and found this problem on leetcode

Given an array of integers, find two numbers such that they add up to a specific target num

22条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-06 01:56

    Using HashMap this is also a solution if complexity for search in HashMap will in order of O(logn) then in worst case the complexity will be O(nlogn)

        public int[] twoSum(int[] nums, int target) {
            
            int [] resultIndex = null;
            
            HashMap map = new HashMap();
            
            for(int i=0;i

提交回复
热议问题