Algorithm : 做一个 leetcode 的算法题

题目:一个无序数组里有99个不重复正整数,范围从1到100,唯独缺少一个整数。如何找出这个缺失的整数?

int FindOneMissNum(int aiArray[], int iNum)
{
int iMissNum = -1;
#if 0
// 方法一:利用数组的下标
// 时间复杂度O(n) 空间复杂度O(n)
vector<int> vect(iNum + 1, 0);
for (int i = 0; i < iNum; i++)
{
vect[aiArray[i] - 1]++;
} for (int i = 0; i < iNum; i++)
{
if (0 == vect[i])
{
iMissNum = i + 1;
break;
}
} //#else // 方法二:先排序,然后比较相邻的两个数差值是否大于1
//时间复杂度:O(N*lgn),空间复杂为O(1)
std::sort(aiArray, aiArray + iNum);
for (int i = 0; i < iNum - 1; i++)
{
if (aiArray[i + 1] - aiArray[i] > 1)
{
iMissNum = aiArray[i + 1] - 1;
break;
}
} #else
// 方法三: 先累加1+2+3+...+100;然后建去数组里面的值,差值就是要查找的数
//时间复杂度O(n), 空间复杂度O(1)
//这里注意一下:如果给的数值不是1...100,需要注意不能溢出,所以这种方法慎用!!!!
int iSum = (1 + 100) * (100 / 2);
for (int i = 0; i < iNum; i++)
{
iSum -= aiArray[i];
}
iMissNum = iSum; #endif printf("MissNum = %2d\n", iMissNum);
return iMissNum;
}

题目扩展:一个无序数组里有若干个正整数,范围从1到100,其中99个整数都出现了偶数次,只有一个整数出现了奇数次(比如1, 1, 2, 2, 3, 3, 4, 5, 5),如何找到这个出现奇数次的整数?

int FindOddNum(int aiArray[], int iNum)
{
int iOddNum = 0;
#if 0
//方法一:利用数组下标
// 时间复杂度O(n), 空间复杂度O(n)
vector<int> vect(iNum + 1, 0);
for (int i = 0; i < iNum; i++)
{
vect[aiArray[i] - 1]++;
} for (int i = 0; i < iNum; i++)
{
if (vect[i] == 1)
{
iOddNum = i + 1;
break;
}
} #else
//方法二:用异或(^=) 相同为假,不同为真
// 时间复杂度O(n), 空间复杂度O(1)
iOddNum ^= aiArray[0];
for (int i = 1; i < iNum; i++)
{
iOddNum ^= aiArray[i];
} #endif
printf("OddNum = %2d\n", iOddNum);
return iOddNum;
}

题目第二次扩展:一个无序数组里有若干个正整数,范围从1到100,其中98个整数都出现了偶数次,只有两个整数出现了奇数次(比如1, 1, 2, 2, 3, 4, 5, 5),如何找到这个出现奇数次的整数?

解题思路:
     1.遍历整个数组,依次做异或运算。由于数组存在两个出现奇数次的整数,所以最终异或的结果,等同于这两个整数的异或结果。
         这个结果中,至少会有一个二进制位是1(如果都是0,说明两个数相等,和题目不符)

2.根据这个结论,我们可以把原数组按照二进制的末位不同,分成两部分,一部分的末位是1,一部分的末位是0。由于A和B的末位不同,
         所以A在其中一部分,B在其中一部分,绝不会出现A和B在同一部分,另一部分没有的情况。
     3.这样一来就简单了,我们的问题又回归到了上一题的情况,按照原先的异或解法,从每一部分中找出唯一的奇数次整数即可。



//时间复杂度:O(N), 空间复杂度O(1)

