zoukankan      html  css  js  c++  java
  • 01. 两数之和

    难度:简单
    题目描述:
    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
     

    你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

     


    示例:

     

    给定 nums = [2, 7, 11, 15], target = 9

     

    因为 nums[0] + nums[1] = 2 + 7 = 9

    所以返回 [0, 1]


     

     

    方法一:暴力解法

    遍历每个元素X,查找是否存在一个值与target-x相等的目标元素

     1 class Solution {
     2     public int[] twoSum(int[] nums, int target) {
     3         for(int i = 0; i < nums.length ; i++){
     4             for(int j = i+1; j< nums.length; j++){
     5                 if(nums[j] == target - nums[i])
     6                     return new int[]{i,j};
     7             }
     8         }
     9         throw new IllegalArgumentException("No two sum solution");
    10     }
    11 }
    View Code
    注意:
            返回数组长度: array.length;
            返回字符串长度: string.length();
     
     
    方法二:遍历两遍哈希表
    在第一次迭代中,将nums中每个元素的值及索引添加到map中;
    在第二次迭代中,检查每个元素所对应的目标元素(target-nums[i])是否在表中,注意的是该目标元素不能是nums[i]本身。
     1 class Solution {
     2     public int[] twoSum(int[] nums, int target) {
     3         Map<Integer,Integer> map = new HashMap<>();
     4         // 将数组的值以及数组的下标放入map中
     5         for(int i = 0;i < nums.length;i++){
     6             map.put(nums[i],i);
     7         }
     8         for(int i = 0; i < nums.length;i++){
     9             int complment = target - nums[i];
    10             // 判断map中是否包含complment 这个数以及complment的数值不是i本身这个数
    11             if(map.containsKey(complment) && map.get(complment) != i)
    12                 return new int[]{i,map.get(complment)};
    13         }
    14         throw new IllegalArgumentException("No two sum solution");
    15     }
    16 }
    View Code
     
    方法三:遍历一遍哈希表
    在进行迭代并将元素插入到表中的同时,我们还会回过头来检查表中是否已经存在当前元素所对应的目标元素。如果它存在,那我们已经找到了对应解,并立即将其返回。
     1 class Solution {
     2     public int[] twoSum(int[] nums, int target) {
     3 
     4         Map<Integer,Integer> map = new HashMap<>();
     5         for(int i = 0; i < nums.length; i++){
     6             int complment = target - nums[i];
     7             if(map.containsKey(complment))
     8                 return new int[]{i,map.get(complment)};
     9             map.put(nums[i],i);
    10         }
    11         throw new IllegalArgumentException("No two sum solution");
    12     }
    View Code
     

    补充知识
     
    1.Map<K,V>是一种映射表,可以通过key快速查找value
     
    2.最常用的一种Map实现是HashMap
     
    此题中用到的Map方法
     
    序号 方法描述
    1 V put(K key, V value)
    将指定的值与此映射中的指定键关联(可选操作)。如果此映射以前包含一个该键的映射关系,则用指定值替换旧值(当且仅当 m.containsKey(k) 返回 true 时,才能说映射 m 包含键 k 的映射关系)。
    2 V get(Object key)
    返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null。
    3 boolean containsKey(Object key)
    如果此映射包含指定键的映射关系,则返回 true。
     
     
     

    <wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

  • 相关阅读:
    Mooncake (排序+贪心)
    The Black Hole of Numbers (strtoint+inttostr+sort)
    随机排序
    EF 随机排序
    禁用DropDownList的Items
    sql之left join、right join、inner join的区别
    SQL优化-索引
    .net 面试问题 汇总
    从简单的例子理解泛型
    1.1 使用内置的Camera应用程序捕获图像
  • 原文地址:https://www.cnblogs.com/fenixG/p/13190746.html
Copyright © 2011-2022 走看看