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. WEB应用中的SESSION知多少?

    作为一名WEB开发程序员,对session的理解是最基础的,但是现状是WEB程序员遍地都是,随便一划拉一大把,不过估计能把session能透彻理解的人应该不是很多,起码我之前对此是知之甚少,偶然看到的 ...

  2. 《Apache之虚拟主机的配置》——RHEL6.3

    1.安装httpd软件包: Yum install httpd 2.启动apache服务: [root@redhat Desktop]# /etc/init.d/httpd start Startin ...

  3. WordPress使用360CDN替换google服务,解决WordPress打开速度变慢问题

    由于wordpress新版本中默认使用了一些google服务及google字体,由于google在天朝中的一些你懂的原因,造成wordpress在打开时被拖慢,这时可以用360推出的公共库CDN服务替 ...

  4. c++11: bind用法

    原型: template< class R, class F, class... Args > bind( F&& f, Args&&... args ); ...

  5. EasyUI DataGrid分页数据绑定

    记录东西感觉很痛苦,总结东西很痛苦,麻烦,不过为了下次的方便和知识的牢固以后要坚持总结. EasyUI DataGrid分页数据绑定 在解决方案中新建两个文件FormMain.aspx(html也可以 ...

  6. Win7构造wifi热点【Written By KillerLegend】

    1:Win+R打开DOS,输入以下命令然后回车,不要关闭该DOS窗口: netsh wlan set hostednetwork  mode = allow ssid =你的wifi热点名字  key ...

  7. Translation001——android

    请尊重原创,转载请注明出处: Author:KillerLegend Link:http://www.cnblogs.com/killerlegend/ BEGIN****************** ...

  8. @RenderSection与@RenderBody

    _LayoutMain: <html> <head> @RenderSection("head") </head> <body> @ ...

  9. 用户登录密码RSA加密后传输的实现,非明文密码传输

    在用户登录页面,用户输入密码后,在传送到服务器端时,为防止在密码传送过程中,被如360这种东东给拦截到, 需要在传送前对密码进行加密,然后再传送! 利用RSA加密,在客户端使用公钥对密码进行加密,在服 ...

  10. Java中resourceBundle和Properties的区别

    第一种办法InputStream is = Test.class.getResourceAsStream("DbConfig.properties");Properties p = ...