栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

2021-10-14

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-10-14

文章目录
  • 第十五天
      • 1.hash_map

第十五天 1.hash_map

前面说到了hash_map就类似于python中的dict词典,一个括号里的值,对应外面的一个值。
下面是一些基本用法

#include  // Header file to include map in the code
#include
#include 
using namespace std;

int main() 
{
	map mp; //creating a map
	map::iterator it; //object for forloop
	  
	mp.insert(pair(1, "Python"));  //inserting values
	mp.insert(pair(2, "C")); 
	mp.insert(pair(3, "Java"));
	mp.insert(pair(4, "PHP"));
	mp.insert(pair(5, "C++"));
	
	mp[6] = "Pearl"; //other way to add element in map
	mp[7] = "Javascript";
	mp[8] = "R Programming";
	mp[9] = "Scala";
	mp[10] = "Rust";
	  
	std::cout<first << " " << it->second << endl; //Printing the values after inserting
	}
	
	mp.erase(10); // removing 10th element
	mp.erase(9);  // removing 9th element
	std::cout<first << " " << it->second << endl;
	}
	 	
	mp.erase(mp.find(6), mp.end()); //removing multiple elements from the map
	  
	std::cout<first << " " << it->second << endl;
	}
	
	std::cout<first << "->" << it->second ; 
		 
    it = mp.find('SQL'); 
      
    if(it == mp.end()) 
        cout <first << "->" << it->second ; 

	std::cout<< endl;
}

Output

Map :
1 Python
2 C
3 Java
4 PHP
5 C++
6 Pearl
7 Javascript
8 R Programming
9 Scala
10 Rust

After removing 2 elements from the map
1 Python
2 C
3 Java
4 PHP
5 C++
6 Pearl
7 Javascript
8 R Programming

After removing multiple elements from the map
1 Python
2 C
3 Java
4 PHP
5 C++

Size of the map5

Key-value pair present : 2->C

Key-value pair not present in a map

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。

做起来很简单的,一遍出了,xs
第一种

class Solution {
public:
    bool isAnagram(string s, string t) {
        sort(s.begin(), s.end());
        sort(t.begin(), t.end());
        if (s == t)
            return true;
        return false;
    }
};

第二种,新学的hash_map

也是比较简单

class Solution {
public:
    bool isAnagram(string s, string t) {
        if (s.size() != t.size())
            return false;
        vector dig(26);
        for (auto ch:s){
            dig[ch - 'a']++;
        }
        for (auto ch:t){
            dig[ch - 'a']--;
            if (dig[ch - 'a'] < 0)
                return false;
        }
        return true;
    }
};

给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。

字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母都恰好只用一次。

示例 1:
输入: strs = [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]
输出: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]

示例 2:
输入: strs = [""]
输出: [[""]]

示例 3:
输入: strs = [“a”]
输出: [[“a”]]

class Solution {
public:
    vector> groupAnagrams(vector& strs) {
        unordered_map> mp;
        for (string& str: strs) {
            string key = str;
            sort(key.begin(), key.end());
            mp[key].emplace_back(str);
        }
        vector> ans;
        for (auto it = mp.begin(); it != mp.end(); ++it) {
            ans.emplace_back(it->second);
        }
        return ans;
    }
};

两数之和

class Solution {
public:
    vector twoSum(vector& nums, int target) {
        unordered_map hashtable;
        for (int i = 0; i < nums.size(); ++i) {
            auto it = hashtable.find(target - nums[i]);
            if (it != hashtable.end()) {
                return {it->second, i};
            }
            hashtable[nums[i]] = i;
        }
        return {};
    }
};
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324283.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号