zoukankan      html  css  js  c++  java
  • [LeetCode]1. Two Sum

    原题链接:https://leetcode.com/problems/two-sum/description/

    意思是给定输入数组和目标数,要求输出数组中两个相加刚好为目标数的元素下标

    我的实现(Brute Force):

     1 class Solution {
     2 public:
     3     vector<int> twoSum(vector<int>& nums, int target) {
     4         vector<int> answer;
     5         for (int i = 0; i < nums.size(); i++) {
     6             for (int j = i; j < nums.size(); j++) {
     7                 if (nums[i] + nums[j] == target && i != j) {
     8                     answer.push_back(i);
     9                     answer.push_back(j);
    10                     return answer;
    11                 }
    12             }
    13         }
    14         return answer;
    15     }
    16 };

    时间复杂度:O(n2​​).

    空间复杂度:O(1).

    怎么样提高算法效率呢?

    先将数列每个元素存到哈希表,然后遍历数列看(目标数-当前元素)的值在hash table中是否存在即可,注意判断下标不能重复。

    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 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");
    }

    时间复杂度:O(n).

    空间复杂度:O(n).

    为了进一步减小需要的存储空间,做法是不需要提前将数列元素保存在哈希表中,可以边遍历数组边加入hash table。这样如果存在数组之前元素与当前元素之和为目标数我们就能输出结果,代码:

    public int[] twoSum(int[] nums, int target) {
        Map<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[] { map.get(complement), i };
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    时间复杂度:O(n).

    空间复杂度:O(n).(PS:需要存储空间最多为存储n个元素的hash table)

    总结:Hash Table,空间换时间。

  • 相关阅读:
    java基础:6.3 封装类、Number类、格式化输出、String
    java 快捷键记录
    java基础:6.2 Object、final、abstract、内部类
    如何解决.so 文件下载到mac 失败的问题
    mac 将.so文件上传到SVN上
    限制输入内容的需求
    Android Intent调用 Uri的使用几种格式
    onItemClick的参数
    Android常见的几种RuntimeException
    android:inputType参数类型说明
  • 原文地址:https://www.cnblogs.com/qianzixuan1996/p/8286761.html
Copyright © 2011-2022 走看看