• 1. Two Sum[E]两数之和


    题目


    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]

    思路


    思路1:双重循环

    这种是最容易想到的思路,比较暴躁,复杂度(O(N^2))

    思路2:哈希表

    题目对数a、b求和,但是返回的是等于target时a、b的下标,于是想到将数组下标与对应值作一个映射表。C++使用unordered_map关联容器可以实现键值与真值的映射。python中使用字典来实现类似功能。

    Tips


    unordered_map

    1. 原型
    template <class T,             //键值类型
              class T,                       // 映射类型
              class hash = hash<key>,      //哈希函数对象类型
              class Pred = equal_to <key>,       //相等比较函数对象类型
              class Alloc = allocator < pair<cosnt key, T> >   //alloctor类
              >
    
    2. 特性
    • 关联性:通过key值检索value,而不是通过绝对地址(和顺序容器不同)
    • 无序性:使用hash表存储,内部无序
    • Map:每个值对应一个key值
    • key唯一性:不存在两个元素的key一样
    • 动态内存管理:使用动态内存管理模型来动态管理所需要的内存空间。
    3. 常用函数
    • count
      原型
      size_type count (const key_type& k) const;
      说明
      使用给定的Key值计算元素。搜素容器中Key值作为输入参数k的元素,并返回元素的数量。由于unorder_map容器不允许存在重复的Key值,这说明如果容器中存在具有该Key值的元素,则该函数返回1,否则返回0。
    4. 小结

    unordered_map的数据以pair<const Key, T>保存,first是键值(key value),second是映射值(the mapped value)。赋值语句m[key value] = the mapped value

    C++


    • 思路1
     vector<int> twoSum(vector<int>& nums, int target) {
            vector<int> results;
            
            for(int i=0;i<nums.size();i++)
            {
                for(int j=i+1;j<nums.size();j++)
                {
                    if(nums[i]+nums[j]==target)
                    {
                        results.push_back(i);
                        results.push_back(j);
                        
                        return results;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }
    
    • 思路2
    vector<int> twoSum(vector<int>& nums, int target) {
            unordered_map<int,int> m;
            vector<int> results;
            
            //数组中的值作为map的键值,其下标作为对应的映射值
            for(int i=0;i<nums.size();i++)  
            {
                m[nums[i]] =i;
            }
            
            for(int i = 0;i<nums.size();i++)
            {
                int t = target - nums[i];
                if(m.count(t) && m[t] != i) // 不能使用同样的数两次
                {
                    results.push_back(i);
                    results.push_back(m[t]);
                    break;
                }
            }
            return results;
        }
    

    Python


    • 思路2
     def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
             #建立字典
            table ={nums[i] : i for i in range(len(nums))}
            
            results = []
            for i in range(len(nums)):
                t = target - nums[i]
                if table.get(t) is not None and (table.get(t) != i):
                    results = [i, table.get(t)]
                    break;
                    
            return results
            
    
  • 相关阅读:
    python之模块与包
    python之模块4
    python之模块3
    python之模块2
    Day10:Linux基础:搭建samba服务
    Day9:Linux基础:程序管理
    Day8: Linux基础片:网络配置
    番外篇:硬盘分区、创建文件系统
    Day7: Linux基础片:系统监控
    Day6: Linux基础片:文件压缩、Vim用法
  • 原文地址:https://www.cnblogs.com/Jessey-Ge/p/10943944.html
Copyright © 2020-2023  润新知