剑桥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方向) - 02.Servlet技术 - 第1章.Servlet
第1章--Servlet Servlet简介 Servlet应用于? 浏览器发出HTTP请求,服务器接收请求后返回响应给浏览器. 接收请求后到返回响应之间: 服务器将请求对象转交给Servlet容器 ...
- Ubuntu14.04 panic --not syncing: Attempt to kill init 解决方法
Ubuntu14.04 panic --not syncing: Attempt to kill init 解决方法 工作电脑装了一个虚拟机玩玩,胡乱下载了一些软件,apt-get了不少操作,后来重启 ...
- [模板]非递归线段树(zkw的变异版本)
类似于zkw,但空间只用两倍,zkw要4倍. 链接 可以下传标记,打熟后很好码. #include <set> #include <cmath> #include <cs ...
- 幸运的袋子(深度优先遍历(Depth First Search,DFS))
题目描述 一个袋子里面有n个球,每个球上面都有一个号码(拥有相同号码的球是无区别的).如果一个袋子是幸运的当且仅当所有球的号码的和大于所有球的号码的积. 例如:如果袋子里面的球的号码是{1, 1, 2 ...
- Python3 Tkinter-Button
1.绑定事件处理函数 from tkinter import * def hello(): print('Hello!') root=Tk() button=Button(root,text='cli ...
- Spring Boot - Filter实现简单的Http Basic认证
Copy自http://blog.csdn.net/sun_t89/article/details/51916834 @SpringBootApplicationpublic class Spring ...
- mysql 导入 大sql文件
任务:第一次用mysql,需要将一个1G左右的sql文件导入: 步骤:1:安装mysql-installer-community-5.7.20.0.msi 64位安装包 2:命令行登录: mysql ...
- wpa_supplicant上行接口浅析
摘自http://blog.csdn.net/fxfzz/article/details/6176414 wpa_supplicant提供的接口 从通信层次上划分, 上行接口:wpa_supplica ...
- 今年暑假不AC (贪心)
Description “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会 ...
- LintCode-532.逆序对
逆序对 在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.给你一个数组,求出这个数组中逆序对的总数. 概括:如果a[i] > a[j] 且 i < j, a[i ...