算法练习-001-返回目标数的下标组合

 question:

1
2
3
4
5
6
7
8
9
10
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
 
You may assume that each input would have exactly one solution, and you may not use the same element twice.
 
Example:
 
Given nums = [2, 7, 11, 15], target = 9,
 
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

 


my solution
复制代码
class Solution {
    public int[] twoSum(int[] nums, int target) {
         int [] result = new int[2] ;
     for (int i = 0; i < nums.length-1; i++) {
            for (int j = i+1; j < nums.length; j++) {
                if(nums[i]+nums[j] == target){
                    result[0]=i;
                    result[1]=j;
                    //System.out.println("elem1:"+i+";elem2:"+j);
                }
        } 
    }
        return result;
        
    }
}
复制代码

 

 

 

 

 

 

7ms的comminter code

复制代码
class Solution {
    public int[] twoSum(int[] nums, int target) {
        int m = 0;
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
         for (int i=0; i < nums.length; i++){
             map.put(nums[i], i);
         }
        for(int j=0; j<nums.length; j++){
            m = target - nums[j];
            if (map.containsKey(m)&&(map.get(m)!=j))
                return new int[] {j, map.get(m)};
        }
        throw new IllegalArgumentException("No two sum solution");
    }
    
}
复制代码

 

 

 

 

 

 

5ms的comminter code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
    public int[] twoSum(int[] nums, int target) {
        if(nums == null) return new int[0];
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i=0; i<nums.length; i++){
            int complement = target - nums[i];
            if(map.containsKey(complement)){
                return new int[]{i,map.get(complement)};
            }
            else map.put(nums[i], i);
        }
        return new int[0];
    }
}

  

 

 

4ms的comminter code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
    public int[] twoSum(int[] nums, int target) {
        int len=nums.length;
        HashMap<Integer, Integer> map=new HashMap<>();
        map.put(nums[0], 0);
        for(int i=1;i<len;i++){
            if(map.containsKey(target-nums[i])){
                int[] returnArray={map.get(target-nums[i]),i};
                return returnArray;
            } else{
                map.put(nums[i], i);
            }
        }
        int[] returnArray={0,0};
        return returnArray;
    }
}

 

3ms的comminter code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] res = new int[]{-1, -1};
        if (nums == null || nums.length < 2) return res;
        int n = nums.length;
        int left = 0;
        int right = n - 1;
        for(int i = 0; i < n; i++) {
            nums[i] = nums[i] * n + (nums[i] < 0 ? -i : i);
        }
        Arrays.sort(nums);
 
        while (left < right) {
            int sum = nums[left] / n + nums[right] / n;
            if (sum == target) {
                res[0] = nums[left] < 0 ? -nums[left] % n : nums[left] % n;
                res[1] = nums[right] < 0 ? -nums[right] % n : nums[right] % n;
                return res;
            } else if (sum < target) {
                left++;
            } else {
                right--;
            }
        }
        throw new IllegalArgumentException();
    }
}

  

 

 

 

2ms的comminter code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
    public int[] twoSum(int[] nums, int target) {
    int max = 2048;
    int[] indexes = new int[max];
    int bitMode = --max;
    int first = nums[0];
 
    for (int i = 1; i < nums.length; i++) {
      int difference = target - nums[i];
      if (difference == first) {
        return new int[]{0, i};
      }
      int index = indexes[difference&bitMode];
      if(index != 0) {
        return new int[]{index, i};
      }
      indexes[nums[i]&bitMode] = i;
    }
    return new int[0];
  }
}

  

  

 

posted @   斑马森林  阅读(389)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示