[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 ...
随机推荐
- OS X EI Capitan安装mcrypt
OS X EI Capitan安装mcrypt (我的博客原文:http://www.jmolboy.com/2015/12/01/mcrypt-extension-on-EI-Capitan/) ...
- GitHub for Windows离线安装的方法
这几天一直在尝试安装GitHub for windows ,安装程序是从https://windows.github.com/ 下载到的OneClick 部署程序,版本号为2.11.0.5.可能是因为 ...
- 在Linux中三种让crontab每秒执行任务的方法
第一种方法: 1.创建脚本文件 cat phplog.sh 2.编辑脚本内容 #!/bin/bash while : ;do /home/scripts.sh 2>/dev/null & ...
- hadoop分布式安装过程
一.安装准备及环境说明 1.下载hadoop-1.2.1,地址:http://apache.spinellicreations.com/hadoop/common/stable/hadoop-1.2. ...
- Oracle 表的连接方式(2)-----HASH JOIN的基本机制3
HASH JOIN的模式 hash join有三种工作模式,分别是optimal模式,onepass模式和multipass模式,分别在v$sysstat里面有对应的统计信息: SQL> sel ...
- MvvmLight for Xamarin.Forms
一.Xamarin.Forms 不使用框架时的绑定 需要注意的是BindingContent,不是DataContent <ContentPage xmlns="http://xama ...
- ActiveMQ.xml文件的主要配置
ActiveMQ.xml文件默认位置位于 activemq/conf/目录下,主要的配置及解析如下:<beans xmlns="http://www.springframework.o ...
- Hive表分区
必须在表定义时创建partition a.单分区建表语句:create table day_table (id int, content string) partitioned by (dt stri ...
- python之类私有成员
python类的成员前加双下划线"__", 则被看作"私有"成员. 实例不能简单地通过<instance>.<name>来访问. 但py ...
- python之域与属性
python, javascript中域与属性是二个不同的概念, 域就是变量, 而属性则是符合某些约束, 例如getter, setter...等的特殊"变量". python中使 ...