Chap6: question38 - 42
38. 数字 k 在有序数组中出现的次数
二分查找:找出第一个 k 和最后一个 k 。
#include <iostream>
using namespace std;
int getFirstOfK(int data[], int length, int k, int low, int high)
{
if(low <= high)
{
int mid = (low + high) / 2;
if(data[mid] == k && (mid == 0 || data[mid-1] != k))
return mid;
else if(data[mid] < k)
low = mid + 1;
else high = mid - 1;
return getFirstOfK(data, length, k, low, high);
}
return -1;
}
int getLastOfK(int data[], int length, int k, int low, int high)
{
if(low <= high)
{
int mid = (low + high) / 2;
if(data[mid] == k && (mid == length-1 || data[mid+1] != k))
return mid;
else if(data[mid] > k)
high = mid - 1;
else low = mid + 1;
return getLastOfK(data, length, k, low, high);
}
return -1;
}
int getNumberOfK(int data[], int length, int k)
{
int count = 0;
if(data != NULL && length > 0)
{
int first = getFirstOfK(data, length, k, 0, length-1);
if(first == -1) return -1; int last = getLastOfK(data, length, k, first, length-1);
count = last - first + 1;
}
return count;
}
int main()
{
int data[] = {1, 2, 3, 3, 3, 3, 4, 5};
cout << getNumberOfK(data, sizeof(data)/4, 3) << endl;
cout << getNumberOfK(data, sizeof(data)/4, 1) << endl;
cout << getNumberOfK(data, sizeof(data)/4, 2) << endl;
cout << getNumberOfK(data, sizeof(data)/4, 5) << endl; return 0;
}
39. 二叉树的深度 && 平衡二叉树的判断 && 二叉树结点的最大距离(题目来自编程之美,解法自创)
note:三种算法都必须是后序遍历。
#include <iostream>
#include <string>
using namespace std;
typedef struct BTNode
{
int v; // default positive Integer.
BTNode *pLeft;
BTNode *pRight;
BTNode(int x) : v(x), pLeft(NULL), pRight(NULL) {}
} BinaryTree;
/********************************************************/
/***** Basic functions ***********/
BinaryTree* createBinaryTree() // input a preOrder traversal sequence, 0 denote empty node.
{
BTNode *pRoot = NULL;
int r;
cin >> r;
if(r != 0) // equal to if(!r) return;
{
pRoot = new BTNode(r);
pRoot->pLeft = createBinaryTree();
pRoot->pRight = createBinaryTree(); }
return pRoot;
}
void release(BinaryTree *root){
if(root == NULL) return;
release(root->pLeft);
release(root->pRight);
delete[] root;
root = NULL;
}
void print(BinaryTree *root, int level = 1){
if(root == NULL) { cout << "NULL"; return; };
string s;
for(int i = 0; i < level; ++i) s += " ";
cout << root->v << endl << s;
print(root->pLeft, level+1);
cout << endl << s;
print(root->pRight, level+1);
}
/******************************************************************/
int getDepth(BinaryTree *root) // leaf Node is at depth 1
{
if(root == NULL) return 0;
int leftDepth = getDepth(root->pLeft);
int rightDepth = getDepth(root->pRight);
return 1 + (leftDepth > rightDepth ? leftDepth : rightDepth);
} bool isBalanced(BinaryTree *root, int *depth) // must be postOrder traversal
{
if(root == NULL) { *depth = 0; return true; };
int leftDepth, rightDepth;
if(isBalanced(root->pLeft, &leftDepth) && isBalanced(root->pRight, &rightDepth))
{
*depth = 1 + (leftDepth > rightDepth ? leftDepth : rightDepth);
if(leftDepth - rightDepth >= -1 && leftDepth - rightDepth <= 1)
return true;
else
return false;
}
}
bool isBalanced(BinaryTree *root)
{
int depth;
return isBalanced(root, &depth);
} int getMaxDistance(BinaryTree *root, int *maxDistance) // leaf node depth is set to 0
{
if(root == NULL) return -1;
int leftDepth = getMaxDistance(root->pLeft, maxDistance);
int rightDepth = getMaxDistance(root->pRight, maxDistance);
if(*maxDistance < 2 + leftDepth + rightDepth)
*maxDistance = 2 + leftDepth + rightDepth;
return 1 + (leftDepth > rightDepth ? leftDepth : rightDepth);
}
int getMaxDistance(BinaryTree *root)
{
int maxDistance = 0;
getMaxDistance(root, &maxDistance);
return maxDistance;
}
int main(){
int TestTime = 3, k = 1;
while(k <= TestTime)
{
cout << "Test " << k++ << ":" << endl; cout << "Create a tree: " << endl;
BinaryTree *pRoot = createBinaryTree();
print(pRoot);
cout << endl; cout << "The depth of binary tree: " << getDepth(pRoot) << endl; if(isBalanced(pRoot))
cout << "Does the tree is a balanced binary tree ? true" << endl;
else
cout << "Does the tree is a balanced binary tree ? false" << endl; cout << "The max distance between two nodes: " << getMaxDistance(pRoot) << endl; release(pRoot);
}
return 0;
}

40. 数组中只出现一次的数字
首先, 参考 Link: Single Number
其次,数组中有两个只出现一次的数字时: 例:{2,4,3,6,3,2,5,5}
#include <iostream>
using namespace std;
void findTwoNumbers(int data[], int length, int *num1, int *num2)
{
if(data == NULL || length < 2) return;
int total = 0;
for(int i = 0; i < length; ++i)
total ^= data[i];
int shift1 = 1;
for(int i = 0; i < sizeof(int)*8; ++i)
{
total >>= 1;
shift1 <<= 1;
if(total & 1)
break;
}
*num1 = *num2 = 0;
for(int i = 0; i < length; ++i)
{
if(data[i] & shift1) *num1 ^= data[i];
else *num2 ^= data[i];
}
}
int main(){
int num1, num2;
int test1[8] = { 2, 4, 3, 6, 3, 2, 5, 5};
findTwoNumbers(test1, 8, &num1, &num2);
cout << num1 << " "<< num2 << endl;
return 0;
}

