• Leetcode-1


    map
    #include <map>
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::map<std::string, int> maps;//键(first)值(second)对
        //数组方式插入
        maps["zhangsan"] = 20;
        maps["lisi"] = 22;
        maps["hanmeimei"] = 15;
        //maps["zhangsan"] = 200; //会把maps["zhangsan"] = 20;覆盖掉
        //insert 函数插入
        maps.insert(std::pair<std::string, int>("lucy", 25));
    
        //insert函数插入value_type数据
        maps.insert(std::map<std::string, int>::value_type("wangwu", 30));
    
        maps.insert(std::make_pair("lilei", 23));
    
        //正向遍历map输出
        std::cout << "forward output:" << std::endl;
        for (std::map<std::string, int>::iterator it = maps.begin(); it != maps.end(); it++)
        {
            std::cout << it->first << ", " << it->second << std::endl;
        }
    
        std::cout << std::endl;
        //反向遍历输出
        std::cout << "reverse output:" << std::endl;
        for (std::map<std::string, int>::reverse_iterator it = maps.rbegin(); it != maps.rend(); it++)
        {
            std::cout << it->first << ", " << it->second << std::endl;
        }
    
        std::cout << std::endl;
        //map长度
        int len = maps.size();
        std::cout << "len =" << len << std::endl;
    
        std::cout << std::endl;
        //查找
        std::map<std::string, int>::iterator it = maps.find("zhangsan");
        if (it == maps.end())
        {
            std::cout << "not find zhangsan..." << std::endl;
        }
        else {
            std::cout << "find:"<<it->first << ", " << it->second << std::endl;
    
            //删除zhangsan
            maps.erase(it);
        }
    
        //正向遍历map输出
        std::cout << "forward output:" << std::endl;
        for (std::map<std::string, int>::iterator it = maps.begin(); it != maps.end(); it++)
        {
            std::cout << it->first << ", " << it->second << std::endl;
        }
    
        int count1 = maps.count("zhangsan"); //如果有,返回1;否则,返回0。map中不存在相同元素,所以返回值只能是1或0。
        int count2 = maps.count("lisi");
        std::cout << count1 <<", "<< count2 << std::endl;
    
        //清空
        maps.erase(maps.begin(), maps.end());
        std::cout << maps.empty() << std::endl;
    }
    //不会自行对存储的键值对进行排序
    #include <unordered_map>
    #include <string>
    #include <iostream>
    
    int main()
    {
        //创建,初始化
        std::unordered_map<std::string, int> umaps{
            {"zhangsan",20},{"wangwu",30}
        };
        //拷贝
        std::unordered_map<std::string, int> umap2(umaps);
        umaps["lili"] = 25;
        //umaps["wangwu"] = 100;
        umaps.insert(std::pair<std::string,int>("lilei", 26));
        umaps.insert(std::make_pair("hanmeimei", 30));
        umaps.insert(std::unordered_map<std::string, int>::value_type("lisi", 18));
        umaps.emplace("haha", 33);//效率高
    
        int len = umaps.size();
        std::cout << "len=" << len << std::endl;
        std::cout << "forward:" << std::endl;
        for (std::unordered_map<std::string, int>::iterator it = umaps.begin(); it != umaps.end(); it++)
        {
            std::cout << it->first << "," << it->second << std::endl;
        }
    
        int count1 = umaps.count("zhangsan");
        std::cout << count1 << std::endl;
    
        std::unordered_map<std::string, int>::iterator it = umaps.find("zhangsan");
        if (it == umaps.end())
        {
            std::cout << "not find zhangsan" << std::endl;
        }
        else 
        {
            std::cout<<"find, " << it->first << "," << it->second << std::endl;
            umaps.erase(it);
        }
    
        std::cout << std::endl<< "forward:" << std::endl;
        for (std::unordered_map<std::string, int>::iterator it = umaps.begin(); it != umaps.end(); it++)
        {
            std::cout << it->first << "," << it->second << std::endl;
        }
        umaps.erase(umaps.begin(), umaps.end());
        std::cout << umaps.size() << std::endl;
    }

    #include <unordered_set>
    #include <iostream>
    
    int main()
    {
        std::unordered_set<int> set = {1,2,100,2,2}; //集合的特点就是其中没有任何重复的元素
        set.emplace(10);
        set.insert(20);
        
        for (std::unordered_set<int>::iterator it = set.begin(); it != set.end(); it++)
        {
            std::cout << (*it) << std::endl;
        }
    
        //查找
        std::unordered_set<int>::iterator iter = set.find(2);
        if (iter == set.end())
        {
            std::cout << "not find..." << std::endl;
        }
        else {
            std::cout << "find---" << (*iter) << std::endl;
        }
        int len = set.size();
        std::cout << "len=" << len << std::endl;
    
        int n = set.count(2);
        std::cout << n << std::endl << std::endl;
    
        set.erase(2);
        for (std::unordered_set<int>::iterator it = set.begin(); it != set.end(); it++)
        {
            std::cout << (*it) << std::endl;
        }
    
        std::cout << "-----"<<std::endl;
        set.clear();
        for (std::unordered_set<int>::iterator it = set.begin(); it != set.end(); it++)
        {
            std::cout << (*it) << std::endl;
        }
    }
     
    /*************************************************************************************************************************
    Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
    You may assume that each input would have exactly one solution, and you may not use the same element twice.
    You can return the answer in any order.
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/two-sum
    ---------------------------------------------------翻译----------------------------------------------------
    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
    你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
    
    示例:
    给定 nums = [2, 7, 11, 15], target = 9
    因为 nums[0] + nums[1] = 2 + 7 = 9
    所以返回 [0, 1]
    ****************************************************************************************************************************/
    #include <iostream>
    #include <vector>
    #include <map>
    #include <unordered_map>
    #include <string>
    using namespace std;
    
    class Solution {
    public:
        vector<int> twoSum(vector<int>& nums, int target)
        {
            for (int i = 0; i < nums.size(); i++)
            {
                for (int j = i + 1; j < nums.size(); j++)
                {
                    if ( (nums[i] + nums[j]) == target)
                    {
                        return{ i,j };
                    }
                }
            }
            return{};
        }
    
    private:
        vector<int>testNums;
    };
    
    class SolutionB {
    public:
        vector<int> twoSum(vector<int>& nums, int target)
        {
            unordered_map<int, int> uMap;
            for (int i = 0; i < nums.size(); i++)
            {
                unordered_map<int, int>::iterator iter = uMap.find(target - nums[i]);
                if (iter != uMap.end())
                {
                    return{ iter->second, i };
                }
                uMap.emplace(nums[i], i);
                //uMap[nums[i]] = i; //把值存到hash下标
            }
            return{};
        }
    
    };
    
    int main()
    {
        vector<int> num = { 2,7,9,11 };
        int target = 11;
    
        Solution A;
        vector<int> out;
        out = A.twoSum(num, target);
        for (vector<int>::iterator it = out.begin(); it != out.end(); it++)
        {
            cout << (*it) << endl;
        }
    
        SolutionB B;
        out = B.twoSum(num, target);
        cout << "Solution B:" << endl;
        for (vector<int>::iterator it = out.begin(); it != out.end(); it++)
        {
            cout << (*it) << endl;
        }
        return 0;
    }
  • 相关阅读:
    关于 Android 进程保活,你所需要知道的一切【转】
    android 按返回键最小化(后台运行)
    android notification完全解析【转】
    使用WakeLock使Android应用程序保持后台唤醒
    [Linux]Vim基本操作
    [STL]map的使用
    [python]使用python进行LINUX系统操作
    python challenge 2:迭代与列表
    python challenge 1、3:字符串处理
    python challenge 0:操作符与内建函数
  • 原文地址:https://www.cnblogs.com/crazybird123/p/14513579.html
Copyright © 2020-2023  润新知