剑桥offer(21~30)
21.题目描述
22.题目描述
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
map<int,int> m;
map<int,int>::iterator it;
for(int i=;i<numbers.size();i++)
m[numbers[i]]++;
for(it=m.begin();it!=m.end();it++)
{
if(it->second>numbers.size()/)
return it->first;
}
return ;
}
};
23.题目描述
class Solution {
public:
vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
vector<int>r;
if(input.empty()||k>input.size()) return r;
sort(input.begin(),input.end());
r.assign(input.begin(),input.begin()+k);
return r;
}
};
24.题目描述
class Solution {
public:
int FindGreatestSumOfSubArray(vector<int> array) {
if(array.size() == )return ;
int sum = array[];
int tmp = array[];
for (int i=;i<array.size();i++){
tmp = (tmp>)?tmp+array[i]:array[i];
sum = tmp>sum?tmp:sum;
}
return sum;
}
};
25.题目描述
class Solution {
public:
int NumberOf1Between1AndN_Solution(int n)
{
int count = ;
for(int i=; i<=n; i++){
int temp = i;
//如果temp的任意位为1则count++
while(temp){
if(temp% == ){
count++;
}
temp /= ;
}
}
return count;
}
};
26.题目描述
class Solution {
public:
static bool cmp(int a,int b){
string A="";
string B="";
A+=to_string(a);
A+=to_string(b);
B+=to_string(b);
B+=to_string(a);
return A<B;
}
string PrintMinNumber(vector<int> numbers) {
string answer="";
sort(numbers.begin(),numbers.end(),cmp);
for(int i=;i<numbers.size();i++){
answer+=to_string(numbers[i]);
}
return answer;
}
};
27.题目描述
class Solution {
public:
int GetUglyNumber_Solution(int index) {
if (index <= )return ;
if (index == ){
return ;
}
int m2=,m3=,m5=;
vector<int>a(index);
a[]=;
for(int i=;i<index;i++){
a[i]=min(a[m2]*,min(a[m3]*,a[m5]*));
if(a[i]==a[m2]*)m2++;
if(a[i]==a[m3]*)m3++;
if(a[i]==a[m5]*)m5++;
}
return a[index-];
}
};
28.题目描述
class Solution {
public:
int FirstNotRepeatingChar(string str) {
char s[] = {};
int i=;
while(str[i]!='\0'){
s[str[i]]++;
i++;
}
i=;
while(str[i]!='\0'){
if(==s[str[i]]){
return i;
}
i++;
}
return -;
}
};
29.题目描述
30.题目描述
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2) { if (!pHead1 || !pHead2) return NULL; stack<ListNode *>p1;
stack<ListNode *>p2;
ListNode* node = NULL; while (pHead1){
p1.push(pHead1);
pHead1 = pHead1->next;
}
while (pHead2){
p2.push(pHead2);
pHead2 = pHead2->next;
}
while ((p1.size()> && p2.size()>) && p1.top() == p2.top()){ node = p1.top();
p1.pop();
p2.pop(); }
return node;
} };
剑桥offer(21~30)的更多相关文章
- 2017年1月4日 星期三 --出埃及记 Exodus 21:30
2017年1月4日 星期三 --出埃及记 Exodus 21:30 However, if payment is demanded of him, he may redeem his life by ...
- 2018-10-13 21:30:51 conversion of number systems
2018-10-13 21:30:51 c language 二进制.八进制和十六进制: 1) 整数部分 十进制整数转换为 N 进制整数采用“除 N 取余,逆序排列”法. 十进制数字 36926 转 ...
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面 Offer 21 这题的解法其实是考察快慢指针和头尾指针. package com.walegarrett.offer; /** * @Aut ...
- 【剑指Offer】俯视50题之21 - 30题
面试题21包括min函数的栈 面试题22栈的压入.弹出序列 面试题23从上往下打印二叉树 面试题24二叉搜索树的后序遍历序列 面试题25二叉树中和为某一值的路径 面试题26复杂链表的复制 ...
- 在线体验 Windows 11「GitHub 热点速览 v.21.30」
作者:HelloGitHub-小鱼干 有什么比无需安装系统,检测硬件兼容度,只要打开一个浏览器,输入某个神秘的地址回车,即可体验 Windows 11 更棒的呢?windows11 就是这么一个小工具 ...
- 【Java】 剑指offer(21) 调整数组顺序使奇数位于偶数前面
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇 ...
- 剑桥offer(31~40)
31.题目描述 统计一个数字在排序数组中出现的次数. 思路:找到最低和最高,相减 class Solution { public: int GetNumberOfK(vector<int> ...
- 剑桥offer系列(1~10)
1.题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路:从左下开始, ...
- 剑指offer 面试30题
面试30题: 题目:包含min函数的栈 题:定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数.在该栈中,调用min.push.pop的时间复杂度都是O(1) 解题思路:1)如果每次 ...
随机推荐
- jmeter获取cookies
使用场景:登录后,后续的请求操作需获取到JSESSIONID才可进行 1.将jmeter的bin目录下的jmeter.properties文件中的CookieManager.save.cookies= ...
- Linux命令应用大词典-第14章 显示登录用户
14.1 w:详细查询已登录当前计算机的用户 14.2 who:显示已登录当前计算机用户的简单信息 14.3 whoami:显示与当前的有效ID相关联的用户名 14.4 logname:显示当前用户的 ...
- 【转】一款已上市MMO手游地图同步方案总结
转自游戏开发主席 1. 客户端地图格子的相关知识 在2.5D的MMO游戏里,角色是通过3D的方式渲染,2D的地图是通过2D的方式显示,所以在客户端一般会有三个坐标系: a) 3D坐标系:所有需要3D渲 ...
- (转)CGMA - Organic World Building in UE4: week 6
原文:丢失,这篇是艺术家博客上发现的,小道整理笔记中,临时放于效果案例目录. In this week we focused on creating the grass and flora t ...
- nodejs promise深度解析
Promise本质上是一个容器,内部有一个执行函数,当promise对象New出来的时候,内部包裹的函数立即执行. V8引擎会将resolve和projeccted两个函数传递进来,resolved含 ...
- centos配置iptables
第一次配置前消除默认的规则 #这个一定要先做,不然清空后可能会悲剧 iptables -P INPUT ACCEPT #清空默认所有规则 iptables -F #清空自定义的所有规则 iptable ...
- Linux下实现Rsync目录同步备份
需求:对于开发机器做目录的数据备份 测试机IP:192.168.1.100 WEB目录:/bckup/ 下面我将用一台机器来备份上面测试机 /bckup下的所有数据,并实现时时同步 备份机器IP: ...
- 衡量经济活动的价值:国内生产总值(GDP, Gross Domestic Product)
定义 GDP是在给定的时期内,经济生产的所有最终产品和服务的市场价值. 由于每一件产品或者服务的交易都会涉及到一个买者和一个卖着,买者支出的每一元钱必然成为卖者收入的每一元钱,因此,GDP既可以看成是 ...
- C语言实验——时间间隔
Description 从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示. 如:3点5分25秒应表示为--03:05:25.假设两个时间在同一天内,时 ...
- 20162328蔡文琛week03
学号 2006-2007-2 <程序设计与数据结构>第X周学习总结 教材学习内容总结 在第三章,我学习到了更多有关于java.util包的知识.了解了多个引用变量可以指向同一个对象.而且J ...