(leetcode169)C++解决多数元素问题以及迭代器和map的使用

首先先对Map的使用做一个简单的总结。
Map是c++的一个标准容器,她提供了很好一对一的关系。
Map中构造方式为

   map<string , int >mapstring;         map<int ,string >mapint;
   map<sring, char>mapstring;         map< char ,string>mapchar;
   map<char ,int>mapchar;            map<int ,char >mapint;

Map中如何添加元素

   map<int ,string> maplive;  
   //方式一
   maplive.insert(pair<int,string>(102,"aclive"));
   //方式二
   maplive.insert(map<int,string>::value_type(321,"hai"));
   //方式三
   maplive[112]="April"; //map中最简单最常用的插入添加!

Map中元素的查找
要引入find()函数,注意要加头文件

#include<algorithm>

用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,
如下有其他博客的两个例子

   map<int ,string >::iterator l_it;; 
   l_it=maplive.find(112);
   if(l_it==maplive.end())
                cout<<"we do not find 112"<<endl;
   else cout<<"wo find 112"<<endl;
#include <map>
#include <string>
#include <iostream>
Using namespace std;
int main()
{
       Map<int, string> mapStudent;
       mapStudent.insert(pair<int, string>(1, “student_one”));
       mapStudent.insert(pair<int, string>(2, “student_two”));
       mapStudent.insert(pair<int, string>(3, “student_three”));
       map<int, string>::iterator iter;
       iter = mapStudent.find(1);
	   if(iter != mapStudent.end())
	   {
       	 	cout<<”Find, the value is ”<<iter->second<<endl;
	   }
 		else
	   {
       		cout<<”Do not Find”<<endl;
	   }
}

迭代器介绍
迭代器(Iterator)是一种检查容器内元素并遍历元素的数据类型。迭代器是指针的泛化,它允许程序员用相同的方式处理不同的数据结构(容器)。
所有容器有含有其各自的迭代器型别(iterator types),所以当你使用一般的容器迭代器时,并不需要含入专门的头文件。不过有几种特别的迭代器,例如逆向迭代器,被定义于 中

c++ 里面的map容器的迭代器里面 有个first 和 second

map<string, int> m;
m["one"] = 1;

map<string, int>::iterator p = m.begin();
p->first; // 这个是  string  值是 "one"
p->second; //这个是 int 值是 1

Map中元素的删除
如果要删除112

   map<int ,string >::iterator l_it;;
   l_it=maplive.find(112);
   if(l_it==maplive.end())
        cout<<"we do not find 112"<<endl;
   else  maplive.erase(l_it);  //delete 112;

注意find函数返回的是一个相应元素的迭代器,所以直接删除迭代器。

Map中的排序问题

Map中的元素是自动按key升序排序,所以不能对map用sort函数

一定要注意,map中的排序是自动的!题目中就因为排序问题产生了一个bug。
例子

  #include <map>
  #include <iostream>

  using namespace std;

 int main( )
 {
   map <int, int> m1;
   map <int, int>::iterator m1_Iter;

   m1.insert ( pair <int, int>  ( 1, 20 ) );
   m1.insert ( pair <int, int>  ( 4, 40 ) );
   m1.insert ( pair <int, int>  ( 3, 60 ) );
   m1.insert ( pair <int, int>  ( 2, 50 ) );
   m1.insert ( pair <int, int>  ( 6, 40 ) );
   m1.insert ( pair <int, int>  ( 7, 30 ) );

   cout << "The original map m1 is:"<<endl;
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout <<  m1_Iter->first<<" "<<m1_Iter->second<<endl;

}

Map中的基本操作

map的基本操作函数:
C++ Maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数

有了以上的知识,我们来进行一道题目

题目描述
给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在多数元素。
思路
本题题面中没有给出数据范围,但最简单的暴力方法(即枚举数组中的每个元素,再遍历一遍数组统计其出现次数,时间复杂度为 O(n^2)会超出时间限制。尝试使用map和iterator。

#include<map>
#include<algorithm>
#include<vector>
using namespace std;
class Solution {
public:
    int majorityElement(vector<int>& nums) {
        map<int,int> vec_nums; //定义一个map存放每个数和其出现的次数
        for(int i=0;i<nums.size();i++){
            map<int,int>::iterator iter; //对于nums里的每个数循环一次,每次循坏都要新创建一个迭代器
            //使用迭代器在vec_nums中找带与nums中元素匹配的key
            iter=vec_nums.find(nums[i]);
            //如果找到,在当前value上+1,即出现次数+1
            if(iter!=vec_nums.end()) iter->second=iter->second+1;
            //如果没有找到,则添加一对新的key,value
            if(iter==vec_nums.end()) vec_nums.insert(pair<int,int>(nums[i],1));
        }
        //创建新的迭代器,遍历生成的vec_nums,如果value值即出现次数大于n/2,就返回当前的key值
        //注意map中是自动按升序排列的,不能>=,例子,这样的话给定3,2,3会返回2
        map<int,int>::iterator iter;
        for(iter=vec_nums.begin();iter!=vec_nums.end();iter++){
            if(iter->second >nums.size()/2)
            return iter->first;
        }
        return 0;
    }  
};

题解方法,非常简洁
方法一

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        unordered_map<int, int> counts;
        int majority = 0, cnt = 0;
        for (int num: nums) {
            ++counts[num];
            if (counts[num] > cnt) {
                majority = num;
                cnt = counts[num];
            }
        }
        return majority;
    }
};

方法二
思路
如果将数组 nums 中的所有元素按照单调递增或单调递减的顺序排序,那么下标为中间的元素(下标从 0 开始)一定是众数。(非常巧妙)

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        return nums[nums.size() / 2];
    }
};

继续加油

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值