int FindTwoOddNum(int aiArray[], int iNum)
{
// 1.计算两个数异或值
int iBitStatus = 0;
for (int i = 0; i < iNum; i++)
{
iBitStatus ^= aiArray[i];
} // 2.找到为1的bit位
int iBit = 0;
while (1)
{
if (iBitStatus & (0x01 << iBit))
{
break;
}
iBit++;
} int iOddNum = 0;
int iOddNum2 = 0;
for (int i = 0; i < iNum; i++)
{
if (aiArray[i] & (0x01 << iBit))
{
iOddNum ^= aiArray[i];
}
else
{
iOddNum2 ^= aiArray[i];
}
} printf("OddNum = %2d, iOddNum2 = %02d\n", iOddNum, iOddNum2);
return iOddNum;
}

Review : 阅读并点评一篇英文技术文章

原文地址:http://download.redis.io/redis-stable/redis.conf

# Redis configuration file example.
#
# Note that in order to read the configuration file, Redis must be
# started with the file path as first argument:
#
# ./redis-server /path/to/redis.conf

# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# units are case insensitive so 1GB 1Gb 1gB are all the same.

################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised no

# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis_6379.pid

# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile ""

# To enable logging to the system logger, just set 'syslog-enabled' to yes,
# and optionally update the other syslog parameters to suit your needs.
# syslog-enabled no

# Specify the syslog identity.
# syslog-ident redis

# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.
# syslog-facility local0

# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16

# By default Redis shows an ASCII art logo only when started to log to the
# standard output and if the standard output is a TTY. Basically this means
# that normally a logo is displayed only in interactive sessions.
#
# However it is possible to force the pre-4.0 behavior and always show a
# ASCII art logo in startup logs by setting the following option to yes.
always-show-logo yes

Redis配置文件:

通用配置:

1.是否以守护进程运行redis;

2.是否打开监督模式;

3.守护进程模式下,设置pid文件目录;

4.设置日志等级(debug/verbose/notice/warning);

5.设置log文件路径,如果为"",表示输出到标准输出;

6.系统日志开关;

7.设置系统日志标记;

8.指定系统日志工具;

9.设置数据库个数;

10.显示logo;

Tips : 学习一个技术技巧

C++中有哪4个与类型转换相关的关键字?这些关键字各有什么特点,应该在什么场合下使用?

类型转换总结:

  • 去const/volatile属性使用const_cast;

  • 基本类型之间转换使用static_cast;

  • 多态类型之间转换使用dynamic_cast;

  • 不同类型的指针之间转换使用reinterpret_cast;

Share : 分享一篇有观点和思考的技术文章

c++ new delete 常踩的坑

原文链接:

https://wetest.qq.com/lab/view/318.html?from=adsout_qqtips_past2_318&sessionUserType=BFT.PARAMS.230036.TASKID&ADUIN=664510732&ADSESSION=1501034659&ADTAG=CLIENT.QQ.5533_.0&ADPUBNO=26719

