ARST第二周打卡
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 常踩的坑
原文链接:
ARST第二周打卡的更多相关文章
- 201521123093 java 第二周学习总结
201521123093 <java程序设计> 第二周学习总结 一.第二周学习总结 答:(1)关于进一步使用码云管理代码,本周才真正学会了如何将Eclipse里的代码上传到码云中,并且能够 ...
- 2017-2018-1 Java演绎法 第二周 作业
团队任务:讨论Android上的游戏软件 参考现代软件工程 第一章 [概论]练习与讨论: 软件有很多种,也有各种分类办法,本次团队任务是讨论选取Android上的一个游戏软件,考虑到每位组员接触的游戏 ...
- 2017-2018-1 Java小组-1623 第二周作业
2017-2018-1 Java小组-1623 第二周作业 关于游戏软件的问题 讨论结果 20162301张师瑜 20162305李昱兴 20162306陈是奇 20162308马平川 2016231 ...
- 三节课MINI计划第二周
任务:完成一份用户反馈的收集,并进行分析 第一步:去你能想到的公开.非公开渠道收集最近90天,至少40条和B站相关的有效用户差评反馈,并根据你对业务的理解分类整理,以表格的形式进行整理,以图片的方式提 ...
- Surprise团队第二周项目总结
Surprise团队第二周项目总结 项目进展 已实现五子棋人人模式部分 人人模式: 基本方式:采取黑棋先行,黑白交替的下棋顺序. 模式:通过鼠标点击相应棋盘中的"交叉点",在lay ...
- python课程第二周重点记录
python课程第二周重点记录 1.元组的元素不可被修改,元组的元素的元素可以被修改(字典在元组中,字典的值可以被修改) 2.个人感觉方便做加密解密 3.一些方法的使用 sb = "name ...
- 20145213《Java程序设计》第二周学习总结
20145213<Java程序设计>第二周学习总结 教材学习内容总结 本周娄老师给的任务是学习教材的第三章--基础语法.其实我觉得还蛮轻松的,因为在翻开厚重的书本,一股熟悉的气息扑面而来, ...
- 20145304 刘钦令 Java程序设计第二周学习总结
20145304 <Java程序设计>第2周学习总结 教材学习内容总结 java可区分基本类型和类类型(即参考类型)两大类型系统. 基本类型主要可区分为整数.字节.浮点数.字符与布尔. 整 ...
- 20145330孙文馨 《Java程序设计》第二周学习总结
20145330孙文馨第二周学习总结 第二周相比于第一周对java语言有了深一点的了解,也意识到多敲代码才是学习计算机语言的最好方法. 教材内容总结 类型.变量与运算符 *基本类型 整数(short. ...
随机推荐
- thinkphp6下载安装与配置图文详细讲解教程(composer下载安装)
thinkphp6发布也有一段时间了,相对来说比较稳定,是时候学习一下thinkphp6框架,提前学习,到正式发布的时候,可以直接拿来做正式的项目,先人一步.thinkPHP6.0在5.1的基础上对底 ...
- Python实用黑科技——解包元素(1)
需求: 很多时候手上已经有了一个具有n个元素的列表或者元组,你打算把这些元素单独取出来(解包)放入n个变量组成的集合(这里的集合和Python自己的set不同)中. 方法: 显然,最好的办法就是直接用 ...
- MySQL安装及基础命令
介绍数据库安装基础命令 linux的下载和安装 mac的下载和安装 windows的下载和安装 介绍: 数据库在开发中占据的位置? 数据库能更简单的使用存储在文件中的数据能更好的解决并发问题,数据统一 ...
- 利用Apache shiro SimpleHash 加密字符串
1.导入包 import org.apache.shiro.crypto.hash.SimpleHash; 1 2.代码 import org.apache.shiro.crypto.hash.Sim ...
- myeclipse使用SVN分支与合并详解
此博文主要内容来源地址:https://blog.csdn.net/liuyifeng1920/article/details/53118183,感谢原创博主: 先介绍一下svn的两种开发和发布的规范 ...
- spring boot 下 开启 gzip
[参考文章]:Spring boot开启Gzip压缩 [参考文章]:Accept-Encoding Spring 版本 :5.1.2-RELEASE 1. application.yml 配置 ser ...
- cassandra3.11.4集群搭建
环境:[centos7.cassandra-3.11.4] 三个节点:[主机名为master,slave-1,slave-2, 用户均为root] 1.下载cassandra cassandra下载地 ...
- 树形dp(灯与街道)
https://cn.vjudge.net/contest/260665#problem/E 题意: 给你一个n个点m条边的无向无环图,在尽量少的节点上放灯,使得所有边都被照亮.每盏灯将照亮以它为一个 ...
- 2.JSON.stringify()Object
JSON.stringify() JSON 通常用于与服务端交换数据. 在向服务器发送数据时一般是字符串. 我们可以使用 JSON.stringify() 方法将 JavaScript 对象转换为字符 ...
- 提高 python 效率的一些细节方式
在列表里面计数 性能:第二种计数方法比第一种快6290倍,为啥因为Python原生的内置函数都是优化过的,所以能用原生的计算的时候,尽量用原生的函数来计算. 过滤一个列表 性能:第二种方法比第一种慢近 ...