41. 和为 S 的连续正数序列。
#include <iostream>
using namespace std;
void numsSumToS(int S)
{
int low = 1, high = 2;
while(low < high)
{
int curSum = 0;
for(int i = low; i <= high; ++i)
curSum += i;
if(curSum < S) ++high;
else if(curSum > S) ++low;
else
{
for(int i = low; i <= high; ++i)
cout << i << '\t';
cout << endl;
++high;
}
}
}
int main(){
int S;
while(true)
{
cout << "cin >> ";
cin >> S;
numsSumToS(S);
}
return 0;
}

42. 翻转单词顺序 && 字符串左旋转
note:左旋转 k 位相当于右旋转 N – k 位, N 为字符串长度。
Link: 7. Reverse Words in a String
Chap6: question38 - 42的更多相关文章
- (转)win7 64 安装mysql-python:_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
原文地址:http://www.cnblogs.com/fnng/p/4115607.html 作者:虫师 今天想在在win7 64位环境下使用python 操作mysql 在安装MySQL-pyth ...
- Effective Modern C++ 42 Specific Ways to Improve Your Use of C++11 and C++14
Item 1: Understand template type deduction. Item 2: Understand auto type deduction. Item 3: Understa ...
- 把《c++ primer》读薄(4-2 c和c++的数组 和 指针初探)
督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. 问题1.我们知道,将一个数组赋给另一个数组,就是将一个数组的元素逐个赋值给另一数组的对应元素,相应的,将一个vector 赋给另 ...
- PAT mooc DataStructure 4-2 SetCollection
数据结构习题集-4-2 集合的运用 1.题目: We have a network of computers and a list of bi-directional connections. Eac ...
- PHP开发程序应该注意的42个优化准则
PHP 独特的语法混合了 C.Java.Perl 以及 PHP 自创新的语法.它可以比 CGI或者Perl更快速的执行动态网页.用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML ...
- win7 64 安装mysql-python:_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
今天想在在win7 64位环境下使用python 操作mysql 在安装MySQL-python 时报错: _mysql.c _mysql.c(42) : fatal error C1083: Can ...
- Atitit J2EE平台相关规范--39个 3.J2SE平台相关规范--42个
Atitit J2EE平台相关规范--39个 3.J2SE平台相关规范--42个 2.J2EE平台相关规范--39个5 XML Parsing Specification16 J2EE Conne ...
- 每天一个linux命令(42):kill命令
Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后台进程就须 ...
- AC日记——画矩形 1.5 42
42:画矩形 总时间限制: 1000ms 内存限制: 65536kB 描述 根据参数,画出矩形. 输入 输入一行,包括四个参数:前两个参数为整数,依次代表矩形的高和宽(高不少于3行不多于10行,宽 ...
随机推荐
- Hibernate 检索策略
概述 检索数据时的 2 个问题: –不浪费内存:当 Hibernate 从数据库中加载 Customer 对象时, 如果同时加载所有关联的 Order 对象, 而程序实际上仅仅需要访问 Custome ...
- Rhel6-mpich2 hpc集群配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.121 server21.example.com 192.168.12 ...
- 当BPM业务流程管理遇上制造业
2015年5月,K2正式与赛科利签约,准备上线核心采购类流程,包括PR.PO.Payment.供应商管理等. 上海赛科利汽车模具技术应用有限公司隶属于上汽集团,现有员工2300余人.为解决汽车“安全” ...
- c/c++面试题(5)(c++重要的概念详解)
1.C++面向对象的三大特征? 1)封装:将客观事物封装成抽象的类,并且设计者可以对类的成员进行访问控制权限控制. 这样一方面可以做到数据的隐藏,保护数据安全;另一方面,封装可以修改类的内部 实现而不 ...
- Mousejack Hacking : 如何利用MouseJack进行物理攻击
0×00 前言 近期安全公司Bastille Networks(巴士底狱)安全研究员发现大多数无线鼠标和接收器之间的通信信号是不加密的.黑客可对一百米范围内存在漏洞的蓝牙无线键鼠进行嗅探甚至劫持,从而 ...
- iOS 剪贴板基本知识
在iOS 中下面三个控件,自身就有复制-粘贴功能 1.UITextView 2.UITextField 3.UIWebView UIKit framework提供了几个类和协议方便我们在自己的应用程序 ...
- [Java Basics] Reflection
For every type of object, the Java virtual machine instantiates an immutable instance of java.lang.C ...
- 如何获取WIN10 Program Files 文件夹下的文件操作权限
找到指定文件,右键-属性-找到指定用户-授"完全控制权限“--更改文件--恢复默认权限.
- OD调试2
通过视频学习,实现了一下简单的traceme爆破.这不是把它的序列号破译出来,只是识别了自己的号码,不算真正的爆破.(与期望有点差异) 先来看一下这款软件的逻辑结构. 先输入用户名 以及序列号 然后 ...
- 设置ASP.NET页面的运行超时时间详细到单个页面及站点
这篇文章主要介绍了如何设置ASP.NET页面的运行超时时间,包括全局超时时间.单个站点超时时间.单个页面请求超时时间,需要的朋友可以参考下 全局超时时间 服务器上如果有多个网站,希望统一设置一 ...