Chp18: Hard
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators.
Solution: deal with 759 + 674.
1. add 759 + 674, but forget to carry. get 323
2. add 759 + 674, but only do the carrying, rather than the addition of each digit. get 1110
3. add the result of the first two operations(recursively): 1110 + 323 = 1433
public int add(int a, int b){
if(b == 0) return a;
if(a == 0) return b;
int sum = a ^ b; // 1
int ope = (a & b) << 1; // 2
return add(sum, ope);
}
18.3 Write a method to randomly generate a set of m integers from an array of size n. Each element must have equal probability of being chosen.
Solution: shrink the array to no longer contain elements that already chosen.
public int[] pickMRandomly(int[] original, int m){
int[] result = new int[m];
int array = original.clone(); // 必须要先复制出来,否则会更改原来数组的结构
for(int j = 0; j < m; j ++){
int index = random(j, array.length - 1);
result[j] = array[index];
array[index] = array[j];
}
return result;
}
18.6 Describe an algorithm to find the smallest one million numbers in one billion numbers. Assume that the computer memory can hold all one billion numbers.
Selection Rank Algorithm: find the ith smallest (or largest) element in an array in linear time.
If the elements are unique, can find it in O(n).
1.pick a random element in the array and use it as a "pivot". Partition elements around the pivot, keep track of the number of elements on the left side of the partition.
2.if there are exactly i elements on the left, then you just return the biggest element on the left.
3.if the left side is bigger than i, repeat the algo on just the left part of the array.
4.if the left side is smaller than i, repeat the algo on the right, but look for the lement with rank i - leftsize.
public int partition(int[] array, int left, int right, int pivot){
while(true){
while(left <= right && array[left] <= pivot) left ++;
while(left <= right && array[right] > pivot) right --;
if(left > right) return left - 1;
swap(array, left, right);
}
}
public int rank(int[] array, int left, int right, int rank){
int pivot = array[randomIntInRange(left, right)];
int leftEnd = partition(array, left, right, pivot);
int leftSize = leftEnd - left + 1;
if(leftSize == rank + 1) return max(array, left, leftEnd);
else if(rank < leftSize) return rank(array, left, leftEnd, rank);
else return rank(array, leftEnd + 1, right, rank - leftSize);
}
Chp18: Hard的更多相关文章
- [图形学] Chp18 OpenGL表面纹理函数
以2D表面为例展示纹理贴图,用opengl设置一个2D纹理,颜色存储在32*32*3的数组中,对应的纹理坐标为0<=s, t<=1.0. 画出几个正方形表面,分别以GL_CLAMP(纹理坐 ...
随机推荐
- 10款很酷的HTML5动画和实用应用 有源码
10款很酷的HTML5动画和实用应用,这里有菜单.SVG动画.Loading动画,总有你喜欢的,而且,每一款HTML5应用都提供源代码下载,方便大家学习和研究,一起来看看吧. 1.HTML5 SVG ...
- 让apache与mysql随着系统自动启动
让apache与mysql随着系统自动启动 在Linux中有一个文件/etc/rc.d/rc.local文件,其系统在启动时会自动加载该文件,我们可以把要启动的服务放入这个文件中即可. 添加以下代码:
- C++ Strings(字符串)
Constructors 构造函数,用于字符串初始化 Operators 操作符,用于字符串比较和赋值 append() 在字符串的末尾添加文本 assign() 为字符串赋新值 at() 按给定索引 ...
- bzoj 1009:[HNOI2008]GT考试
这道题机房n多人好久之前就A了…… 我到现在才做出来…… 一看就是DP+矩阵乘法,但是一开始递推式推错了…… 正确的递推式应该是二维的…… f[i][j] 表示第准考证到第 i 位匹配了 j 位的方案 ...
- ARC小知识
ARC是iOS 5推出的新功能,全称叫 ARC(Automatic Reference Counting).简单地说,就是代码中自动加入了retain/release,原先需要手动添加的用来处理内存管 ...
- NSTimer定时器类
NSTimer是Cocoa中比较常用的定时器类,基本操作如下: handleTimer方法可以自行定义.在需要的地方创建timer即可,handleTimer就可以每0.5秒执行一次. - (vo ...
- iOS Foundation框架 -3.利用NSNumber和NSValue将非OC对象类型数据存放到集合
1.Foundation框架中提供了很多的集合类如:NSArray,NSMutableArray,NSSet,NSMutableSet,NSDictionary,NSMutableDictionary ...
- Easyui datebox控件打开页面就验证解决方法
问题描述: datebox时间控件有些场景下默认值需要为空,但是为空的情况下打开页面会自动验证,十分影响美观. 实现原理: <input class="easyui-databox&q ...
- 为什么要用ajax
Ajax应用程序的优势在于:1. 通过异步模式,提升了用户体验2. 优化了浏览器和服务器之间的传输,减少不必要的数据往返,减少了带宽占用3. Ajax引擎在客户端运行,承担了一部分本来由服务器承担的工 ...
- php读取excel文件的实例代码
php读取excel文件的实例代码. 代码: <?php /** * php读取excel文件 * by www.jbxue.com */ $this->loadexcel();//半酣p ...