CCI_chapter 19 Moderate
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的更多相关文章
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- Mediaplayer error (-19,0)
Android MediaPlayer 发生 error (-19,0) 错误解决方法. 引起原因:由于多次实例化MediaPlayer.start() 进行播放操作引起的.由于没有及时释放内存资源导 ...
- 录像时调用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 ...
- CSharpGL(19)用glReadPixels把渲染的内容保存为PNG图片(C#)
CSharpGL(19)用glReadPixels把渲染的内容保存为PNG图片(C#) 效果图 本文解决了将OpenGL渲染出来的内容保存到PNG图片的方法. 下载 CSharpGL已在GitHub开 ...
- ABP(现代ASP.NET样板开发框架)系列之19、ABP应用层——审计日志
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之19.ABP应用层——审计日志 ABP是“ASP.NET Boilerplate Project (ASP.NET ...
- 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/ ...
- js正则表达式校验非负整数:^\d+$ 或 ^[1-9]\d*|0$
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 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/ ...
- C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)
我们知道,企业号主要是面向企业需求而生的,因此内部消息的交流显得非常重要,而且发送.回复消息数量应该很可观,对于大企业尤其如此,因此可以结合企业号实现内部消息的交流.企业号具有关注安全.消息无限制等特 ...
随机推荐
- HDOJ 1393 Weird Clock(明白题意就简单了)
Problem Description A weird clock marked from 0 to 59 has only a minute hand. It won't move until a ...
- jzp线性筛及其简单应用
前言: 很久以前看过了线性筛,没怎么注意原理,但是后来发现线性筛还有很有用的.. 比如上次做的一道题就需要找出每个数的最小质因子,先筛再找就太慢了..一看线性筛发现就可以直接在筛的过程中处理出来了! ...
- B树——思路、及C语言代码的实现
0.序 本人现读本科大二,这学期学习数据结构,老师为我们的期末作业布置一道任选题,而我一直以来都有听说B树是一棵挺神奇的树,所以我选择了它,当然更重要的原因是因为B树的难度最高,我喜欢做有挑战性的工作 ...
- 使用strace查看C语言级别的php源码
XCACHE XCache 是一个开源的 opcode 缓存器/优化器, 这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接 ...
- Engineering Economics
相关简介 工程经济学 –-研究各种技术在使用过程中如何以最小的投入获得预期产出或者说如何以等量的投入获得最大的产出: –-如何用最低的寿命周期成本实现产品.作业以及服务的必要功能. 软件工程经济学 工 ...
- AvalonEdit 移除自身ScrollViewer (可配合外部自定义ScrollViewer 使用)
http://community.sharpdevelop.net/forums/p/11977/42764.aspx#42764 1: <Style TargetType="{x:T ...
- 使用GCM服务(Google Cloud Messaging)实现Android消息推送
最近在网上查了关于很多Android消息推送的资料,其中主要有四种方法. 1) 使用GCM服务(Google Cloud Messaging) 2) 使用XMPP协议(Openfire + Spark ...
- nginx 配置正向 HTTP 代理服务器[转]
如果不想写到 ngnix.conf 中,那么可以在相同的目录下建立另外一个文件夹存放单独的文件,比如新建一个 proxy 的子目录,然后再在里面新建文件 prox.conf ,然后添加如下内容: s ...
- 魅蓝Note2跑分 MT6753性能究竟如何
MT6753实力究竟如何? 采用LP工艺的MT6753实际上在性能和功耗方面并不比MT6752高,相反,同频下功耗要高1/3左右.并且其内存带宽是5.3G/s,小于MT 6752的6.4G/s 而且没 ...
- Android 属性动画(一)
1.概述 Android提供了几种动画类型:View Animation .Drawable Animation .Property Animation .View Animation相当简单,不过只 ...