Two Sum

  • 問題
    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].
  • 解決
package com.lsy.leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TwoSum {
    
     public static int[] twoSum1(int[] nums, int target) {
            int size=nums.length;
            int[] solution=new int[2];
            for(int i=0;i<size;i++) {
                int exact=target-nums[i];
                for(int j=i+1;j<size;j++) {
                    if(nums[j]==exact) {
                        solution[0]=i;
                        solution[1]=j;
                        return solution;}
                }
            }
            
            return solution;
            
        }

     
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] nums= {3,2,4};
        int target=6;
        System.out.println(Arrays.toString(twoSum1(nums,target)));
        //Arrays類得到了補充完整的Arrays.toString方法。專門為了將任何類型的數組轉變?yōu)樽址O計的。
        //數組直接tostring輸出是地址
    }
//brute force:  %time o(n^2):最壞,n(n-1)/2   %space  o(1)最壞幾個
    public int[] twoSum2(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {     //直接nums.length
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] == target - nums[i]) {
                    return new int[] { i, j };     //簡潔,初始化  new int[]{i,j}
                }
            }
        }
        throw new IllegalArgumentException("No two sum solution"); //不輸出就扔出異常
    }
//Two-pass Hash Table %time o(1):最壞n+n  系數不重要 2n=n  %space o(n)
    public int[] twoSum3(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();  //數據結構
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);                    //key value的選擇
        }
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (map.containsKey(complement) && map.get(complement) != i) {
                return new int[] { i, map.get(complement) };
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }



//One-pass Hash Table  %time o(n) %space o(n)
public int[] twoSum4(int[] nums, int target) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {    //一個循環(huán)解決
        int complement = target - nums[i];
        if (map.containsKey(complement)) {      //肯定不包括自己   
            return new int[] { map.get(complement), i };
        }
        map.put(nums[i], i);
    }
    throw new IllegalArgumentException("No two sum solution");
}
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容