剑桥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,就抛弃前面的数组,从当前值重写 ...
随机推荐
- Unity Lighting - Choosing a Lighting Technique 选择照明技术(一)
Choosing a Lighting Technique 选择照明技术 https://unity3d.com/cn/learn/tutorials/topics/graphics/choosi ...
- Java开发工程师(Web方向) - 02.Servlet技术 - 第1章.Servlet
第1章--Servlet Servlet简介 Servlet应用于? 浏览器发出HTTP请求,服务器接收请求后返回响应给浏览器. 接收请求后到返回响应之间: 服务器将请求对象转交给Servlet容器 ...
- Java应用基础微专业-设计篇
第1章--抽象与接口 1.1 抽象 An abstract class can be created without abstract methods, the purpose of doing th ...
- VMware实现控制台功能(VMware Remote Console)
说明: 刚开始一脸懵逼,google了一些资料,发现基本没有能快速落地的,自己做完后梳理了一下发上来供大家参考. 如果帮到你了,请点赞评论关注,以资鼓励,多谢~ 实现VMware控制台功能主要有两种方 ...
- lintcode50 数组剔除元素后的乘积
数组剔除元素后的乘积 给定一个整数数组A. 定义B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], 计算B的时候请不要使用除法. 您在真实的面试中是 ...
- POJ - 3259
要判断是否有负的权值 #include<iostream> #include<stdio.h> #include<algorithm> #include<st ...
- python 读取 log日志的编码问题
1.我要读取log日志的”执行成功”的个数,log日志编码格式为GBK 2.显示报错,大致意思是说utf-8的代码不能解析log日志 3.后来想想把log日志用GBK编码读出来,写到新文件中,用utf ...
- "Hello world!"团队第一次会议
今天是我们"Hello world!"团队第一次召开会议,今天的会议可能没有那么正式,但是我们一起确立了选题——基于WEB的售票系统.博客内容是: 1.会议时间 2.会议成员 3. ...
- 欢迎来怼-Alpha周(2017年10月19)贡献分配规则和分配结果
.从alpha周(2017年10月19日开始的2周)开始,提高贡献分比重. 贡献分 : 团队分 = 1 : 5 教师会在核算每位同学总分时按比例乘以系数. 每位同学带入团队贡献分10分,如果团队一共7 ...
- c#事件实质
c#的事件实际上是对windows消息的封装: windows消息系统分为3部分:消息队列,消息循环,窗口过程(wndproc函数)