• LeetCode-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.leetcode.study;
    
    public class Main {
    
        public static void main(String[] args) {
            int[] nums = new int[]{2,7,11,15};
            int target = 9;
            System.out.println(twoSum(nums, target)[0]);
            System.out.println(twoSum(nums, target)[1]);
    
        }
        
        public static int[] twoSum(int[]nums,int target){
            int first=0;
            int second=0;
            boolean flag=false;
            for(int i = 0;i<nums.length;i++){
                for(int j = 0;j<nums.length;j++){
                    if(i==j)continue;
                    if(nums[i]+nums[j]==target){
                        first=i;
                        second=j;
                        flag=true;
                        break;
                    }
                }
                if(flag==true){
                    break;
                }
            }
            return new int[]{first,second};
        }
    
    }

    我的思路是两重循环,来计算和。

     我的思路比较耗时,下面看LeetCode提供的;另外两种速度较快的算法。

    package com.leetcode.study;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class Main {
    
        public static void main(String[] args) {
            int[] nums = new int[]{2,7,11,15};
            int target = 9;
            System.out.println(twoSum(nums, target)[0]);
            System.out.println(twoSum(nums, target)[1]);
    
        }
        
        public static int[] twoSum(int[]nums,int target){
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            for(int i = 0;i<nums.length;i++){
                map.put(nums[i],i);
            }
            for(int i = 0;i<nums.length;i++){
                int res = target-nums[i];
                if(map.containsKey(res)){
                    return new int[]{i,map.get(res)};
                }
            }
            throw new IllegalArgumentException("No two sum solution.");
        }
    
    }

    这种方法使用了java里面的hashmap集合。首先通过一次循环把所有的数放到hashmap中,然后,通过hashmap的containsKay方法来判断。

    还有一种方法是更简单的方法。使用一次循环就可以。下面看代码。

    package com.leetcode.study;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class Main {
    
        public static void main(String[] args) {
            int[] nums = new int[]{2,7,11,15};
            int target = 9;
            System.out.println(twoSum(nums, target)[0]);
            System.out.println(twoSum(nums, target)[1]);
    
        }
        
        public static int[] twoSum(int[]nums,int target){
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            for(int i = 0;i<nums.length;i++){
                int res = target-nums[i];
                if(map.containsKey(res)){
                    return new int[]{map.get(res),i};
                }
                map.put(nums[i], i);
            }
            throw new IllegalArgumentException("No two sum solution.");
        }
    
    }
  • 相关阅读:
    玩不转云计算的架构
    从《从架构的角度看,如何写好代码?》中来看如何编写单元测试代码
    换种形式工作
    程序员下一门要学的编程语言Swift
    从钉钉微应用定制化导航栏看如何实现Hybrid App开发框架
    纯灌水Linus主义
    kFreeBSD有活过来的迹象?UbuntuBSD
    架构的重要性
    MacOS下如何进行Git的冲突(Conflict)处理
    [转]以Facebook为案例剖析科技公司应有的工具文化
  • 原文地址:https://www.cnblogs.com/LoganChen/p/8782630.html
Copyright © 2020-2023  润新知