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

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. System.IO中的File、FileInfo、Directory与DirectoryInfo类(实例讲解)

    一.建立的文件夹(对这些文件进行以上四个类的操作): 父目录: 父目录的子目录以及父目录下的文件: 子目录下的文件: 二.效果图 三.代码实现 using System; using System.I ...

  2. MVC 学习(二)之Linq to Sql 简单Demo

    Linq to Entities 已经我的一篇博文中阐述了,这里阐述一下简单的Linq to Sql 的增删改查.Linq to sql 与Linq to Entities虽然同属于DataBase- ...

  3. mongodb:短网址项目

    短网址项目概述 1.短网址项目,是将给定的长网址,转换成短网址. 如 新浪 http://t.cn/zQd5NPw ,其中zQd5NPw就是短网址 前段页面如下

  4. SAP 设置周期性的后台程序,SM36,图解操作 (转)

    SM36是设置SAP周期性运行的事务码 来测试一下,首先先写一个程序: 我有一个zzp_people2的数据表. DATA : INT1 TYPE I. DATA : ITAB LIKE ZZP_PE ...

  5. Java 测试URL地址是否能正常连接

    public static int testWsdlConnection(String address) throws Exception { int status = 404; try { URL ...

  6. C# HttpHelper 采集

    httphelper http://www.sufeinet.com/thread-6-1-1.html

  7. VO,DO,DTO,PO,POJO,EJB

    PO:persistent Object,持久化对象,和数据库一一对应. VO:view Object,视图对象,用于展示,把某个页面或者组件的数据封装起来. DO:Domain Object,领域对 ...

  8. 在centos上编译安装mariadb数据库

    一.安装前提(准备数据文件.安装其他依赖的软件) 1.准备数据存放的目录 [root@localhost ~]# fdisk /dev/sdb  (fdisk /dev/sdb 创建一个逻辑分区/de ...

  9. Ubuntu12.04配置mod_python

    安装: sudo apt-get install libapache2-mod-python python-mysqldb 然后编辑配置文件/etc/apache2/sites-enabled/000 ...

  10. 还在花钱搞开发?猿团YTFCloud,零基础照样做专业APP

    近日,猿团科技再推新品:YTFCloud.这是一套一体化的云端解决方案,用户可以通过平台提供的各类解决方案,一键创建应用,也就是说,YTFCloud实现了APP的DIY自制,用户无需懂得编程,零基础制 ...