202. Happy Number

Write an algorithm to determine if a number n 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.

Return True if n is a happy number, and False if not.

Example:

  1. Input: 19
  2. Output: true
  3. Explanation:
  4. 12 + 92 = 82
  5. 82 + 22 = 68
  6. 62 + 82 = 100
  7. 12 + 02 + 02 = 1

  1. class Solution {
  2. public:
  3. //主要是判断平方和是否会重复,如果重复就会造成cycle,否则就可以一直算下去,直到为1
  4. long help(int n){
  5. long res = 0;
  6. while(n){
  7. res += (n%10)*(n%10);
  8. n=n/10;
  9. }
  10. return res;
  11. }
  12. bool isHappy(int n) {
  13. if(n == 1){
  14. return true;
  15. }
  16. if(m.find(n) != m.end()) return false;
  17. else{
  18. m[n] = true;
  19. return isHappy(help(n));
  20. }
  21. }
  22. private:
  23. map<int,bool> m;
  24. };

很巧妙,借鉴链表的快慢指针

  1. class Solution {
  2. public:
  3. //借鉴链表是否有环的快慢指针法
  4. long help(long n){
  5. long res=0;
  6. while(n){
  7. long tmp = n%10;
  8. n = n/10;
  9. res += tmp*tmp;
  10. }
  11. return res;
  12. }
  13. bool isHappy(int n) {
  14. long slow=n,fast=n;
  15. do{
  16. slow = help(slow);
  17. fast = help(fast);
  18. fast = help(fast);
  19. if(slow == 1 || fast == 1) return true;
  20. }while(slow != fast);
  21.  
  22. return false;
  23. }
  24. };

leetcode 30day--2的更多相关文章

  1. LeetCode 983. Minimum Cost For Tickets

    原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...

  2. 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)

    Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...

  3. 【LeetCode】983. 最低票价 Minimum Cost For Tickets(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  4. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  6. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  7. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  8. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  10. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

随机推荐

  1. 如何将python下载源地址修改为国内镜像源

    (1)在  C:\Users\xxx 下面创建新的目录  pip 文件夹 (2)在 pip目录下创建后缀为ini,名为pip的文件,另存为  (pip.ini) 文件内容设置为:(清华源) [glob ...

  2. Java 集合看这一篇就够了

    大家好,这里是<齐姐聊数据结构>系列之大集合. 话不多说,直接上图: Java 集合,也称作容器,主要是由两大接口 (Interface) 派生出来的: Collection 和 Map ...

  3. 深入了解Redis(7)-缓存穿透,雪崩,击穿

    redis作为一个内存数据库,在生产环境中使用会遇到许多问题,特别是像电商系统用来存储热点数据,容易出现缓存穿透,雪崩,击穿等问题.所以实际运用中需要做好前期处理工作. 一.缓存雪崩 1.概念 缓存雪 ...

  4. pyquery 匹配NavigableString

    pyquery 匹配NavigableString不像xpath那样精确找打匹配对象,只需匹配包含NavigableString的根节点

  5. 灵魂拷问:你真的理解System.out.println()执行原理吗?

    原创/朱季谦 灵魂拷问,这位独秀同学,你会这道题吗?  请说说,"System.out.println()"原理...... 这应该是刚开始学习Java时用到最多一段代码,迄今为止 ...

  6. C++ std::thread 多线程中的异常处理

    环境: VS2019 包含头文件: #include <iostream>#include<thread>#include<exception> 线程函数采用try ...

  7. GO-数据类型

    目录 数据类型 1.分类 2.布尔类型 3.整型 4.浮点型 5.字符类型 6.字符串 7.复数类型 数据类型 1.分类 Go语言内置以下这些基础类型: 类型 名称 长度 零值 说明 bool 布尔类 ...

  8. Vue留言 checked框案列

    在命令行窗口输入vue create "工程名"命令 来创建vue脚手架

  9. mybatis-plus自动填充

    1,给字段添加注解 @TableField(value = "create_time", fill = FieldFill.INSERT) 2,添加填充处理器,需要实现接口Meta ...

  10. docker在win7下的使用

    1,安装 win7下需要安装docker-toolbox,然后通过Docker Quickstart Terminal运行 2,加速 直接pull的话是拉取的docker hub上的镜像,速度非常慢, ...