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 常踩的坑
原文链接: