剑桥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,就抛弃前面的数组,从当前值重写 ...
随机推荐
- Iview 表单提交: Cannot read property 'validate' of undefined
提交表单时,提示报这个错,找了半天,然后也百度了很久,一直没找到答案,代码如下: <link href="~/lib/iview3.1.4/styles/iview.css" ...
- 题解 CF682C 【Alyona and the Tree】
简单搜索题,我们每找到一组不满足题目给出条件的点和边就将其整个子树删除,然后最终答案加上该子树的大小即可.注意,搜索的时候如果当前的边权和sum已经为负了,应该将其改为0(可以想想为什么) 注:题目翻 ...
- 【转】网游服务器中的GUID(唯一标识码)实现-基于snowflake算法
本文中的算法采用twitter的snowflake算法,具体请搜索介绍,原来是用Scala写的,因我项目需要,改写成C++语言,主要用于高效的生成唯一的ID, 核心算法就是毫秒级时间(41位)+机器I ...
- IMPI Python集群运行报错:
Intel MPI环境利用hostfile多主机运行下报错 HYDU_process_mfile_token (../../utils/args/args.c:523): token slots no ...
- 【Python 开发】第三篇:python 实用小工具
一.快速启动一个web下载服务器 官方文档:https://docs.python.org/2/library/simplehttpserver.html 1)web服务器:使用SimpleHTTPS ...
- Thunder团队第五周 - Scrum会议4
Scrum会议4 小组名称:Thunder 项目名称:i阅app Scrum Master:李传康 工作照片: 邹双黛同学在拍照,所以不在照片内. 参会成员: 王航:http://www.cnblog ...
- 《JavaScript 高级程序设计》总结
一.JS基本概念 1.命名规则 变量名区分大小写(test和Test是两个不同的变量名),标识符采用驼峰命名格式,即:第一个字母小写,剩下的每个有意义的单词首字母大写: 标识符第一个字符必须是以字母. ...
- TensorFlow高级API(tf.contrib.learn)及可视化工具TensorBoard的使用
一.TensorFlow高层次机器学习API (tf.contrib.learn) 1.tf.contrib.learn.datasets.base.load_csv_with_header 加载cs ...
- 活学活用wxPython
http://www.czug.org/python/wxpythoninaction/
- 第72天:jQuery实现下拉菜单
jQuery实现下拉菜单 一.居中 1.块元素居中:给块元素本身设置:margin:0 auto;,块元素必须设置宽度 2.行内块元素居中:给元素父级设置text-algin:center; < ...