ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2021-10-14

2021-10-14 21:59:18  阅读:148  来源: 互联网

标签:10 return 14 map second mp pair string 2021


文章目录

第十五天

1.hash_map

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

#include <map> // Header file to include map in the code
#include<iostream>
#include <string>
using namespace std;

int main() 
{
	map<int, string> mp; //creating a map
	map<int, string>::iterator it; //object for forloop
	  
	mp.insert(pair<int, string>(1, "Python"));  //inserting values
	mp.insert(pair<int, string>(2, "C")); 
	mp.insert(pair<int, string>(3, "Java"));
	mp.insert(pair<int, string>(4, "PHP"));
	mp.insert(pair<int, string>(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<<endl<<"Map : "<<endl;
	for (it = mp.begin(); it != mp.end(); it++) {
	  std::cout<< it->first << " " << it->second << endl; //Printing the values after inserting
	}
	
	mp.erase(10); // removing 10th element
	mp.erase(9);  // removing 9th element
	std::cout<<endl<<"After removing 2 elements from the map"<<endl;
	
	for (it = mp.begin(); it != mp.end(); it++) {
	  std::cout<< it->first << " " << it->second << endl;
	}
	 	
	mp.erase(mp.find(6), mp.end()); //removing multiple elements from the map
	  
	std::cout<<endl<<"After removing multiple elements from the map"<<endl;
	
	for (it = mp.begin(); it != mp.end(); it++) {
	  std::cout<< it->first << " " << it->second << endl;
	}
	
	std::cout<<endl<<"Size of the map"<<mp.size(); //this will return the number of elements present in the map
	
	it = mp.find('c'); 
      
    if(it == mp.end()) 
        cout <<endl<< "Key-value pair not present in a map" ; 
    else
        cout <<endl<< "\nKey-value pair present : " 
          << it->first << "->" << it->second ; 
		 
    it = mp.find('SQL'); 
      
    if(it == mp.end()) 
        cout <<endl<< "\nKey-value pair not present in map" ; 
    else
        cout <<endl<< "\nKey-value pair present : " 
          << it->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<int> 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<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string, vector<string>> mp;
        for (string& str: strs) {
            string key = str;
            sort(key.begin(), key.end());
            mp[key].emplace_back(str);
        }
        vector<vector<string>> ans;
        for (auto it = mp.begin(); it != mp.end(); ++it) {
            ans.emplace_back(it->second);
        }
        return ans;
    }
};

两数之和

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> 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 {};
    }
};

标签:10,return,14,map,second,mp,pair,string,2021
来源: https://blog.csdn.net/m0_51338712/article/details/120772570

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有