17.1 swap a number in place.(without temporary variables)

a = a ^ b;

b = a ^ b;

a = a ^ b;

17.3 Write a function which computes the number of trailing zeros in n factorial.

To count the number of zeros, we only need to count the pairs of multiples of 5 and 2. There will always be more multiples of 2 than 5 though, so, simply counting the number of multiples of 5 is sufficient.

 public int count(int num){
int count = 0;
if(num < 0) return -1;
for(int i = 5; num /i > 0; i *= 5)
count += num / i;
return count;
}

17.4 Write a method which finds the maximum of two numbers. You should not use if-else or any other comparison operator.

 public int flip(int bit){
return 1 ^ bit;
}
public int sign(int a){
return flip((a >> 31) & 0x1);
}
public int getMax(int a, int b){
int c = a - b;
int sa = sign(a); //if a >= 0 : 1 other : 0
int sb = sign(b); //if b >= 1 : 1 other : 0
int sc = sign(c); //depends on whether a - b overflows, like a = INT_MAX, b < 0
int use_sign_a = sa ^ sb;
int use_sign_c = flip(sa ^ sb);
int k = use_sign_a * sa + use_sign_c * sc;
int q = flip(k);
return a * k + b * q;
}

17.9 Design a method to find the frequency of occurrences of any given word in a book.

The first question that you should ask is if you will be doing this operation once or repeatedly.

Solution: Single Query

go through the book, word by word, count the number of times that words appears. O(n)

Solution: Repetitive Queries

do pre-processing on the book! create a hash table which maps from a word to its frequency.

 Hashtable<String, Integer> setupDic(String[] book){
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
for(String word : book){
word = word.toLowerCase();
if(word.trim() != ""){
if(!table.containsKey(word)) table.put(word, 0);
table.put(word, table.get(word) + 1);
}
}
return table;
}
int getFrequency(Hashtable<String, Integer> table, String word){
if(table == null || word == null) return -1;
word = word.toLowerCase();
if(table.containsKey(word)) return table.get(word);
return 0;
}

17.11 Implement a method rand7() given rand5(). Given a method that generates a random number between 0 and 4, write a method that generates a random number between 0 and 6.

Nondeterministic Number of Calls

 public int rand7(){
while(true){
int num = 5 * rand5() + rand5();
if(num < 21) return num % 7;
}
}

Chp17: Moderate的更多相关文章

  1. Moderate 加入空格使得可辨别单词数量最多 @CareerCup

    递归题目,注意结合了memo的方法和trie的应用 package Moderate; import java.util.Hashtable; import CtCILibrary.AssortedM ...

  2. found 12 vulnerabilities (7 moderate, 5 high) run `npm audit fix` to fix them, or `npm audit` for details

    npm 安装包之后,如果出现类似下面的信息 found 12 vulnerabilities (7 moderate, 5 high) run `npm audit fix` to fix them, ...

  3. 题解——ATCoder AtCoder Grand Contest 017 B - Moderate Differences(数学,构造)

    题面 B - Moderate Differences Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Stat ...

  4. Atcoder B - Moderate Differences

    http://agc017.contest.atcoder.jp/tasks/agc017_b B - Moderate Differences Time limit : 2sec / Memory ...

  5. CCI_chapter 19 Moderate

    19 1  Write a function to swap a number in place without temporary variables void swap(int &a, i ...

  6. [图形学] Chp17 OpenGL光照和表面绘制函数

    这章学了基本光照模型,物体的显示受到以下效果影响:全局环境光,点光源(环境光漫反射分量,点光源漫反射分量,点光源镜面反射分量),材质系数(漫反射系数,镜面反射系数),自身发光,雾气效果等.其中点光源有 ...

  7. Atcoder | AT2665 【Moderate Differences】

    又是一道思路特别清奇的题qwq...(瞪了一上午才发现O(1)的结论...差点还想用O(n)解决) 问题可以转化为是否能够由\(f_{1}=a\)通过\(\pm x \in[c,d]\)得到\(f_{ ...

  8. Atcoder #017 agc017 B.Moderate Differences 思维

    LINK 题意:给出最左和最右两个数,要求往中间填n-2个数,使得相邻数间差的绝对值$∈[L,R]$ 思路:其实也是个水题,比赛中大脑宕机似的居然想要模拟构造一个数列,其实我们只要考虑作为结果的数,其 ...

  9. Fedora 24中的日志管理

    Introduction Log files are files that contain messages about the system, including the kernel, servi ...

随机推荐

  1. 10款基于jquery实现的超酷动画源码

    1.jQuery二级下拉菜单 下拉箭头翻转动画 之前我们分享过不少基于jQuery的二级下拉菜单,甚至是多级的下拉菜单,比如这款jQuery/CSS3飘带状多级下拉菜单就非常华丽.但今天要介绍的这款j ...

  2. struts2指定集合元素的泛型

    public class LoginAction implements Action{ private List users; public void setUsers(List users){ th ...

  3. Vs2010搭建directshow 环境

    一:材料 1, vs2010 2, winsdk7.1  http://www.microsoft.com/en-us/download/details.aspx?id=8442 更具自己电脑选择32 ...

  4. [Guava源码分析]ImmutableCollection:不可变集合

    摘要: 我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3888557.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的 ...

  5. Linux美化——终端提示符

    1. PS1变量简介[1] PS1是Linux终端用户的一个环境变量,用来说明命令行提示符的设置. 可以使用 man bash命令查看bash手册,找到该变量支持的特殊字符,以及这些特殊字符的意义: ...

  6. NSTimer定时器类

    NSTimer是Cocoa中比较常用的定时器类,基本操作如下: handleTimer方法可以自行定义.在需要的地方创建timer即可,handleTimer就可以每0.5秒执行一次.   - (vo ...

  7. 腾讯微博OAuth2.0 .NET4.0 SDK 发布以及网站腾讯微博登陆示例代码(原创)

    1.使用简单方便,包含详细注释: 2.暂时只支持xml格式字符串的转换,建议接口使用xml参数:3.QweiboSDK.Controllers命名空间下已包含所有API接口:4.只需调用到Qweibo ...

  8. TDirectory.GetDirectories 获取指定目录下的目录

    使用函数: System.IOUtils.TDirectory.GetDirectories 所有重载: class function GetDirectories(const Path: strin ...

  9. Ubuntu12.10编译openwrt遇到的错误

    由于Openwrt有很多工具是要先编译的,在Ubuntu12.10平台下编译openwrt时就遇到了下面这样的错误:elf.cpp: In static member function 'static ...

  10. Javascript Array.prototype.some()

    当我们使用数组时,查找数组中包含某个特殊的项是非常常见的动作.下面例子是一个简单的实现: 01 planets = [ 02     "mercury", 03     " ...