剑桥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,就抛弃前面的数组,从当前值重写 ...
随机推荐
- 【转】cocos2dx3.2学习笔记之Director(导演类)
转载:https://blog.csdn.net/u013435551/article/details/38579747 在Cocos2d-x中,把统筹游戏大局的类抽象为导演类(Director),D ...
- Python|一文简单看懂 深度&广度 优先算法
一.前言 以后尽量每天更新一篇,也是自己的一个学习打卡!加油!今天给大家分享的是,Python里深度/广度优先算法介绍及实现. 二.深度.广度优先算法简介 1. 深度优先搜索(DepthFirstSe ...
- ionic 获取input的值
1.参数传递法 例子:获取input框内容 这里有个独特的地方,直接在input处使用 #定义参数的name值,注意在ts中参数的类型 在html页面中 <ion-input type=&quo ...
- Vuejs 实现简易 todoList 功能 与 组件
todoList 结合之前 Vuejs 基础与语法 使用 v-model 双向绑定 input 输入内容与数据 data 使用 @click 和 methods 关联事件 使用 v-for 进行数据循 ...
- 【MySQL解惑笔记】Centos7下卸载彻底MySQL数据库
彻底卸载Yum安装的MySQL数据库 在我第二章MySQL数据库基于Centos7.3-部署过程中,因为以前安装过其它的版本所以没有卸载干净影响后期安装 一.卸载Centos7自带的Maridb数据库 ...
- File Searching
Description Have you ever used file searching tools provided by an operating system? For example, in ...
- android入门 — ListView
ListView主要是用来解决大量数据展示的问题,它的用途很广泛,几乎所有的app都会用到,比如说知乎.今日头条.微博.通讯录等. ListView允许用户通过上下滑动的方式将屏幕外的数据滚动到屏幕中 ...
- JS DOM视频相关的知识
1.实现点击a标签改变图片时,如果a的href属性有一个目标网址,但是点击又必须跳转到另外一张图,往往会最后跳转到目标网址,可以在onclick事件函数中加入ruturn false,阻止跳转到页面. ...
- 每个zone的low memory是怎么计算出来的
内核都是试图让活动页和不活动页的数量均衡 在分配内存时每次都会唤醒wakeup_swapd,这个函数会在 现在是不是已经没有全局的LRU表了?已经都变成per cgroup级别的LRU表了吗? ina ...
- 解决编写 xml 没有代码提示
有时候在编写 struts.xml 会没有代码提示,一般是因为没有联网导致的,或者之前配置过 dtd 文件 url,但是文件路径之后被修改了. 解决方案有: 让电脑联网 修改 dtd 的本地路径以及 ...