剑桥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(),只要两个相等, ...
随机推荐
- 记录---Testin上新手测试用例设计实战---碎乐3.2.0
平台上给的版本是碎乐3.12版的,但是平台上给的安装包下载不了,所以加群咨询之后给出了直接去手机应用商店下载搜索到的版本的对策.所以就那应用商店中找到的3.2.0版本来设计测试用例.因为任务中没有给出 ...
- Linux命令应用大词典-第34章 打印与传真
34.1 lpr:打印文件 34.2 lpq:显示打印队列状态 34.3 lprm:取消打印作业 34.4 lpstat:显示cups状态信息 34.5 cupsaccept:接受作业发送到目的地 3 ...
- Java初始化方法:类、容器
Java初始化方法:类.容器 初始化类(非final): Struts2的DefaultActionMapper类中: public DefaultActionMapper() { ...
- 查找 二叉树中 k1 到 k2区间的节点
vector<int> res; int key1, key2; void traverse(TreeNode * root){//采用前序遍历 if(root == NULL) retu ...
- Training Models
In this page, I am going to talk about the 'hello world' model that is linear regression and train i ...
- tendermint学习
怎么把两个节点变成验证节点 1. 两个节点分别启动 2. 两个节点同时把自己的公钥信息添加到对方的创始快配置文件中,总而言之,创始块一样 3. unsafe_reset_priv_validator ...
- Linux系统inotify工具安装配置
inotify主要功能 Inotify 是一个 Linux特性,它监控文件系统操作,比如读取.写入和创建.Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多.学习如何将 ...
- 常用实例:js格式化手机号为3 4 4形式
如何在填写手机号时将格式转换为3 4 4形式: 一:填写手机号时,在keyup事件中判断长度,符合条件时在值后面插入空格 $('#username').on('keyup',function(e){ ...
- Reversing Encryption(模拟水题)
A string ss of length nn can be encrypted(加密) by the following algorithm: iterate(迭代) over all divis ...
- 《JavaScript 高级程序设计》总结
一.JS基本概念 1.命名规则 变量名区分大小写(test和Test是两个不同的变量名),标识符采用驼峰命名格式,即:第一个字母小写,剩下的每个有意义的单词首字母大写: 标识符第一个字符必须是以字母. ...