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. 在ArcGIS中WGS84大地坐标和投影平面坐标的转换

    以WGS84转换为北京54坐标为例: 首先你要先知道转化的参数,鉴于我国曾使用不同的坐标基准(BJ54.State80.Correct54),各地的重力值又有很大差异,所以很难确定一套适合全国且精度较 ...

  2. windows store app 读写图片

    using System; using System.Threading.Tasks; using System.Runtime.InteropServices.WindowsRuntime; usi ...

  3. 使用awstats分析iis站点的日志

    环境:win7 + iis7 + perl(ActivePerl-5.20.1.2000) + awstats 7.3 一.找到iis日志所在目录 建议全部都打勾 二.安装perl AWStats是p ...

  4. thymeleaf 局部变量、属性优先级、注释

    九.局部变量(local variable) 之前在th:each中遇到过局部变量 <tr th:each="prod : ${prods}"> ... </tr ...

  5. WCF全面解析第二章 地址(Adress)

    2.1 统一资源标识(URL) 2.1.1 Http/Https 2.1.2 Net.TCP 2.1.3 Net.Pipe WCF只将命名管道专门用于同一台机器的跨进程通信. 2.1.4 Net.Ms ...

  6. [转]DataGridView绑定泛型List的种种

    1.DataGridView数据绑定对比(DataTable与泛型List):当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时,DataGri ...

  7. Arrays.asList的使用及异常问题

    将数组转成List问题,通常我们习惯这样写成:List<String> list = Arrays.asList("1","2"); 于是我们这样就 ...

  8. usb wifi driver run in ubuntu support 360/xiaodu and with 3.13.0-32-generic

    (为了实现usb-wifi用在linux系统上,需求解决方案,过程记录和如何实现) 重点解决3.13.0-32-generic内核编译 mt7601 usb wifi 驱动问题. 1:首先下载MT76 ...

  9. mysql5.5 Replication 主从同步

    mysql5.5 Replication 主从同步 ------------------[主]------------------[mysqld]server-id=1 log-bin=mysql-b ...

  10. 用于主题检测的临时日志(d94169f9-f1c0-45a2-82d4-6edc4bd35539 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

    这是一个未删除的临时日志.请手动删除它.(5327dce0-d2d1-4fba-8801-d3ff67564a96 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)