剑桥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(),只要两个相等, ...
随机推荐
- Python拼接字符串的7种方法
1.直接通过+操作: s = 'Python'+','+'你好'+'!'print(s) 打印结果: Python,你好! 2.通过join()方法拼接: 将列表转换成字符串 strlist=['Py ...
- 腾讯云ubuntu安装使用MySQL
安装步骤 ubuntu@VM---ubuntu:~$ sudo apt-get install mysql-server (密码: root/root) ubuntu@VM---ubuntu:~$ s ...
- centos7安装zabbix3.2详解
服务器端安装 1.安装仓库 rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noar ...
- 【递归入门】组合+判断素数:dfs(递归)
题目描述 已知 n 个整数b1,b2,…,bn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和. 例如当 n=4,k=3,4 个整数分别为 3,7,12, ...
- dice2win
触发交易 转0个 https://etherscan.io/tx/0x784e80167353a886183106cbe3bd15e614cafdb5d6885ccd101177aa0f937a36 ...
- es6从零学习(三):Class的基本用法
es6从零学习(三):Class的基本用法 一:定义一个类 //定义类 class Point { constructor(x, y) { this.x = x; this.y = y; } toSt ...
- Java学习个人备忘录之多线程
进程:正在进行中的程序(直译). 线程:就是进程中一个负责程序执行的控制单元(执行路径) 一个进程中可以有多个执行路径,称之为多线程. 一个进程中至少要有一个线程. 开启多个线程是为了同时运行多部分代 ...
- MyBatis 基本构成与框架搭建
核心组件 SqlSessionFactoryBuilder (构造器) 根据配置信息(eg:mybatis-config.xml)或者代码来生成SqlSessionFactory. SqlSessio ...
- 一个例子说明mouseover事件与mouseenter事件的区别
<html> <head> <meta charset="UTF-8"> <title>haha</title> < ...
- UCP协议
UDP只在ip数据报的服务上增加了一点功能,就是复用和分用还有差错检验的功能 (1)UDP是面向无连接:发送之前不需要建立连接,减少了时间延续 (2)UDP只是尽最大努力交付,不能保证无措 (3)UD ...