剑桥offer(31~40)
31.题目描述
class Solution {
public:
int GetNumberOfK(vector<int> data ,int k) {
int low=;
int high = data.size()-;
int mid=;
int index=-;
while(low<=high){
mid = (low+high)/;
if(k == data[mid] )
index=mid;
break;
if(k>data[mid]){
low = mid+;
}else if(k<data[mid]){
high = mid -;
}
}
if(index==-)return ;
low = mid -;
high = mid +;
while(low>=&&data[low]==k)
low--;
while(high<data.size() &&data[high]==k)
high++;
return high-low-;
}
};
32.题目描述
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{ // if(pRoot == NULL) return 0;
// return max(TreeDepth( pRoot->left),TreeDepth( pRoot->right))+1; queue<TreeNode*> q;
if(!pRoot) return ;
q.push(pRoot);
int level=;
while(!q.empty()){
int len=q.size();
level++;
while(len--){
TreeNode* tem=q.front();
q.pop();
if(tem->left) q.push(tem->left);
if(tem->right) q.push(tem->right);
}
}
return level;
}
};
33.题目描述
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot==NULL)
return true;
int left=depth(pRoot->left);
int right=depth(pRoot->right);
if(abs(left-right)>)
return false;
bool booleft=IsBalanced_Solution(pRoot->left);
bool booright=IsBalanced_Solution(pRoot->right);
return booleft&&booright;
}
int depth(TreeNode *root){
if(root==NULL)
return ;
int left=depth(root->left);
int right=depth(root->right);
return (left>right)?(left+):(right+);
}
};
34.题目描述
class Solution {
public:
void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
int tmp =;
for (auto it=data.begin();it<data.end();it++){
tmp ^=(*it);
}
int index = ;
while((tmp&)==){
index++;
tmp=tmp>>;
}
for (auto it=data.begin();it<data.end();it++){
if((*it>>index)&==){
*num1 ^=(*it);
}else{
*num2 ^=(*it);
}
}
}
};
35.题目描述
class Solution {
public:
vector<vector<int> > FindContinuousSequence(int sum) {
int n = ;
int a1 = ;
vector<vector<int> >a;
vector<int>v;
while ( * sum - n*(n - )>){
if (( * sum - n*(n - )) % ( * n) == ){
a1 = ( * sum - n*(n - )) / ( * n);
cout << n << "----" << a1 << endl;
v.clear();
for(int i=;i<n;i++){
v.push_back(a1+i);
}
a.push_back(v);
}
n++;
}
reverse(a.begin(),a.end());
return a;
}
};
36.题目描述
class Solution {
public:
vector<int> FindNumbersWithSum(vector<int> array,int sum) {
vector<int> result;
int length = array.size();
int start = ;
int end = length - ;
while (start < end)
{
if (array[start] + array[end] == sum)
{
result.push_back(array[start]);
result.push_back(array[end]);
break;
}
else if (array[start] + array[end] < sum)
start++;
else
end--;
}
return result;
}
};
37.题目描述
class Solution {
public:
string LeftRotateString(string str, int n) {
int len = str.length();
while(n--){
char ch = str[];
for(int i=;i<len;i++){
str[i-]=str[i];
}
str[len-]=ch;
}
return str;
}
};
38.题目描述
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//字符串分割函数
vector< string> split(string str, string pattern)
{
vector<string> ret;
if (pattern.empty()) return ret;
size_t start = , index = str.find_first_of(pattern, );//查找,并返回index
while (index != string::npos)
{
if (start != index)
ret.push_back(str.substr(start, index - start));
start = index + ;
index = str.find_first_of(pattern, start);
}
if (!str.substr(start).empty())
ret.push_back(str.substr(start));
return ret;
} int main()
{
string str = "the sky #is blue";
string pattern = "# ";
vector< string> result = split(str, pattern);
//cout << "The result:" << result.size() << endl;
for (int i = result.size()-; i>=; i--)
{
cout << result[i]<<" ";
}
cout << endl;
return ;
}
39.题目描述
class Solution {
public:
bool IsContinuous( vector<int> numbers ) {
int max=-;
int min=;
int a[] = {};
int len = numbers.size();
if (len!=){
return false;
}
for(int i=; i<len; i++){
a[numbers[i]]++;
if(numbers[i] == ){
continue;
}
if(a[numbers[i]]>){
return false;
}
if(max < numbers[i]){
max = numbers[i];
}
if(min > numbers[i]){
min = numbers[i];
}
}
if(max-min <){
return true;
}else{
return false;
}
}
};
40.题目描述
class Solution {
public:
int LastRemaining_Solution(unsigned int n, unsigned int m)
{
if (n == || n == )
return -;
vector <int>a(n);
for (int i = ; i<n; i++){
a[i] = i;
}
int index = -;
while (a.size()>){
index = (m+index)%a.size() ;
a.erase(a.begin() + index);
index--;
}
return a[];
}
};
剑桥offer(31~40)的更多相关文章
- 剑指 Offer 31. 栈的压入、弹出序列 + 入栈顺序和出栈顺序的匹配问题
剑指 Offer 31. 栈的压入.弹出序列 Offer_31 题目详情: 解析: 这里需要使用一个栈来模仿入栈操作. package com.walegarrett.offer; /** * @Au ...
- 剑指 Offer 31. 栈的压入、弹出序列
剑指 Offer 31. 栈的压入.弹出序列 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如,序列 {1,2,3,4,5} 是某 ...
- 【剑指Offer】俯视50题之31 - 40题
面试题31连续子数组的最大和 面试题32从1到n整数中1出现的次数 面试题33把数组排成最小的数 面试题34丑数 面试题35第一个仅仅出现一次的字符 面试题36数组中的逆序对 面试题37两个链表的第一 ...
- 【Java】 剑指offer(31) 栈的压入、弹出序列
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否 ...
- [剑指Offer] 31.整数中1出现的次数
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- 剑指offer 面试40题
面试40题: 题目:最小的k个数 题:输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 解题代码一: # -*- coding ...
- 【Offer】[40] 【最小的K个数】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入n个整数,找出其中最小的k个数.例如,输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 牛客网刷题地 ...
- 剑指offer第40题
package com.yan.offer; /** * 题目描述: * * 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. * * @author Ya ...
- 剑指offer(40)数组中只出现一次的数字
题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 题目分析 第一种方法:使用js中的indexOf()和lastIndexOf(),只要两个相等, ...
随机推荐
- Django学习总结②----关系运算与F,Q关系
关联mysql步骤: 第一步:下载pymysql:pip install pymysql 第二步:在工程目录下的init文件下,将pymysql引入 import pymysql pymysql.in ...
- MyBatis 注解配置及动态SQL
一.注解配置 目前MyBatis支持注解配置,用注解方式来替代映射文件,但是注解配置还是有点不完善,在开发中使用比较少,大部分的企业还是在用映射文件来进行配置.不完善的地方体现在于当数据表中的字段 ...
- 《剑指Offer》题十一~题二十
十一.旋转数组的最小数字 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如,数组{3, 4, 5, 1, 2}为{ ...
- OpenGL ES 2.0 -- 制作 3D 彩色旋转三角形 - 顶点着色器 片元着色器 使用详解
最近开始关注OpenGL ES 2.0 这是真正意义上的理解的第一个3D程序 , 从零开始学习 . 案例下载地址 : http://download.csdn.net/detail/han120201 ...
- 有关c#的学习笔记整理与心得
[ 塔 · 第 一 条 约 定 ] 整理c#:Array Arraylist List Hashtable Dictionary Stack Queue等 Array 的容量是固定的,而 ArrayL ...
- (转)apktool+dex2jar+jd_gui
转:http://www.cnblogs.com/MichaelGuan/archive/2011/10/25/2224578.html apktool: 可以解析资源文件,比如布局文件xml等,方便 ...
- TCP系列35—窗口管理&流控—9、紧急机制
一.概述 我们在最开始介绍TCP头结构的时候,里面有个URG的标志位,还有一个Urgent Pointer的16bits字段.当URG标志位有效的时候,Urgent Poinert用来指示紧急数据的相 ...
- yum 安装 redis php-redis
yum 安装 redis php-redis redis和php-redis在官方源上是没有的,需要安装其他的源,其他源的地址为 http://mirrors.ustc.edu.cn/fedora ...
- 不同品牌交换机设置telnet方法
H3C交换机:1.设置telnet system-view super password level 3 cipher ******telnet server enable user-interfac ...
- C# 知识回顾 - 匿名方法
C# 基础回顾 - 匿名方法 目录 简介 匿名方法的参数使用范围 委托示例 简介 在 C# 2.0 之前的版本中,我们创建委托的唯一形式 -- 命名方法. 而 C# 2.0 -- 引进了匿名方法,在 ...