Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表。在程序中具体表现为
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 弗洛伊德判环解循环的更多相关文章
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- LeetCode 202. Happy Number (快乐数字)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- [LeetCode] 202. Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- LeetCode 202 Happy Number
Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...
- Java for LeetCode 202 Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- (easy)LeetCode 202.Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- Java [Leetcode 202]Happy Number
题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...
- 40. leetcode 202. Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
随机推荐
- wpf 任务栏闪烁
[StructLayout(LayoutKind.Sequential)] public struct FLASHWINFO { public UInt32 cbSize; public IntPtr ...
- iOS 如何给Xcode7项目添加“.pch”文件
1.首先打开你的项目(演示使用一个空的项目),按照以下步骤即可 找到“Supporting Files”文件夹,右键即可看到下图,选择“New File...” 2.选择"iOS" ...
- Ubuntu 14.04下搭建Python3.4 + PyQt5.3.2 + Eric6.0开发平台
引言 找了很多Python GUI工具集,还是觉得PyQt比较理想,功能强大跨平台,还支持界面设计器.花一天时间折腾了Ubuntu14.04(32位)+ Python3.4 + Qt5.3.2 + P ...
- SAP 打开账期
1.先OB52修改账期: 如下界面开得公司9000下面 7.8月份的账期 2.mmpv 关闭上两个账期 3.mmrv 查看现在账期情况
- css2选择器
CSS1&2元素选择器 选择符 类型 版本 简介 * 通配选择符 CSS2 所有元素对象. E 类型(HTML)选择符 CSS1 以文档语言对象类型作为选择符. E#myid id选择符 ...
- Slave failed to initialize relay log info structure from the repository
现象 查看slave 服务状态 show slave status\G; 错误 Last_Errno: 1872 Last_Error: Slave failed to initialize rela ...
- Mac上的抓包工具Charles
http://blog.csdn.net/jiangwei0910410003/article/details/41620363 $********************************** ...
- Til the Cows Come Home
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...
- CSS3中的Transition属性详解(贝赛尔曲线)
transition语法: transition : [<'transition-property'> || <'transition-duration'> || <'t ...
- oracle每天清理归档日志
http://langzhiwang888.iteye.com/blog/1675033 参考这里的内容 在数据库服务器上新建一个bat文件(文件名随意) 编辑此文件为: rman target 's ...