zoukankan      html  css  js  c++  java
  • [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)

      题目描述:

    /*
    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].
    */

    这道题给了我们一个数组,还有一个目标数target,让我们找到两个数字,使其和为target。首先想到的就是暴力搜索,遍历所有的组合项,获得结果,思路比较简单,代码如下:
    func func1(s []int, tag int) []int {
        for i := 0; i < len(s)-1; i++ {
            for j := i + 1; j < len(s); j++ {
                if s[i]+s[j] == tag {
                    return []int{i, j}
                }
            }
        }
        return nil
    }
    这个算法的时间复杂度是O(n^2)。虽然节省了空间,但是时间复杂度高。尝试用空间换时间,使用一个HashMap,建立数字和其坐标位置之间的映射,在遍历数组的时候,用target减去遍历到的数字,就是另一个需要的数字了,直接在HashMap中查找其是否存在即可,那么代码就可这样写:
    func func2(s []int, tag int) []int {
        hash := make(map[int]int)
        for i := 0; i < len(s); i++ {
            hash[s[i]] = i
        }
    
        for i := 0; i < len(s); i++ {
            temp := tag - s[i]
            if _, ok := hash[temp]; ok {
                if hash[temp] == i {
                    continue
                }
                return []int{i, hash[temp]}
            }
        }
        return nil
    }
    
    
    这个算法的时间复杂度是O(n)。再想想,代码好像可以更加简洁一些,把两个for循环合并成一个:
    func func3(s []int, tag int) []int {
        hash := make(map[int]int, len(s))
        for k, v := range s {
            if j, ok := hash[tag-v]; ok {
                return []int{j, k}
            }
            hash[v] = k
        }
        return nil
    }
    这样看起来就比较舒服ok了,完整的贴上来,运行一下试试吧。
    package main
    
    import (
        "fmt"
    )
    
    func main() {
        nums := []int{2, 7, 11, 15}
        target := 9
        s := func1(nums, target)
        //s := func2(nums, target)
        //s := func3(nums, target)
        fmt.Printf("nums[%d]+nums[%d]=%d+%d=%d
    ", s[0], s[1], nums[s[0]], nums[s[1]], target)
        fmt.Printf("return [%d,%d]", s[0], s[1])
    }
    
    //暴力破解 时间复杂度O(n^2)
    func func1(s []int, tag int) []int {
        for i := 0; i < len(s)-1; i++ {
            for j := i + 1; j < len(s); j++ {
                if s[i]+s[j] == tag {
                    return []int{i, j}
                }
            }
        }
        return nil
    }
    
    //用map辅助查找 时间复杂度O(n)
    func func2(s []int, tag int) []int {
        hash := make(map[int]int)
        for i := 0; i < len(s); i++ {
            hash[s[i]] = i
        }
    
        for i := 0; i < len(s); i++ {
            temp := tag - s[i]
            if _, ok := hash[temp]; ok {
                if hash[temp] == i {
                    continue
                }
                return []int{i, hash[temp]}
            }
        }
        return nil
    }
    //优化一下,把两个for循环合并成一个
    func func3(s []int, tag int) []int {
        hash := make(map[int]int, len(s))
        for k, v := range s {
            if j, ok := hash[tag-v]; ok {
                return []int{j, k}
            }
            hash[v] = k
        }
        return nil
    }


  • 相关阅读:
    sql 2012中获取表的信息,包含字段的描述
    C#如何创建泛型类T的实例
    C# 之 DataReader 和 DataSet 的区别
    C#进阶系列——WebApi 接口参数不再困惑:传参详解
    IIS事件查看器_WebServer事件查看器_帮助查看IIS-Web服务器事件执行日志
    SQL分页查询的几种方式
    freeRTOS中文实用教程3--中断管理之延迟中断处理
    freeRTOS中文实用教程2--队列
    freeRTOS中文实用教程1--任务
    UML和模式应用5:细化阶段(5)---系统顺序图
  • 原文地址:https://www.cnblogs.com/chase-wind/p/9547140.html
Copyright © 2011-2022 走看看