[mock]7月25日
1. 将一个数组分成左右两部分,使得右边的某个连续子段和减去左边的某个连续字段和最小[7,8,9,|3,5,-1] sum right - sum left minimal
想到左右分一刀,O(n),然后对左右分别取最大字段和算法,这样是O(n^2)。但是其实左右各扫一遍,然后记录下来就行了。
int minimaldif(vector<int> &a){
vector<int> max(a.size());
int sum = max[0] = a[0];
for (int i = 1; i < a.size(); i++)
{
if (sum < 0) sum = 0;
sum = sum + a[i];
if (sum > max[i-1])
max[i] = sum;
else
max[i] = max[i-1];
}
// min 数组(略)
int result = a[a.size() - 1] - a[0];
// i is the start of right section
for (int i = 1; i < a.size(); i++)
{
int maxVal = max[i];
int minVal = min[i];
if (minVal - maxVal < result)
{
result = minVal - maxVal;
}
}
return result;
}
2. 有若干0和1组成的等长字符串,求所有字符串相互的海明距离之和。
比如:“001000” “110000” “010111”
ham("001000","110000") = 3
ham("001000","010111") = 5
ham(“110000”,“010111”) = "4
那么结果为12
这道题的关键在于各个位之间其实没有关系,如果字符串长度为1,比如4个“1”和2个“0”,那么结果就是4*2==8。n位的就是把n位的结果相加就行了。
int sumofHamdistance(vector<string> &a) {
int result = 0;
int len = a[0].length();
for (int i = 0; i < len; i++)
{
int count1 = 0;
int count0 = 0;
for (int j = 0; j < a.size(); j++)
{
if (a[j][i] == '0') count0++;
else if (a[j][i] == '1') count1++;
}
result += count1 * count0;
}
return result;
}
3. 有两个整数x和y,问这两个整数是否有相同的质因数。比如:
samePrimefactor(68 ,17) = false 68 = 2 * 2 * 17 17 = 17
samePrimefactor(136, 578) = true 136 = 2 * 2 * 2 * 17, 578 = 2 * 17 * 17
这道题可以用GCD来做,还需要研究一下。
int gcd(int x,int y) {
return y?gcd(y, x % y):x;
}
bool can(int a,int g) {
while (g > 1) {
if (g % a == 0) {
return true;
}
g = gcd(g, a);
a /= g;
}
return (a == 1);
}
int solution(vector<int> &A, vector<int> &B) {
// write your code in C++98
int n = A.size(), result = 0;
for (int i = 0; i < n; ++i) {
int g = gcd(A[i], B[i]);
if (can(A[i] / g, g) && can(B[i] / g, g)) {
++result;
}
}
return result;
}
发现的缺点一是括号的风格不一致,二是边界条件老是弄错,差个一。
[mock]7月25日的更多相关文章
- 2016年12月25日 星期日 --出埃及记 Exodus 21:20
2016年12月25日 星期日 --出埃及记 Exodus 21:20 "If a man beats his male or female slave with a rod and the ...
- HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4528 小明系列故事——捉迷藏 Time Limit: 500/200 MS (Java/O ...
- 2016年11月25日 星期五 --出埃及记 Exodus 20:16
2016年11月25日 星期五 --出埃及记 Exodus 20:16 "You shall not give false testimony against your neighbor.不 ...
- 2016年10月25日 星期二 --出埃及记 Exodus 19:9
2016年10月25日 星期二 --出埃及记 Exodus 19:9 The LORD said to Moses, "I am going to come to you in a dens ...
- 2016年6月25日 星期六 --出埃及记 Exodus 14:22
2016年6月25日 星期六 --出埃及记 Exodus 14:22 and the Israelites went through the sea on dry ground, with a wal ...
- jvm 之 国际酒店 6月25日上线内存溢出原因
6月25日OMS,Ihotel上线成功后执行了一个批处理,SOA报警提示某一台IHOTEL机器调用OMS失败率大于阀值,登录这个机器后发现这台机器CPU使用率处于80%以上,调用OMS有的时候超过5秒 ...
- 天津Uber优步司机奖励政策(1月25日~1月31日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 长沙Uber优步司机奖励政策(1月25日~1月31日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 西安Uber优步司机奖励政策(1月25日~1月31日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
随机推荐
- Unity3d 动态批处理的问题
这段时间做unity3d的优化,主要的入手是减少draw call. 1.代码上主要是把一些零碎的同材质的合并成一个大的mesh. 2.减少不必要的全屏后期处理.把摄像机的renderin ...
- jQuery实现用户注册的表单验证
用户注册的表单往往是需要进行验证的,否则会有一些不否合规则的数据入库,后果会不堪设想,本文通过jquery来实现. <html> <head> <meta chars ...
- (IIS8/8.5/Apache)301域名重定向
用Apache的.htaccess来做301域名转向1.开启apache支持.htaccess,方法:在Apache的配置文件httpd.conf中,找到<Directory /> ...
- 【转】Linux Framebuffer
全面的framebuffer详解 一.FrameBuffer的原理 FrameBuffer 是出现在 2.2.xx 内核当中的一种驱动程序接口. Linux是工作在保护模式下,所以用户态进程是无法象D ...
- springMVC之事务配置(问题来源:为什么数据保存不了)
参考文章:http://www.cnblogs.com/leiOOlei/p/3725911.html 自己的亲身体会,来源问题this.sessionFactory.getCurrentSessio ...
- MySQL 多实例数据库还原脚本-备份集与端口对应
版本:5.5.14 OS: ConetOS 6.3 1.创建recover.sh [root@yoon export]# vi recover.sh #!/bin/bash bakdir=/exp ...
- Python 学习教程
<Core Python Programming>勘误参考表 http://starship.python.net/crew/wesc/cpp/errata2.htm 笨办法学 Pytho ...
- MVC学习系列——记一次失败面试后,感想。
在此写博客之际,热烈庆祝母校苏州科技学院,正式改名为苏州科技大学. 一晃眼,从自己投身IT行业已经两年有余,期间经历了结婚.买房等人生大事,非常感谢我的老婆,谢谢她这么爱我,嫁给我这个码农,呵呵... ...
- C++中的仿函数,std::function和bind()的用法
1.仿函数:又叫std::function,是C++中的一个模板类 2.C语言中的函数指针: int add(int a,int b) { return a+b; } typedef int (*f ...
- web服务器和应用服务器
通俗的讲,Web服务器传送(serves)页面使浏览器可以浏览,然而应用程序服务器提供的是客户端应用程序可以调用(call)的方法(methods).确切一点,你可以说:Web服务器专门处理HTTP请 ...