剑桥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(),只要两个相等, ...
随机推荐
- Java开发工程师(Web方向) - 04.Spring框架 - 第1章.Spring概述
第1章.Spring概述 Spring概述 The Spring Framework is a lightweight solution and a potential one-stop-shop f ...
- 基于 CPython 解释器,为你深度解析为什么Python中整型不会溢出
前言 本次分析基于 CPython 解释器,python3.x版本 在python2时代,整型有 int 类型和 long 长整型,长整型不存在溢出问题,即可以存放任意大小的整数.在python3后, ...
- Python3 集合
1.集合的表示 集合是一个无序不重复的元素序列 创建空集合 set() 2.集合的运算 a={1,2,3} b={2,3,4} print(a-b) #a中包含b中不包含 print(a|b) #a中 ...
- 动态规划——最长上升子序列LIS及模板
LIS定义 一个数的序列bi,当b1 < b2 < … < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, …, aN),我们可以得到一些上升的子序列(ai1 ...
- .从列表结束中删除第N个节点
描述 给定一个链表,从列表的最后删除倒数第n个元素 例如: 给定链表:1-> 2-> 3-> 4-> 5,并且n = 2. 删除倒数第二个,链表将变为1-> 2-> ...
- YaoLingJump开发者日志(七)
LGame用起来真是各种蛋疼,插背景都可以显示不出来.在屏幕结束后释放资源,重载该屏幕时再setbackground也不行,直接用Lpaper当background更不行,会把tilemap上的东 ...
- node必学的Hello World实现--服务器实现
node是JavaScript运行在后端的一种实现.而后端语言,不管是php,java都需要一个服务器才能跑起来,node如是. node的服务器较php而言,少了单独安装服务器的步骤,node的服务 ...
- node中的path.resolve
path.resolve([arg1,arg2,...])根据参数的不同,返回值存在两种情况. 以下为参数的两种情况: 1.每个参数都不带'/',比如path.resolve(),或者path.res ...
- CEntOS6.5从启动界面直接进入命令行界面
ctrl + alt + F1 ctrl + alt + F2 ctrl + alt + F3 ctrl + alt + F4 ctrl + alt + F5 ctrl + alt + F6 同时按下 ...
- 【Python】Python中*args 和**kwargs的用法
好久没有学习Python了,应为工作的需要,再次拾起python,唤起记忆. 当函数的参数不确定时,可以使用*args 和**kwargs,*args 没有key值,**kwargs有key值. 还是 ...