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(纹理坐 ...
随机推荐
- 配置Hibernate二级缓存步骤
配置Hibernate二级缓存步骤: 加入二级缓存的jar包及配置文件 jar包位置:hibernate-release-4.1.8.Final\lib\optional\ehcache下所有jar包 ...
- Virtualizing WrapPanel VS toolkit:WrapPanel
用toolkit:WrapPanel的时候,LIST太大,内存不行,等下我试试 Virtualizing WrapPanel这个 http://www.codeproject.com/Articles ...
- 实验九--裸机LCD
一.环境 系统:ubuntu12.04 开发板:jz2440 编译器:gcc 二.说明 有空补上 三.代码 Makefile: CC = arm-linux-gcc LD = arm-linux-ld ...
- 【Sharing】开发与研发
[声明]此文为转载,只为收藏. 按:这几天我一直在写这篇东西,本来是胸有成竹,没想到后来越写越发现自己在这个题目下有太多话想说,而以我现在的能力又不能很好地概括总结,以至于越写越长,文章结构也变得混乱 ...
- window store app 附件读取
public static async Task<bool> DisplayApplicationPicker(string folderName, string fileName) { ...
- 一个Ctrl+V下的问题
对于电脑快捷键来说恐怕没什么比Ctrl+C和Ctrl+V更熟悉的了. 最近做了一个小程序,界面上有一个文本框,要做的事情就是把从别的地方复制内容后粘贴到文本框中,然后以自己处理后的格式显示出来. 为了 ...
- QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码(转)
OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的所有内容. QQ登录OAuth2 ...
- php取得当前访问url文件名的几种方法
php下获取当前访问的文件名的几种方法.推荐函数:一是PHP获取当前页面的网址: dedecms用的也是这个哦. <?php //获得当前的脚本网址 function GetCurUrl() { ...
- Translation002—Package Index(Android包索引)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 看本翻译前请您注意: 本人初学android,可能有的翻译不是非常准确,但本人尽最大努力,不清楚处会做标记,并附 ...
- xv6实验环境搭建
安装bochs 因为要运行的是xv6,所以不能直接使用 apt-get 直接获取软件.apt-get获取到的软件不支持SMP (Symmetric Multi-Processing).因此,需要下载源 ...