ARST第二周打卡的更多相关文章

  1. 201521123093 java 第二周学习总结

    201521123093 <java程序设计> 第二周学习总结 一.第二周学习总结 答:(1)关于进一步使用码云管理代码,本周才真正学会了如何将Eclipse里的代码上传到码云中,并且能够 ...

  2. 2017-2018-1 Java演绎法 第二周 作业

    团队任务:讨论Android上的游戏软件 参考现代软件工程 第一章 [概论]练习与讨论: 软件有很多种,也有各种分类办法,本次团队任务是讨论选取Android上的一个游戏软件,考虑到每位组员接触的游戏 ...

  3. 2017-2018-1 Java小组-1623 第二周作业

    2017-2018-1 Java小组-1623 第二周作业 关于游戏软件的问题 讨论结果 20162301张师瑜 20162305李昱兴 20162306陈是奇 20162308马平川 2016231 ...

  4. 三节课MINI计划第二周

    任务:完成一份用户反馈的收集,并进行分析 第一步:去你能想到的公开.非公开渠道收集最近90天,至少40条和B站相关的有效用户差评反馈,并根据你对业务的理解分类整理,以表格的形式进行整理,以图片的方式提 ...

  5. Surprise团队第二周项目总结

    Surprise团队第二周项目总结 项目进展 已实现五子棋人人模式部分 人人模式: 基本方式:采取黑棋先行,黑白交替的下棋顺序. 模式:通过鼠标点击相应棋盘中的"交叉点",在lay ...

  6. python课程第二周重点记录

    python课程第二周重点记录 1.元组的元素不可被修改,元组的元素的元素可以被修改(字典在元组中,字典的值可以被修改) 2.个人感觉方便做加密解密 3.一些方法的使用 sb = "name ...

  7. 20145213《Java程序设计》第二周学习总结

    20145213<Java程序设计>第二周学习总结 教材学习内容总结 本周娄老师给的任务是学习教材的第三章--基础语法.其实我觉得还蛮轻松的,因为在翻开厚重的书本,一股熟悉的气息扑面而来, ...

  8. 20145304 刘钦令 Java程序设计第二周学习总结

    20145304 <Java程序设计>第2周学习总结 教材学习内容总结 java可区分基本类型和类类型(即参考类型)两大类型系统. 基本类型主要可区分为整数.字节.浮点数.字符与布尔. 整 ...

  9. 20145330孙文馨 《Java程序设计》第二周学习总结

    20145330孙文馨第二周学习总结 第二周相比于第一周对java语言有了深一点的了解,也意识到多敲代码才是学习计算机语言的最好方法. 教材内容总结 类型.变量与运算符 *基本类型 整数(short. ...

随机推荐

  1. 【线性代数】4-2:投影(Porjections)

    title: [线性代数]4-2:投影(Porjections) categories: Mathematic Linear Algebra keywords: Projections Project ...

  2. 二维DFT

    学习DIP第4天 傅里叶变换数学原理会在后续完整介绍,目前只实现代码,观察下结果,公式在上一篇博客中已经描述 内容迁移至 http://www.face2ai.com/DIP-2-2-二维DFT/ h ...

  3. node中的crypto内置模块

    crypto模块的目的是为了提供通用的加密和哈希算法.用纯JavaScript代码实现这些功能不是不可能,但速度会非常慢.Nodejs用C/C++实现这些算法后,通过cypto这个模块暴露为JavaS ...

  4. 黑马vue---16、vue中通过属性绑定为元素设置class类样式

    黑马vue---16.vue中通过属性绑定为元素设置class类样式 一.总结 一句话总结: 这里就是为元素绑定class样式,和后面的style样式区别一下 vue中class样式绑定方式的相对于原 ...

  5. nginx 错误集锦

    1)下载地址 http://nginx.org/en/download.html 找windows的下载. 2)然后解压到自己的一个目录. 3)配置环境变量,将解压到的路径加进去. 4)修改配置文件 ...

  6. JDBC与ODBC

     ODBC(Open Database Connectivity)是一组对数据库访问的标准API,这些API通过SQL来完成大部分任务,而且它本身也支持SQL语言,支持用户发来的SQL.ODBC定义了 ...

  7. 解决 无法启动此程序,因为计算机中丢失opencv_world341.dll。请尝试重新安装改程序已解决此问题

    在运行OpenCV程序时报错:“无法启动此程序,因为计算机中丢失opencv_world341.dll.请尝试重新安装改程序已解决此问题”. 解决方法 我的bin目录是 D:\opencv\build ...

  8. 前端知识点回顾之重点篇——jQuery实现的原理

    jQuery jQuery的实现原理 参考:https://blog.csdn.net/zhouziyu2011/article/details/70256659 外层沙箱和命名空间$ 为了避免声明了 ...

  9. 【SR汇总】算法时间效率

    1.SRCNN-0.39s SRCNN处理速度. 论文:Learning a Deep Convolutional Network forImage Super-Resolution 中,4.2节. ...

  10. vscode如何右键选择浏览器运行html文件

    我们利用Vscode软件编写html的时候,一般都想右键选择html文件,然后直接选择浏览器运行,但是默认是没有的: 下面鄙人给大家分享一下如何设置. 这里你会发现有好多类似的插件,你自己选择一个喜欢 ...