19 1  Write a function to swap a number in place without temporary variables

void swap(int &a, int &b)
{
b = a - b; // 4 = 9 - 5
a = a - b; // 5 = 9 - 4
b = a + b; // 9 = 4 + 5
}
void swap(int & a, int &b)
{
a = a^b;
b = a^b;
a = a^b;
}

19.3 Write an algorithm which computes the number of trailing zeros in n factorial

详细分析见编程之美

int numZeros(int num)
{
int res = ;
while(num > ){
res+= num /;
num/=;
} return res;
}

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

EXAMPLE
Input: 5, 10
Output: 10

int getMax(int a, int b)
{
int c = a - b;
int k = (c>>) & 0x1;
return a - k * c;
}

19.5 The Game of Master Mind is played as follows:
The computer has four slots containing balls that are red (R), yellow (Y), green (G) or
blue (B) For example, the computer might have RGGB (e g , Slot #1 is red, Slots #2 and
#3 are green, Slot #4 is blue)
You, the user, are trying to guess the solution You might, for example, guess YRGB
When you guess the correct color for the correct slot, you get a “hit” If you guess
a color that exists but is in the wrong slot, you get a “pseudo-hit” For example, the
guess YRGB has 2 hits and one pseudo hit
For each guess, you are told the number of hits and pseudo-hits
Write a method that, given a guess and a solution, returns the number of hits and
pseudo hits

用hash存储计算机存储的个个slot值。

void estimate(String guess, String solution, int &hit, int &pesohit)
{
hit = ;
pesohit = ;
int hash = ;
for(int i = ; i< ; ++i)
hash |= <<(solution[i] - 'A'); for(int i = ; i< ;; ++i)
{
if(solution[i] == guess[i])
++hit;
else if( (<<(guess[i]-'A')) & hash >= )
++pesohit;
}
}

19.6 Given an integer between 0 and 999,999, print an English phrase that describes the  integer (eg, “One Thousand, Two Hundred and Thirty Four”)

代码略长,感觉面试不会出

19.7 You are given an array of integers (both positive and negative) Find the continuous  sequence with the largest sum Return the sum

EXAMPLE
Input: {2, -8, 3, -2, 4, -10}
Output: 5 (i e , {3, -2, 4} )

http://www.cnblogs.com/graph/archive/2013/05/23/3095831.html

19 8  Design a method to fnd the frequency of occurrences of any given word in a book

分清查询一次和多次的区别

19.9

19 10 Write a method to generate a random number between 1 and 7, given a method
that generates a random number between 1 and 5 (i e , implement rand7() using
rand5())

int rand7()
{
while(true)
{
int res = rand5() - + * (rand5() - );
if(res < )
{
return res% + ;
}
}
}

19.11 Design an algorithm to find all pairs of integers within an array which sum to a speci-
fed value

void printPairSums(vector<int> array, int sum)
{
sort(array.begin(), array,end());
int first = 0;
int last = array.size()-1;
while(firat <= last)
{
int temp = array[first] + array[last];
if(temp == sum){
cout<<array[first]<<" "<<array[last]<<endl;
}else if(temp < sum){
++first;
}else{
--last;
}
}
}

  

CCI_chapter 19 Moderate的更多相关文章

  1. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  2. Mediaplayer error (-19,0)

    Android MediaPlayer 发生 error (-19,0) 错误解决方法. 引起原因:由于多次实例化MediaPlayer.start() 进行播放操作引起的.由于没有及时释放内存资源导 ...

  3. 录像时调用MediaRecorder的start()时发生start failed: -19错误

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  4. CSharpGL(19)用glReadPixels把渲染的内容保存为PNG图片(C#)

    CSharpGL(19)用glReadPixels把渲染的内容保存为PNG图片(C#) 效果图 本文解决了将OpenGL渲染出来的内容保存到PNG图片的方法. 下载 CSharpGL已在GitHub开 ...

  5. ABP(现代ASP.NET样板开发框架)系列之19、ABP应用层——审计日志

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之19.ABP应用层——审计日志 ABP是“ASP.NET Boilerplate Project (ASP.NET ...

  6. js正则表达式校验非负浮点数:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. js正则表达式校验非负整数:^\d+$ 或 ^[1-9]\d*|0$

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. js正则表达式校验非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

    我们知道,企业号主要是面向企业需求而生的,因此内部消息的交流显得非常重要,而且发送.回复消息数量应该很可观,对于大企业尤其如此,因此可以结合企业号实现内部消息的交流.企业号具有关注安全.消息无限制等特 ...

随机推荐

  1. 几种开源SIP协议栈对比OPAL,VOCAL,sipX,ReSIProcate,oSIP

    随着VoIP和NGN技术的发展,H.323时代即将过渡到SIP时代,在H.323的开源协议栈中,Openh323占统治地位,它把一个复杂而又先进 的H.323协议栈展现在普通程序员的眼前,为H.323 ...

  2. Qt入门(3)——信号和槽

    信号和槽用于对象间的通讯.信号/槽机制是Qt的一个中心特征并且也许是Qt与其它工具包的最不相同的部分.在图形用户界面编程中,我们经常希望一个窗口部件的一个变化被通知给另一个窗口部件.更一般地,我们希望 ...

  3. MD中bitmap源代码分析--设置流程

    1. 同步/异步刷磁盘 Bitmap文件写磁盘分同步和异步两种: 1) 同步置位:当盘阵有写请求时,对应的bitmap文件相应bit被置位,bitmap内存页被设置了DIRTY标志.而在下发写请求给磁 ...

  4. VS2010旗舰版安装图解

    微软公布了最新的 Visual Studio 2010 软件开发编程平台及 .Net Framework 4 框架.这次 VisualStudio 2010 包含 Professional 专业版.P ...

  5. Ftp实现文件同步

    通常在做服务器与服务器文件.服务器与本地文件同步时通过Ftp服务实现,下面就以服务器文件和本地同步为例,介绍一下Ftp同步文件:首先建立一个Ftp站点服务,基本身份验证登陆,端口号为默认的21:Ftp ...

  6. Java基础知识强化52:经典排序之冒泡排序(BubbleSort)

    1. 冒泡排序的原理图: 2. 冒泡排序代码实现: package cn.itcast_01; /* * 数组排序之冒泡排序: * 相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处 ...

  7. Pythn中的super用法

    在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的写法如代码段1: 代码段1: 代码如下: class A: def __init__(self): p ...

  8. params关键字载入空值的陷阱

    在编写方法时,不确定这个方法要传入多少个参数,或者随着程序的开发速度,该方法的参数会发生很大变化,在C#中引入了params关键字,使用params修饰object类型的数组并作为方法的参数类型,可以 ...

  9. (一)《Java编程思想》学习——按位运算符、移位运算符

    (第三章) (一)按位运算符 按位逻辑运算符有: “与”(AND)        & 1&1=1;1&0=0;0&0=0 “或”(OR) | 1|1=1;1|0=1;0 ...

  10. (转)强大的JQuery表单验证插件 FormValidator使用介绍

    jQuery formValidator表单验证插件是客户端表单验证插件.在做B/S开发的时候,我们经常涉及到很多表单验证,例如新用户注册,填写个人资料,录入一些常规数据等等.在这之前,页面开发者(J ...