算法
文章平均质量分 55
cheung_seol
这个作者很懒,什么都没留下…
展开
-
位运算求平均值
在程序中,使用位运算比加减乘除运行效率要高,尤其是在需要多次迭代或者重复大量计算时。以求两个数的平均值运算为例,应用场景可以参见二分法查找有序数组。first代表数组第一个元素,last代表数组最后一个元素 那么我们可以使用位运算实现mid = (first+last)/2mid = first + ((last - first) >> 1);容易想通等式是成立的。等式右边乘以二再除以2,得到原创 2016-01-05 09:25:11 · 656 阅读 · 0 评论 -
巧用位操作符之——^异或运算符
一道题目 题目:Single Number II Given an array of integers, every element appears three times except for one. Find that single one. 除了一个元素外,其它元素都是出现三次,求那个元素? Note: Your algorithm should ha原创 2016-01-19 11:24:21 · 1283 阅读 · 0 评论 -
Best Time to Buy and Sell Stock 股票交易最大收益
主要是利用动态规划的思想解决问题。Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one tran原创 2016-01-13 10:50:18 · 1001 阅读 · 0 评论