剑桥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)如果每次 ...
随机推荐
- Selenium(Python)PageObject页面对象
使用PageObject页面对象的好处是, 当页面元素的位置发生改变时, 只需要去修改Xpath或者ID, 而不用去修改测试用例本身: 本次的思路是: 1.常用方法类 2.页面对象类 3.测试用例类 ...
- div布局方案整理
实际项目开发过程中遇到页面 DIV 左右布局的需求:左侧 DIV 固定宽度,右侧 DIV 自适应宽度,填充满剩余页面,由此引申出本文的几种解决方案 1 左侧 DIV 设置 float 属性为 left ...
- 如何往eclipse中导入maven项目
现在公司中大部分项目可能都是使用maven来构建,假如现在摆在你面前有一个maven的项目,如果你要学习它,如何将它导入到像eclipse这样的集成开发工具中呢,以项目public_class_1为例 ...
- 【SpringCloud】 第十篇: 高可用的服务注册中心
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- leetcode-单词探索
单词搜索 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母 ...
- 单词 (Play on Words UVA - 10129 )
题目描述: 原题:https://vjudge.net/problem/UVA-10129 题目思路: 1.明显是判断欧拉路径 2.欧拉路径的两个条件 a.图连通 b.至多为两个奇点,且一个为起点一个 ...
- (转)CGMA - Organic World Building in UE4: week 6
原文:丢失,这篇是艺术家博客上发现的,小道整理笔记中,临时放于效果案例目录. In this week we focused on creating the grass and flora t ...
- 正式放弃Edge,重新拥抱Chrome
从Edge还叫斯巴达的时候我就开始用了,本来对浏览器的要求也没多高,能够打开多个选项卡,稳定,支持最新的规范就好了. 但是Edge真的是越来越让我失望了,卡死问题越来越多,崩溃越来越频繁,我也快奔溃了 ...
- 【未完】训练赛20190304:KMP+树状数组+线段树+优先队列
头炸了啊,只做出L题,前两天刚看的Shawn zhou的博客学习的,幸亏看了啊,否则就爆零了,发现题目都是经典题,线段树,KMP,我都没看过,最近又在复习考研,真后悔大一大二没好好学习啊,得抽时间好好 ...
- BluetoothServerSocket详解
一. BluetoorhServerSocket简介 1. 继承关系 public final class BluetoothServerSocket extends Object implement ...