剑桥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,就抛弃前面的数组,从当前值重写 ...
随机推荐
- HTTP基本定义
一.网络的简单定义: 1.http:是www服务器传输超文本向本地浏览器的传输协议.(应用层) 2.IP:是计算机之间相互识别通信的机制.(网络层) 3.TCP:是应用层通信之间通信.(传输层) IP ...
- 腾讯云ubuntu安装使用MySQL
安装步骤 ubuntu@VM---ubuntu:~$ sudo apt-get install mysql-server (密码: root/root) ubuntu@VM---ubuntu:~$ s ...
- NHibernate3快速上手教程FluentNHibernate配置与DBHelper(已过期,有更好的)
很多学习NHibernate的新手很容易卡在配置文件这一关,正所谓万事开头难,上手后再配合官方文档就比较容易了. 网上关于配置文件的资料非常多,但由于版本的问题,许多老的教程中都没有明确指出类库的版本 ...
- [转载]Tensorflow中reduction_indices 的用法
Tensorflow中reduction_indices 的用法 默认时None 压缩成一维
- aishell数据处理为thchs30格式
目录 aishell数据转换格式 aishell数据转化方法 aishell数据格式对于用神经网络处理数据的同学来说比较不友善,因为他只有文字转录和音素级别的转录,并没有拼音的转录. 而thchs30 ...
- TensorFlow | ReluGrad input is not finite. Tensor had NaN values
问题的出现 Question 这个问题是我基于TensorFlow使用CNN训练MNIST数据集的时候遇到的.关键的相关代码是以下这部分: cross_entropy = -tf.reduce_sum ...
- 使用Response.Write实现在页面的生命周期中前后台的交互
Response.Write()方法非常的常见,也很普通,就是向http output中输出一string.其输出的内容位于页面的最顶端,常用来实现显示一些页面消息框等逻辑. 一般来说,在页面的整个生 ...
- 20162328蔡文琛week02
学号 20162328 <程序设计与数据结构>第2周学习总结 教材学习内容总结 这周学习了课本中的第二章内容,比起第一章,本章难度有略微底稿,从刚开始的显示字符转变为简单的加减乘除运算,经 ...
- 20145214实验二 Java面向对象程序设计
20145214实验二 Java面向对象程序设计 初步掌握单元测试和TDD 三种代码 伪代码 `百分制转五分制:` `如果成绩小于60,转成"不及格"` `如果成绩在60与70之间 ...
- iOS开发SDWebImage源码解析之SDWebImageManager的注解
最近看了两篇博客,写得很不错,关于SDWebImage源码解析之SDWebImageManager的注解: 1.http://www.jianshu.com/p/6ae6f99b6c4c 2.http ...