今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表。在程序中具体表现为

one = change(one); //一倍速度
two = change(change(two));//两倍速度

即一倍速度的人调用生成函数change一次,两倍速度的人调用生成函数change两次。

Leetcode 202 Happy Number 就是这样一道简单的题,实现方法有很多,但是弗洛伊德判环是比较简单,同时效率也是较高的那种。

class Solution {
public:
int change(int n){
int ans = 0;
for ( ; n!=0 ; ans+= (n%10)*(n%10),n/=10);
return ans;
}
bool isHappy(int n) {
int one = n;
int two = n;
while(1){
one = change(one); //一倍速度
two = change(change(two));//两倍速度
if(one == 1 || two == 1) return true;
if(one == two) return false;
} }
};

Leetcode 202 Happy Number 弗洛伊德判环解循环的更多相关文章

  1. Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环

    分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...

  2. LeetCode 202. Happy Number (快乐数字)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. [LeetCode] 202. Happy Number 快乐数

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  4. LeetCode 202 Happy Number

    Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...

  5. Java for LeetCode 202 Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  6. (easy)LeetCode 202.Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  7. Java [Leetcode 202]Happy Number

    题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

  8. 40. leetcode 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  9. C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

随机推荐

  1. Dx unsupported class file version 52.0

    最近用ADT时遇到这个bug,折腾了好几天. 报错信息: Dx unsupported class file version 52.0 Conversion to Dalvik format fail ...

  2. iconv命令 gbk 转 UTF-8

    -----linux gbk 转 UTF-8-------- iconv 用法 iconv -f "gbk" -t "utf-8" < infile &g ...

  3. hdu 5826 (物理) physics

    题目:这里 题意:光滑的水平直线上有n个质量相等的小球,已知每个小球的初始位置,初始速度和方向,每个小球的每个时刻的加速度a都满足a*v=c,v是该时刻的速度,c是已知的 常数,小球之间的碰撞是完全碰 ...

  4. 【转】HTTP状态码(HTTP Status Code)

    原文链接:http://www.chaoji.com/features/httpstatus.aspx 一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - ...

  5. 实战录 | Redis的主从服务器搭建

    <实战录>导语 云端卫士<实战录>栏目定期会向粉丝朋友们分享一些在开发运维中的经验和技巧,希望对于关注我们的朋友有所裨益.本期分享人为云端卫士安全平台工程师田全磊,将带来Red ...

  6. SQL2008性能计数器注册表配置单元一致性失败

    按照这个操作 http://jingyan.baidu.com/article/7c6fb4287c923880652c9074.html如果在注册表中没有 HKEY_LOCAL_MACHINE\SO ...

  7. 2600: [Ioi2011]ricehubh

    Description 乡间有一条笔直而长的路称为"米道".沿着这条米道上 R 块稻田,每块稻田的坐标均为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺 ...

  8. (转)深入理解PHP之数组(遍历顺序)

    深入理解PHP之数组(遍历顺序)(转) http://www.laruence.com/2009/08/23/1065.html (鸟哥) 经常会有人问我, PHP的数组, 如果用foreach来访问 ...

  9. 编程之美2014挑战赛 复赛 Codehunt平台试题答案

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  10. Linux下paste命令

    paste 用于将多个文件按照列队列进行合并. 该命令主要用来将多个文件的内容合并,与cut命令完成的功能刚好相反. 1.原文件: 1>a.txt [root@localhost home]# ...