剑桥offer系列(1~10)
1.题目描述
class Solution {
public:
bool Find(vector<vector<int> > array,int target) {
if(array.size()==)return false;
int r=array.size();
int c=array[].size();
int i=r-,j=;
while(i<r&&i>-&&j<c&&j>-){
if(array[i][j] == target)return true;
if(array[i][j]>target){
i--;
}else{
j++;
}
}
return false;
}
};
2.题目描述
//length为牛客系统规定字符串输出的最大长度,固定为一个常数
class Solution {
public:
void replaceSpace(char *str,int length) {
int i = ;
int j = ;
int nSpace = ;
char *pStr = NULL;
pStr = (char*)malloc(sizeof(char)*length * );
for (i = , j = ; i<length; i++, j++)
{
if (' ' == str[i])
{
pStr[j] = '\%';
pStr[++j] = '';
pStr[++j] = '';
}
else
{
pStr[j] = str[i];
} }
for( i=;i<j;i++ )
{
str[i] = pStr[i];
} free(pStr);
pStr = NULL;
}
};
3.题目描述
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(struct ListNode* head) { struct ListNode* tmp = head;
vector<int>v;
while(tmp != NULL){
v.push_back(tmp->val);
tmp = tmp->next;
}
reverse(v.begin(),v.end());
return v;
}
};
4.题目描述
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
struct TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> in) {
int in_size = in.size(); if (in_size == )
return NULL;
vector<int> pre_left, pre_right, in_left, in_right;
int val = pre[];
TreeNode* node = new TreeNode(val);//root node is the first element in pre
int p = ;
for (; p < in_size; ++p){
if (in[p] == val) //Find the root position in in
break;
}
for (int i = ; i < in_size; ++i){
if (i < p){
in_left.push_back(in[i]);//Construct the left pre and in
pre_left.push_back(pre[i + ]);
}
else if (i > p){
in_right.push_back(in[i]);//Construct the right pre and in
pre_right.push_back(pre[i]);
}
}
node->left = reConstructBinaryTree(pre_left, in_left);
node->right = reConstructBinaryTree(pre_right, in_right);
return node;
}
};
5.题目描述
class Solution
{
public:
void push(int node) {
stack1.push(node);
} int pop() { if(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.top());
stack1.pop();
}
}
int node = stack2.top();
stack2.pop();
return node;
} private:
stack<int> stack1;
stack<int> stack2;
};
6.题目描述
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
class Solution {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
int i=;
int len = rotateArray.size();
if(len==)return ;
if(len==)return rotateArray[];
while(i<len-){
if(rotateArray[i]<=rotateArray[i+]){
i++;
}else{
break;
}
}
return rotateArray[i+];
}
};
7.题目描述
class Solution {
public:
int Fibonacci(int n) {
if(n == )
return ;
if(n == )
return ;
int numfn1 = , numfn2 = ;
int currentnum;
for(int i=; i<=n; ++i) {
currentnum = numfn1+numfn2;
numfn1 = numfn2;
numfn2 = currentnum;
}
return currentnum;
}
};
8.题目描述
class Solution {
public:
int jumpFloor(int number) {
if (number == )return ;
if (number == )return ;
int val;
int num1=;
int num2=;
while(number-->){
val = num1+num2;
num1 = num2;
num2 = val;
}
return val;
}
};
9.题目描述
class Solution {
public:
int jumpFloorII(int number) {
int f=,fn=;
for(int i=;i<=number;i++){
fn=*f;
f=fn;
}
return fn;
}
};
10.题目描述
class Solution {
public:
int rectCover(int number) {
if(number==)return ;
if(number==)return ;
int n1=,n2=,val=;
for(int i=;i<number;i++){
val = n1 + n2;
n1=n2;
n2=val;
}
return val;
}
};
剑桥offer系列(1~10)的更多相关文章
- 干货 | 剑指offer系列文章汇总
下面是名企面试中经常会出现的面试题目,大家可以戳相应的题目查看题目细节,其答案会在紧接着的后一篇中出现 剑指offer系列 始 剑指offer—灯管问题(1) 剑指offer—10人电梯(2) ...
- ABP(现代ASP.NET样板开发框架)系列之10、ABP领域层——实体
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之10.ABP领域层——实体 ABP是“ASP.NET Boilerplate Project (ASP.NET样板 ...
- JVM基础系列第10讲:垃圾回收的几种类型
我们经常会听到许多垃圾回收的术语,例如:Minor GC.Major GC.Young GC.Old GC.Full GC.Stop-The-World 等.但这些 GC 术语到底指的是什么,它们之间 ...
- Mysql高手系列 - 第10篇:常用的几十个函数详解,收藏慢慢看
这是Mysql系列第10篇. 环境:mysql5.7.25,cmd命令中进行演示. MySQL 数值型函数 函数名称 作 用 abs 求绝对值 sqrt 求二次方根 mod 求余数 ceil 和 ce ...
- java高并发系列 - 第10天:线程安全和synchronized关键字
这是并发系列第10篇文章. 什么是线程安全? 当多个线程去访问同一个类(对象或方法)的时候,该类都能表现出正常的行为(与自己预想的结果一致),那我们就可以所这个类是线程安全的. 看一段代码: pack ...
- ShoneSharp语言(S#)的设计和使用介绍系列(10)— 富家子弟“语句“不炫富
ShoneSharp语言(S#)的设计和使用介绍 系列(10)— 富家子弟“语句“不炫富 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/Sho ...
- RabbitMQ 入门系列:10、扩展内容:延时队列:延时队列插件及其有限的适用场景(系列大结局)。
系列目录 RabbitMQ 入门系列:1.MQ的应用场景的选择与RabbitMQ安装. RabbitMQ 入门系列:2.基础含义:链接.通道.队列.交换机. RabbitMQ 入门系列:3.基础含义: ...
- 剑指offer系列57---整数中1出现的次数
[题目]求出1~n的整数中1出现的次数.(10进制) package com.exe11.offer; /** * [题目]求出1~n的整数中1出现的次数. * @author WGS * */ pu ...
- 剑指offer系列56---连续子数组的最大和
[题目]输入一个整型数组,数组里有正数也有负数.数组中一个或连续多个整数组成一个子数组. * 求所有子数组和的最大值. * [思路]连续求和数组元素.一旦得到的和小于0,就抛弃前面的数组,从当前值重写 ...
随机推荐
- .NET MVC和.NET WEB api混用时注意事项
1.同时配置了mvc路由和api路由时,mvc路由无法访问(调用所有mvc路由全部404错误) 在Global.asax中,需注意路由注册的顺序,将api路由注册放在最后: 即将 void Appli ...
- Java开发工程师(Web方向) - 01.Java Web开发入门 - 第1章.Web应用开发概述
第1章--Web应用开发概述 Web应用开发概述 浏览器-服务器架构(BS-architecture) browser/ App ---- request ----> server ...
- c++容器 STL
2019-01-24 22:30:32 记录学习PAT的一些知识,有待更新 注:本文是对Algorithm 算法笔记 的总结 C++标准库模板(Standard Template Library,ST ...
- NMAP-主机扫描
1.全面扫描 2.扫描指定段 3.ping扫描 只进行ping操作,十分隐蔽 4.无ping扫描 适用于防火墙禁止ping 5.TCP SYN扫描 6.TCP ACK扫描 7.UDP扫描 8.ICMP ...
- 2019-1-92.4G射频芯片培训资料
2019-1-92.4G射频芯片培训资料 培训 RF 小书匠 欢迎走进zozo的学习之旅. 2.4G芯片选型 2.4G芯片开发 Q&A 2.4G芯片选型 芯片类型 soc 防盗标签2.4G无 ...
- Thunder团队第五周 - Scrum会议3
Scrum会议3 小组名称:Thunder 项目名称:i阅app Scrum Master:王航 工作照片: 苗威同学在拍照,所以不在照片内. 参会成员: 王航(Master):http://www. ...
- 20145214 《Java程序设计》第8周学习总结
20145214 <Java程序设计>第8周学习总结 教材学习内容总结 日志API 使用日志的起点是Logger类,Logger类的构造函数标示为protected,不是java.util ...
- 2019寒假训练营寒假作业(二) MOOC的网络空间安全概论笔记部分
视频课程--MOOC的网络空间安全概论笔记 第一章 网络空间安全概述 2001年,网络空间概念被首次提出: 网络空间安全框架: 1.设备层安全: 可通过截获电磁辐射获取计算机信息.通过硬件木马(恶意电 ...
- Martin Fowler关于IOC和DI的文章(中文版)
IoC容器和Dependency Injection模式 Martin Fowler 编者语:最近研究IoC,在网上搜索到很多网页推荐阅读Martin Fowler的一片名叫Inversion of ...
- Java中I/O流之数据流
Java 中的数据流: 对于某问题:将一个 long 类型的数据写到文件中,有办法吗? 转字符串 → 通过 getbytes() 写进去,费劲,而且在此过程中 long 类型的数需要不断地转换. ...