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的更多相关文章

  1. [图形学] Chp18 OpenGL表面纹理函数

    以2D表面为例展示纹理贴图,用opengl设置一个2D纹理,颜色存储在32*32*3的数组中,对应的纹理坐标为0<=s, t<=1.0. 画出几个正方形表面,分别以GL_CLAMP(纹理坐 ...

随机推荐

  1. Fedora 19 配置参考

    1. 安装完Fedora 19之后,第一件事不是升级系统,而是添加源. 下载我配置好的源,非常全面,适用Fedora 19 x86_64.点我下载 打开终端,切换到repo.zip所在目录:mv re ...

  2. jquery 从页面获取li数组,删除不在数组中的key

    应用场景: 获取页面 li 下面 key的值,添加到 arr数组 删除车型不在arr 数组中的value值. 示例代码: var getSaleModels = function(brand_id){ ...

  3. 将double类型的值保留几位小数

    1.第一个参数(3.1415926)是要处理的数值.第二个参数(1)为要保留的几位小数.第三个参数是按照“四舍五入”还是"直接取这一位的值"(MidpointRounding.To ...

  4. Oracle中排序列中值相同引发的问题(译)

    This queston came up on the Oracle newsgroup a few days ago: 这个问题在Oracle的新闻中心被提出了一段时间: I have a tabl ...

  5. WPF 绑定四(层级绑定)

    xaml: <Window x:Class="WpfApplication1.Window4" xmlns="http://schemas.microsoft.co ...

  6. MvvmCross for WPF File Plugin

    本文以MvvmCross为框架基础 最近用了File Plugin插件,一开始也是没用明白,写一下记录下来,也方便需要的人吧 首先这个File Plugin需要先在UI项目里创建一个Bootstrap ...

  7. oracle中的记录类型

    单词RECORD有“记录”的意思,因此RECORD也称为“记录类型”,使用该类型的变量可以存储由多个列值组成的一行数据. 在声明记录类型变量之前,首先需要定义记录类型,然后才可以声明记录类型的变量. ...

  8. Java中构造函数执行顺序的问题

    1,  先执行内部静态对象的构造函数,如果有多个按定义的先后顺序执行:而且静态类的构造函数只会被执行一次,只在其第一个对象创建时调用,即便是创建了同一个类的多个对象,例如main()函数里b1,b2创 ...

  9. 进程和cpu绑定

    #include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<sys/sysinfo ...

  10. IEEE802是一个局域网标准系列

    视频教程 http://v.youku.com/v_show/id_XNjgyODA0NzE2.html?from=s1.8-1-1.2 干线 总线+环形网的传递机制 环形令牌总线 802.4