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. ...
随机推荐
- codeforces708C
CF708C Centroids 题意翻译 给定一颗树,你有一次将树改造的机会,改造的意思是删去一条边,再加入一条边,保证改造后还是一棵树. 请问有多少点可以通过改造,成为这颗树的中心?(如果以某个点 ...
- 用java写一个两个任意长度字符串数字和的算法
package com.cn.test.string; public class StringTest { public static void main(String[] args) { Strin ...
- python 查看源代码
使用包inspect import inspect from mxnet import contrib 查看模块所在路径: inspect.getsourcefile(contrib) 查看源码: i ...
- Vue.js 生命周期、计算属性及侦听器
一.创建一个Vue实例 每个Vue应用都是使用Vue函数创建一个Vue实例.所有的Vue组件都是一个Vue实例,并且接受相同的选项对象(一些根实例特有的选项除外). 数据和方法 当一个实例被创建后,它 ...
- qt 元对象系统
元对象系统 Qt中的元对象系统是用来处理对象间通讯的信号/槽机制.运行时的类型信息和 动态属性系统. 它基于下列三类: QObject类: 类声明中的私有段中的Q_OBJECT宏: 元对象编译器(mo ...
- 微PE:装机不求人,教你制作最纯净的PE安装系统
https://www.jianshu.com/p/50fd699ea916 超好用的PE工具,免费.纯净.无广告,装系统必备! https://www.jianshu.com/p/fecf090b2 ...
- SpringCloud(一)之微服务核心组件Eureka(注册中心)的介绍和使用
一 Eureka服务治理体系1.1 服务治理服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现. Spring Cloud Eureka是Spring Clou ...
- react中使用map时onClick事件失效
分享一些踩过的坑 react中使用map时onClick事件失效 <span> { count.map( (item,index)=>{ return <span style= ...
- WebLogic服务器
WebLogic是美国Oracle公司出品的一个application server确切的说是一个基于JAVAEE架构的中间件,BEA WebLogic是用于开发.集成.部署和管理大型分布式Web应用 ...
- Oracle 对某张表中的某一列进行取余,将结果集分为多个集合
比如分为 5个集合,那么就用某一列和5 取余 ,分别可以取 余数为 0.1.2.3.4 的结果集,那么就把集合分为5个小的集合了 1.取余数为 0 的集合 select * from (select ...
类型转换总结: