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

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

Solution: 辅助函数;循环计算每位数字的平方和,直到出现结果为1(返回true)或者重复(返回false)

 class Solution {
public:
bool isHappy(int n) {
map<int,bool> m;
int ret=sum(n);
while(ret!=){
if(m[ret]==true)return false;
m[ret]=true;
ret=sum(ret);
}
return true;
}
int sum(int n){
int ret=;
while(n){
ret += (n%)*(n%); //不支持(n%10)^2
n /= ;
}
return ret;
}
};

【LeetCode】202 - Happy Number的更多相关文章

  1. 【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

  2. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  3. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  4. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  5. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  6. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  7. 【一天一道LeetCode】#202. Happy Number

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  8. 【刷题-LeetCode】202. Happy Number

    Happy Number Write an algorithm to determine if a number n is "happy". A happy number is a ...

  9. 【Leetcode】179. Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

随机推荐

  1. java异常——RuntimeException和User Define Exception

    1.RuntimeException public class RuntimeException { public static void main(String[] args) { // TODO ...

  2. 重新格式化hdfs系统的方法

    重新格式化hdfs系统的方法: (1)查看hdfs-ste.xml <span style="font-size:18px;"><property> < ...

  3. 动态库加载出错,cannot restore segment prot after reloc: Permission denied

    转自:taolinke的博客 项目中碰到的问题,编译好的so文件,放到其他机器上去加载,报了错误,cannot restore segment prot after reloc: Permission ...

  4. python list删除元素 del remove

    L=[5,4,3,2,1,'abc'] del 按照index删除比如: del L[i] del L[i:j] remove按照内容删除 L.remove('abc') L.remove(0)#会报 ...

  5. python 调用 C++ code

    本文以实例code讲解python 调用 C++的方法. 1. 如果没有参数传递从python传递至C++,python调用C++的最简单方法是将函数声明为C可用函数,然后作为C code被pytho ...

  6. A - 畅通工程

    A - 畅通工程 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  7. 推荐个Java代码质量检测的利器 —— FindBugs

    一.下载 插件的下载地址(sourceforge):FindBugs-Eclipse插件 二.安装 Eclipse插件的安装,就不多说了. 三.使用 1.找一个Project,选中它(也可以针对某个P ...

  8. 编译android出错

    注意:frameworks/base/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java 使用了未经检查或不安全的操作.注意:要了 ...

  9. MySQL之Join

    参见MySQL(以5.1为例)中官方手册:MySQL官方手册-JOIN 假设有以下几个表 t1 id book 1 java 2 c++ 3 php t2 id author 2 zhang 3 wa ...

  10. VS2008下使用 CMFCPropertyGridCtrl 转载

    http://blog.csdn.net/sunnyloves/article/details/5655575 在DLG中的基本应用 . 首先在Cxxdlg.h文件中加入 public: CMFCPr ...