剑桥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录制Web脚本
设置Firefox浏览器代理, 点击右上角的菜单: 点击选项: 点击高级: 点击设置: 点击手动配置代理, 输入本地的IP地址和端口号8888,与JMeter代理服务器的端口号保持一致: 好了,浏览器 ...
- Unity Lighting - Choosing a Lighting Technique 选择照明技术(一)
Choosing a Lighting Technique 选择照明技术 https://unity3d.com/cn/learn/tutorials/topics/graphics/choosi ...
- 搭建redis集群的过程中遇到的问题
1.GCC没有安装或版本不对 报错信息如下 CC adlist.o /bin/sh: cc: command not found make[1]: *** [adlist.o] Error 127 m ...
- lintcode204 单例
单例 单例 是最为最常见的设计模式之一.对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例.例如,对于 class Mouse (不是动物的mouse哦),我们应 ...
- Python数据分析实战-Boston Public Schools GEO数据分析-Part1
项目目标: Boston Public Schools Geo数据是来自于Boston地区的公共学校的数据,具体描述了学校的坐标,名字,类型等.基于此数据,我们可以学习一些基本的Python数据分析的 ...
- 自测之Lesson9:时钟与信号
题目一:编写一个获取当前时间的程序,并将其以“year-mon-day time”的形式输出. 程序代码: #include <stdio.h> #include <time.h&g ...
- VS2013运行C++报错:This function or variable may be unsafe. Consider using fopen_s instead.
在vs2013中运行时发生的关于方法调用的不安全错误. 1.更换方法,但是有些方法更改后参数不变,所以可能比较麻烦. 2.添加一条预处理器定义: 点击项目——>属性——>c/c++——&g ...
- http-bio-8080"-exec-6"(转)
现象如下: Tomcat7启动后,后台抛出如下异常,前台一直无法登陆Exception in thread ""http-bio-8080"-exec-6" j ...
- 第二次作业 编程题 PAT 1001A+B Format
Github的object-oriented仓库:1001.A+BFormat(20) 1.解题的思路过程 在之前学习C语言时曾经碰到过类似的将数字转换成字符输出的情况,这道题目要求输出的数字每三个间 ...
- lintcode-181-将整数A转换为B
181-将整数A转换为B 如果要将整数A转换为B,需要改变多少个bit位? 注意事项 Both n and m are 32-bit integers. 样例 如把31转换为14,需要改变2个bit位 ...