当前位置:网站首页>笔试面试题目:求丢失的猪
笔试面试题目:求丢失的猪
2020-11-08 10:30:00 【osc_ccy4urvn】
原文发表于:
今天国庆,也是中秋,实在难得。在21世纪的100年内,仅有4年是这样的。今天在家里,陪家人,做饭吃,干家务活,看点闲书,顺便写点东西,待会出去逛逛,然后回来跑跑步。
校园秋招陆续开始了,祝在校同学拿到心仪的offer,也祝社招的同学跳槽顺利。
今天,我们来看下A公司的一个面试题:
有n只猪,用车拉到菜市场去卖,这群猪的身上分别贴了1~n的编号,突然,有一只猪从车上跳下溜走了,求溜走的猪的编号。
这猪还是挺可怜的,溜走了,也要追查编号。下面,我们来看看算法。
算法1:作差法
思路:
Step1: 计算出1~n的和a.
Step2: 求剩余猪的编号之和b.
Step3: a-b即为溜走猪的编号。
这种算法的缺点是:求1~n的和,可能会溢出。
算法2:标记法
思路:开辟一个数组m,用m[i]=0或1来记录i是否存在,针对丢失的猪j, 必有m[j]=0.
这种算法的缺点是:空间复杂度为O(n)
算法3:排序法
思路:对剩余的猪进行排序,在溜走的猪j的编号处,必然出现断裂,从而知道j的具体值。
这种算法的缺点是:以快排为例,时间复杂度和空间复杂度都无法达到最优。
算法4:异或法(最佳算法)
思路:
Step1: 计算出1~n的异或值a.
Step2: 求剩余猪的编号异或值b.
Step3: 求a和b的异或值,即为溜走的猪的编号j.
原理如下:
假设n=5, 丢失的猪的编号是3, 那么剩余的猪的编号是2, 4, 1, 5,下面我们来计算:
j = 1^2^3^4^5^2^4^1^5
显然,根据异或的交换律性质,可以对上述运算进行简化,如下:
j = 1^1^2^2^4^4^5^5^3 = 3
这就求出了溜走的猪的编号。此时,时间复杂度是O(n), 空间复杂度是O(1), 这是最佳算法。至于程序,很简单,故不再赘述。
在之前的文章中,我们其实可以看到,异或是一种特殊的“加减法”,所以,算法1和算法4是有异曲同工之妙的。关于二进制的异或,可以参考:
版权声明
本文为[osc_ccy4urvn]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4302796/blog/4707993
边栏推荐
- 413【毕设课设】基于51单片机无线zigbee无线智能家居光照温湿度传输监测系统
- 来自不同行业领域的50多个对象检测数据集
- Ulab 1.0.0 release
- 学习小结(关于深度学习、视觉和学习体会)
- Iqkeyboardmanager source code to see
- Astra: Apache Cassandra的未来是云原生
- ArrayList源码分析
- Deeplight Technology Bluetooth protocol SRRC certification services
- ts流中的pcr与pts计算与逆运算
- AMD Zen3首发评测:频率超5GHz,IPC提升不止19%,这次真的Yes了 - 知乎
猜你喜欢
游戏优化性能杂谈(十一) - 知乎
laravel8更新之速率限制改进
仅用六种字符来完成Hello World,你能做到吗?
墨者学院SQL注入解题
Test requirements for MIC certification of Bluetooth 2.4G products in Japan
The young generation of winner's programming life, the starting point of changing the world is hidden around
Web novice problem of attacking and defending the world
Astra: the future of Apache Cassandra is cloud native
Seven features of Python 3.9
Insight -- the application of sanet in arbitrary style transfer
随机推荐
Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
Is there a big difference between i5 1135g7 and i51035g1? Which is better?
Rust:命令行参数与环境变量操作
Tiktok live monitoring Api: random recommendation
Cloud Alibabab笔记问世,全网详解仅此一份手慢无
print( 'Hello,NumPy!' )
How does spotify drive data-driven decision making?
Flink的sink实战之一:初探
抖音直播监控Api:随机推荐
Cloud alibabab notes come out, the whole network detailed explanation only this one hand is slow
How can a technician take over a complex system?
Oops, the system is under attack again
Unparseable date: 'mon Aug 15 11:24:39 CST 2016', time format conversion exception
Bili Bili common API
软件测试培训班出来好找工作么
Python loop distinction (while loop and for loop)
来自不同行业领域的50多个对象检测数据集
Distributed consensus mechanism
vivoy73s和荣耀30青春版的区别
2 days, using 4 hours after work to develop a test tool