zoukankan      html  css  js  c++  java
  • leetcode-1-两数之和

    题目:

    1、暴力解法

     1 package com.example.demo;
     2 
     3 public class TestLeetCode {
     4     public static void main(String[] args) {
     5         TestLeetCode t = new TestLeetCode();
     6         int[] i = {2, 7, 11, 15};
     7         int target = 17;
     8         int[] o = t.twoSum(i, target);
     9 
    10         for (int i1 : o) {
    11             System.out.println(i1);
    12         }
    13 
    14     }
    15 
    16     /**
    17      * 暴力解法就是将所有的数据都算一遍,和目标值比较,同泽返回i,j索引,否则返回null即可(遍历冒泡)   题目要求返回的是索引数据,并不是值数组
    18      * @param nums
    19      * @param target
    20      * @return
    21      */
    22     public int[] twoSum(int[] nums, int target) {
    23 
    24         for (int i = 0; i < nums.length; i++) {
    25             //j = i + 1 ,不和自身相加
    26             for (int j = i + 1; j < nums.length; j++) {
    27                 if ((nums[j] + nums[i]) == target) {
    28                     return new int[]{i, j};
    29                 }
    30             }
    31         }
    32         return null;
    33     }
    34 }

    2、使用map数据结构

    package com.example.demo;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class TestLeetCode {
        public static void main(String[] args) {
            TestLeetCode t = new TestLeetCode();
            int[] i = {2, 7, 11, 15};
            int target = 17;
            int[] o = t.twoSum(i, target);
    
            for (int i1 : o) {
                System.out.println(i1);
            }
    
        }
    
        /**
         * 利用hash数据结构,先将数组中的value作map的key,index作map的value,保存起来,然后遍历数组,
         * 如果存在一个结果等于target-nums[i]时,此时这个值和nums[i]就是目标值,再通过map.get()来获取到对应的索引
         *
         * @param nums
         * @param target
         * @return
         */
        public int[] twoSum(int[] nums, int target) {
    
            Map<Integer, Integer> map = new HashMap<>();
    
            for (int i = 0; i < nums.length; i++) {
                map.put(nums[i], i);
            }
    
            for (int i = 0; i < nums.length; i++) {
                int index_value = target - nums[i];
                if (map.containsKey(index_value) && nums[i] != index_value) {
                    return new int[]{i, map.get(index_value)};
                }
            }
            return null;
        }
    }

    修改:

    package com.example.demo;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class TestLeetCode {
        public static void main(String[] args) {
            TestLeetCode t = new TestLeetCode();
            int[] i = {2, 7, 11, 15};
            int target = 17;
            int[] o = t.twoSum(i, target);
    
            for (int i1 : o) {
                System.out.println(i1);
            }
    
        }
    
        /**
         * 利用hash数据结构,一次遍历,在遍历的时候判断是否存在,不存在则将当前的值放到map里,以便之后调用
         *
         * @param nums
         * @param target
         * @return
         */
        public int[] twoSum(int[] nums, int target) {
    
            Map<Integer, Integer> map = new HashMap<>();
    
            for (int i = 0; i < nums.length; i++) {
                int index_value = target - nums[i];
                if (map.containsKey(index_value) && nums[i] != index_value) {
                    //这块换了位置,是因为map里边放的都是比当前索引小的值
                    return new int[]{map.get(index_value), i};
                }
                map.put(nums[i], i);
            }
            return null;
        }
    }

    参考:https://leetcode-cn.com/problems/two-sum/solution/liang-shu-zhi-he-by-leetcode-2/

  • 相关阅读:
    初学Google Code,Subversion和TortoiseSVN
    成长,真有你想象的那样迫切?
    Java中十个常见的违规编码
    Eclipse打包工具Fatjar
    “旁观者效应”是如何毁掉我们的代码的
    java设计模式—分类
    java中的关键字static(静态变量)和final定义常量
    JAVA UDP打洞必备知识点NAT
    [Struts]在jsp里处理比较复杂的内容?
    反向链接referrer的原理
  • 原文地址:https://www.cnblogs.com/nxzblogs/p/11122037.html
Copyright © 2011-2022 走看看