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 ...
随机推荐
- Ubuntu 14.04 (32位)上搭建Hadoop 2.5.1单机和伪分布式环境
引言 一直用的Ubuntu 32位系统(准备下次用Fedora,Ubuntu越来越不适合学习了),今天准备学习一下Hadoop,结果下载Apache官网上发布的最新的封装好的2.5.1版,配置完了根本 ...
- win8.1 vs2010 C++环境下 编译Android Adb.exe
1 IntelliSense: cannot open source file "usb100.h" adb 这是因为没有安装sdk造成的.win7下安装wdk,vs2010能够 ...
- 存在网路的情况下重命名SDE中数据图层错误(The orphan junction feature class cannot be renamed)
运行环境为ArcGIS9.3,VS2008. 问题描述:数据通过SDE存储在Oracle10g数据库中,数据集中存在几何网络,在存在网络的情况下通过程序对其中的数据图层进行重命名,弹出"Th ...
- [z]vs中无法加入断点进行调试的解决方案
http://blog.chinaunix.net/uid-15464162-id-3799069.html [ 1] 以前也遇到过同样的问题,但没有问个为什么,也没有探个毕竟.昨天调试一个DLL,添 ...
- ABAP 日期时间函数
HR_JP_MONTH_BEGIN_END_DATE CALL FUNCTION 'HR_JP_MONTH_BEGIN_END_DATE' EXPORTING IV_DATE = ' IMPORTIN ...
- MongoDB 3.2 在windows上的安装
翻译自 https://docs.mongodb.org/master/tutorial/install-mongodb-on-windows/ 在windows上安装 MongoDB 平台支持:从M ...
- oracle 根据字段分组后,将组内的数据根据字段排序
row_number() over(partition by 分组字段 order by 排序字段 desc)
- codeforces 429E
题意:给定n<=100000线段[l,r],然后给这些线段染色(red or blue),求最后平面上任意一个点被蓝色及红色覆盖次数只差的绝对值不大于1 思路:把每条线段拆成2个点[l<& ...
- haskell中的let和where
haskell中有两种定义局部变量的方法let和where,方法分别如下 roots a b c = ((-b + det) / (a2), (-b - det) / (a2)) *a*c) a2 = ...
- go protobuf 安装
1.https://github.com/google/protobuf/releases/tag/v3.0.0 下载需要的版本,如果执行autogen.sh的过程中出现autoreconf not ...