leecode第一百六十九题(求众数)
class Solution {
public:
void quick_sort(vector<int>& nums,int res,int res_end)
{
if(res_end-res<)//错过,不能小于2
return;
int begin=res;
int end=res_end;
bool flag=true; while(res!=res_end)
{
if(flag)
{
if(nums[res]>nums[res_end])
{
int temp=nums[res];
nums[res]=nums[res_end];
nums[res_end]=temp;
res++;
flag=!flag;
}
else
res_end--;
}
else
{
if(nums[res]>nums[res_end])//错过,不小心写成小于了
{
int temp=nums[res];
nums[res]=nums[res_end];
nums[res_end]=temp;
res_end--;
flag=!flag;
}
else
res++;
}
} quick_sort(nums,begin,res);
quick_sort(nums,res+,end); } int majorityElement(vector<int>& nums) {
int len=nums.size();
//int res;
//sort(nums.begin(),nums.end());//解法一:现成API
//quick_sort(nums,0,len-1);//解法二:手写了快排,但是最长的那个时间通不过,这是因为sort()函数里面不是单纯的快排,里面还有堆排序和插入排序等情况
//return nums[len/2]; int count = ;//解法三,网友提供,从第一个数开始count=1,遇到相同的就加1,遇到不同的就减1,减到0就重新换个数开始计数,总能找到最多的那个
int maj = nums[];
for (int i = ; i < len; i++) {
if (maj == nums[i])
count++;
else {
count--;
if (count == ) {
maj = nums[i + ];
}
}
}
return maj; //解法4:哈希表
}
};
分析:
题简单但是思想不简单啊,尤其是网友公认的这个,值的学习。
leecode第一百六十九题(求众数)的更多相关文章
- leecode第一百六十题(相交链表)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- leecode第一百五十五题(最小栈)
class MinStack { public: stack<int> cur_stack; stack<int> cur_min;//用来存储最小值的栈 int min_nu ...
- leecode第一百二十二题(买卖股票的最佳时机II)
class Solution { public: int maxProfit(vector<int>& prices) { int len=prices.size(); ) ; , ...
- leecode第一百二十四题(二叉树中的最大路径和)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- 第一百六十九节,jQuery,基础事件
jQuery,基础事件 学习要点: 1.绑定事件 2.简写事件 3.复合事件 JavaScript 有一个非常重要的功能,就是事件驱动.当页面完全加载后,用户通过鼠标 或键盘触发页面中绑定事件的元素即 ...
- 第三百六十九节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现搜索功能
第三百六十九节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现搜索功能 Django实现搜索功能 1.在Django配置搜索结果页的路由映 ...
- leecode第一百三十六题(只出现一次的数字)
class Solution { public: int singleNumber(vector<int>& nums) { int len=nums.size(); ; ;i&l ...
- python第一百六十九天,第十九周作业
FIRSTCRM 学员管理开发需求: 1.分讲师\学员\课程顾问角色, 2.学员可以属于多个班级,学员成绩按课程分别统计 3.每个班级至少包含一个或多个讲师 4.一个学员要有状态转化的过程 ,比如未报 ...
- 第二百六十九节,Tornado框架-Session登录判断
Tornado框架-Session登录判断 Session需要结合cookie来实现 Session的理解 1.用户登录系统时,服务器端获取系统当前时间,进行nd5加密,得到加密后的密串 2.将密串作 ...
随机推荐
- 《nginx - 基本操作/配置》
一:基本操作 - 开启 Nginx nginx -c nginx.conf - Nginx 的平滑重启 kill -HUP nginx主进程号(平滑重启) - 停止 Nginx * Kill -Q ...
- CentOS 6.8 安装TigerVNC 实现 Linux 远程桌面并安装火狐浏览器
CentOS 6.8 安装TigerVNC 实现 Linux 远程桌面并安装火狐浏览器 vnc客户端地址:https://files.cnblogs.com/files/MYSQLZOUQI/vnc- ...
- Rosserial实现Windows-ROS交互操作
安装 sudo apt-get install ros-indigo-rosserial-windows sudo apt-get install ros-indigo-rosserial-serve ...
- selenium操作浏览器
import org.openqa.selenium.WebDriver; import common.StartFireFox; public class TestBrowser { public ...
- js对象属性名驼峰式转下划线
一.题目示例: 思路: 1.匹配属性名字符串中的大写字母和数字 2.通过匹配后的lastIndex属性获取匹配到的大写字母和数字的位置 3.判断大写字母的位置是否为首位置以及lastIndex是否为0 ...
- cookie session 讲解
cookie: cookie的定义: cookie 是由web服务器保存在用户浏览器(客户端)上的小文本文件,它可以包含有关用户的信息,并且在每次请求时会携带保存的数据去访问服务器,所以cookie有 ...
- nc linux命令详解
NetCat,在网络工具中有“瑞士军刀”美誉,其有Windows和Linux的版本.因为它短小精悍(1.84版本也不过25k,旧版本或缩减版甚至更小).功能实用,被设计为一个简单.可靠的网络工具,可通 ...
- Zuul 网关路由
Zuul 网关路由 路由是微服务架构中不可或缺的一部分,例如:/api/user映射到user服务,/api/shop映射到shop服务. Zuul是一个基于JVM的路由和服务端的负载均衡器.Zuul ...
- Hdu1241 Oil Deposits (DFS)
Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...
- hdu5115 Dire Wolf
题目链接 区间DP $dp_{i,j}$为杀掉$i~j$内的狼的最小代价 枚举$i~j$中最后杀掉的狼,$dp_{i,j}=min\{ { {k\in{[i,j]}} | dp_{i,k-1}+dp_ ...