剑桥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)如果每次 ...
随机推荐
- Git一分钟系列--快速安装git客户端
在项目开发过程中,几乎所有公司都会用到版本控制工具来管理自己的项目资源文件,比如Git,SVN. 什么是svn? 版本控制软件,通过svn来实现版本控制首先需要搭建一个服务器,在服务器上创建仓库保存项 ...
- Linux命令应用大词典-第33章 X Window
33.1 xhost:X服务器的访问控制程序 33.2 xinit:X Window系统初始化 33.3 Xlsclients:在显示器中列出正在运行的客户端应用程序 33.4 xlsfonts:显示 ...
- 82. Single Number [easy]
Description Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Example Given [1, ...
- Elasticsearch 相同内容文档,不同score(评分)的奇怪问题
原文:http://stackoverflow.com/questions/14580752/elasticsearch-gives-different-scores-for-same-documen ...
- 2.hbase原理(未完待续)
hbase简介相关概念hmsterhregionserver表regionhstorememstorestorefilehfileblockcacheWALminorcompactmajorcompa ...
- 官方文档 恢复备份指南三 Recovery Manager Architecture
本节讨论以下问题: About the RMAN Environment 关于RMAN环境 RMAN Command-Line Client ...
- Sum of Consecutive Prime Numbers(素数打表+尺取)
Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...
- PokeCats开发者日志(十三)
现在是PokeCats游戏开发的第六十二天的晚上,把软著权登记证书的截图加上,又重新提交审核了一遍,但愿能过吧...
- AppScan工作原理&操作教程
一.AppScan的工作原理 对一个综合性的大型网站来说,可能存在成千上万的页面.以登录界面为例,至少要输入用户名和密码,即该页面存在两个字段,当提交了用户名和密码等登录信息,网站需要检查是否正确,这 ...
- G# GUID
GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多可 ...