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日的更多相关文章

  1. 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 ...

  2. HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4528 小明系列故事——捉迷藏 Time Limit: 500/200 MS (Java/O ...

  3. 2016年11月25日 星期五 --出埃及记 Exodus 20:16

    2016年11月25日 星期五 --出埃及记 Exodus 20:16 "You shall not give false testimony against your neighbor.不 ...

  4. 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 ...

  5. 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 ...

  6. jvm 之 国际酒店 6月25日上线内存溢出原因

    6月25日OMS,Ihotel上线成功后执行了一个批处理,SOA报警提示某一台IHOTEL机器调用OMS失败率大于阀值,登录这个机器后发现这台机器CPU使用率处于80%以上,调用OMS有的时候超过5秒 ...

  7. 天津Uber优步司机奖励政策(1月25日~1月31日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. 长沙Uber优步司机奖励政策(1月25日~1月31日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 西安Uber优步司机奖励政策(1月25日~1月31日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

随机推荐

  1. 左右滑动删除ListView条目Item--第三方开源--SwipeToDismiss

    Android的SwipeToDismiss是github上一个第三方开源框架(github上的项目链接地址:https://github.com/romannurik/Android-SwipeTo ...

  2. python杂记-6(time&datetime模块)

    #!/usr/bin/env python# -*- coding: utf-8 -*-import timeprint(time.clock())##返回处理器时间,3.3开始已废弃 , 改成了ti ...

  3. Nginx+uWSGI+Django原理

    Python的Web开发中,如果使用Django框架,那么较为成熟稳定的服务器架构一般是Nginx+uWSGI+Django.而为什么一定要三个结合在一起呢?直接使用Django的runserver来 ...

  4. windows32下安装zend framework2

    首先安装好php(5.3.3以上).apache和mysql apache 开启mod_rewrite 模块 将所有AllowOverride None设置为AllowOverride FileInf ...

  5. cdev_系列函数

    内核中每个字符设备都对应一个 cdev 结构的变量,下面是它的定义: linux-2.6.22/include/linux/cdev.h struct cdev {    struct kobject ...

  6. Java Day 05

    数组第二种定义 数组-遍历 数组操作的核心思想就是对角标的操作: 数组-求最值 1.循环 比较 排序 选择排序 把原始数组分割成了两个数组,至少有一个是有序的 冒泡排序 相邻元素比较 位置置换代码提取 ...

  7. iTween基础之Move(移动)

    1,五种移动方法:2, 函数的基础属性及用法 原文地址:http://blog.csdn.net/dingkun520wy/article/details/50476864 iTween官网:http ...

  8. Access

    一般系统的实现: 管理系统的分析与设计 --->>数据表的设计创建 --->> 设计“查询”与“宏” --->> 创建窗体与报表 --->>系统注册 启 ...

  9. learning from the previous teams

    开发人员水平有限.分配任务的时候经常有说这个事儿做不到,或者压根不知道怎么做:验收工作频出意外,DEV写了一个模块之后,验收的时候发现模块质量不行,代码质量低是其次,无法按照给定的接口工作.设计不足. ...

  10. FTP操作类(支持异步)

    public delegate void DownloadProgressChangedEventHandle(string information, long currentprogress, lo ...