Floyd 判圈 算法
来源:互联网 发布:网络写手. 正规的网站 编辑:程序博客网 时间:2024/04/29 23:50
转自 leetcode 202------判断开心数
给定任意正数,比如19。
计算该正数各位上的数字的平方和,即12 + 92 = 82
重复第二步,对计算结果进行计算,得到
总结:
1、
int digitSquareSum(int n) { int sum = 0, tmp; while (n) { tmp = n % 10; sum += tmp * tmp; n /= 10; } return sum;}bool isHappy(int n) { int slow, fast; slow = fast = n; do { slow = digitSquareSum(slow); fast = digitSquareSum(fast); fast = digitSquareSum(fast); } while(slow != fast); if (slow == 1) return 1; else return 0;}
这是一个单向链表问题,使用Floyd判圈算法,可以计算出其中有没有环。
链接:http://blog.csdn.net/javasus/article/details/50015687
http://www.siafoo.net/algorithm/10
2、基础证明
- 为什么会成环
- for nin [1,10), g(n)>=n, since g(n)=n*n>=nwith equality hold onlyfor n=1
- for other n with only highest digit nonzero (eg. 10, 90, 500, 4000,20000, etc), g(n)<n
- and we can factor n into sum of numbers with only highest digitnonzero, eg. 12045 = 10000 + 2000 + 40 + 5
in this way, we can show for any n>=100, g(n) < n
对于任意正数n,跳转结果是有限的 - 环的大小
可以计算[1,100]里面的所有情况,从而得到环的大小,以及这100个数的对应是否是开心数的表。input: n (positive integer)func g = digitSquareSumwhile n>99 n = g(n)output: HappyTable(n)
使用表或者数组存储每步的计算结果和判断结果
The common choice is to use map or set to check if there is a loop. since the maximum sum is no greater than 2x2 + 9x9x9 (2,999,999,999), we can use a 1000 lengh hashtable . It's not O(1) space but it's faster than the fast-slow pointer solution.class Solution {public: int tran(int n){ int ans = 0; while(n){ ans += pow(n%10, 2); n/=10; } return ans; } bool isHappy(int n) { bool rep[1000]; memset(rep, 0, sizeof(rep)); n = tran(n); while(!rep[n]){ rep[n] = true; if(n == 1) return true; n = tran(n); } return false; }};
public boolean isHappy(int n) { Set<Integer> inLoop = new HashSet<Integer>(); int squareSum,remain; while (inLoop.add(n)) { squareSum = 0; while (n > 0) { remain = n%10; squareSum += remain*remain; n /= 10; } if (squareSum == 1) return true; else n = squareSum; } return false;}
0 0
- Floyd判圈算法
- Floyd判圈算法
- floyd判圈算法
- Floyd判圈算法
- Floyd 判圈 算法
- Floyd判圈算法
- Floyd判圈算法
- Floyd判圈算法
- floyd判圈算法
- Floyd 判圈算法
- 关于Floyd判圈算法
- Floyd判圈算法理解
- Floyd判圈算法【区庆亮】
- Floyd判圈算法理解
- Floyd判圈算法理解
- 算法-floyd判环(圈)算法
- UVA11549(Floyd判圈算法)
- UVa11549 - Calculator Conundrum(Floyd判圈算法)
- 结构体sizeof()的原则
- 文件系统的设计与实现(操作系统课程设计)
- hdu 2560 Buildings
- Prototype使用Hash对象
- 最短路径(bellman算法详解)
- Floyd 判圈 算法
- Java虚拟机及跨平台原理
- C++ 类 自定义迭代器
- Servlet - Listener、Filter、Decorator
- 百度地图API之定位当前和公交查询
- 【iOS】NSDate分类,获得中国农历
- iOS 修炼方法
- 设计模式(4)--使用ASM实现AOP
- uwp开发————拍照