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(纹理坐 ...
随机推荐
- UITableView学习笔记
//非原创 看TableView的资料其实已经蛮久了,一直想写点儿东西,却总是因为各种原因拖延,今天晚上有时间静下心来记录一些最近学习的TableView的知识.下面进入正题,UITableView堪 ...
- <hash命令:显示、添加或清除哈希表>
linux系统下的hash指令: 说明:linux系统下会有一个hash表,当你刚开机时这个hash表为空,每当你执行过一条命令时,hash表会记录下这条命令的路径,就相当于缓存一样.第一次执行命令s ...
- win8.1环境下硬盘安装centos6.5双系统
作为一名软件工程师或是相关行业的从业人员,经常会用到linux系统,这里为大家介绍的安装linux的方案是在原有win环境下安装linux双系统,两个系统选择分别为win8.1 和centos6.5. ...
- css style与class之间的区别
问题描述: 网页点击[导出]按钮后,将页面table内容另存成excel文件,却发现无法保存表格样式 分析过程: 1.table表格用class,而不是style.导出时并没有导出class定义 ...
- 转: js操作cookie
cookie的几个概念 http://dearhappyfish.blog.163.com/blog/static/1901094152012422114753777/ js操作cookie 转:ht ...
- 转换 Html 内容为纯文本内容(html,文本互转)
转自http://www.cnblogs.com/jyshi/archive/2011/08/09/2132762.html : /// <summary> /// 转换纯文本内容为 HT ...
- Zencart 国家排序及中文名称的扩展
最终实现效果如上 具体步骤: 1. 手动或SQL修改数据表,增加2个字段 ) ) '; 2. 修改admin/countries.php文件,增加表单插入编辑功能, 共计7处,此处忽略具体代码. 3. ...
- Crusher Django 学习笔记4 使用Model
http://crusher-milling.blogspot.com/2013/09/crusher-django-tutorial4-using-basic.html 顺便学习一下FQ Crush ...
- 使用VisualSVN Server自动发布站点
使用VisualSVN Server自动发布站点可以节省大量的发布时间. 适用于项目测试版本 通常一个项目在测试阶段会遇到以下问题 1.开发人员修改缺陷要实时反应到测试环境 2.项目经理想让客户及时看 ...
- windows创建桌面快捷方式的VBA脚本
Dim wShell, oShortcut 'Dim strDesktop$ ' 为了与VBS兼容, Dim strDesktop ' 这里改写一下,测试通过... Set w ...