leetcode 30day--2
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:
Input: 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
class Solution {
public:
//主要是判断平方和是否会重复,如果重复就会造成cycle,否则就可以一直算下去,直到为1
long help(int n){
long res = 0;
while(n){
res += (n%10)*(n%10);
n=n/10;
}
return res;
}
bool isHappy(int n) {
if(n == 1){
return true;
}
if(m.find(n) != m.end()) return false;
else{
m[n] = true;
return isHappy(help(n));
}
}
private:
map<int,bool> m;
};
很巧妙,借鉴链表的快慢指针
class Solution {
public:
//借鉴链表是否有环的快慢指针法
long help(long n){
long res=0;
while(n){
long tmp = n%10;
n = n/10;
res += tmp*tmp;
}
return res;
}
bool isHappy(int n) {
long slow=n,fast=n;
do{
slow = help(slow);
fast = help(fast);
fast = help(fast);
if(slow == 1 || fast == 1) return true;
}while(slow != fast); return false;
}
};
leetcode 30day--2的更多相关文章
- LeetCode 983. Minimum Cost For Tickets
原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...
- 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)
Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...
- 【LeetCode】983. 最低票价 Minimum Cost For Tickets(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
随机推荐
- Linux最常用的命令大全
Linux最常用的命令大全 按功能索引 目录处理命令 ls mkdir pwd cd rmdir cp mv rm 文件处理命令 touch cat tac more less head tail l ...
- GCC编译选项笔记
警告选项 -Wall:开启大多数的警告信息 -Wextra:开启额外的警告信息,比如参数未使用警告(-Wunused-parameter) -Werror:将警告当作错误,中断编译 优化选项 -O,- ...
- 【迷宫问题】CodeForces 1292A A NEKO's Maze Game
题目大意 vjudge链接 共两行,从(1,n)到(2,n). 每过一个时刻会有一个位置的状态变化,从能到达这个位置变成不能到达,或从不能到达变成能到达,问在每个时刻中是否能从起点到终点. 数据范围 ...
- 服务器同一个tomcat部署2两个相同的项目
项目A,B(B 是A 的复制) 若把A,B工程同时部署到tomcat下,会发生只能访问A,B工程中的其中一个,而另一个会出现404错误(或者无法访问),此时可参照如下方法解决: 步骤1:找到工程下的w ...
- java安全编码指南之:ThreadPool的使用
目录 简介 java自带的线程池 提交给线程池的线程要是可以被中断的 正确处理线程池中线程的异常 线程池中使用ThreadLocal一定要注意清理 简介 在java中,除了单个使用Thread之外,我 ...
- 一个例子"入坑"布谷鸟算法(附完整py代码)
布谷鸟是比较新的启发式最优化算法,但其与传统的遗传算法,退火算法等相比,被证明收敛速度更快,计算效率更高! 文章目录 本文诞生的缘由 布谷鸟算法思想简介 更新位置的方式 莱维飞行 局部随机行走 抛出个 ...
- 用node.js给C#写一个数据表的实体类生成工具
虽然微软提供了T4模板,但是我感觉非常难用.哪儿比得上直接用脚本来写模板来的爽. 因为要给一个老项目做周边的工具,需要连接到数据库. 我习惯性用EntityFrameworkCore来做,因为毕竟从出 ...
- Java 第四课 对象 类
1.构造方法可以为private public 2.抽象类可以有构造方法,但是必须在子类中调用(super.构造方法)
- AWK实现把一个文件根据内容进行分组输出多个文件
AWK实现把一个文件根据内容进行分组输出多个文件 1.首先准备文件data.txt(分隔符为tab) 第一列省编码,第二列省名称...... 2.将该大文件根据第一列的省编码进行分组并输出到各个省编码 ...
- java关键字之super
1.在子类的构造方法的第一条语句处调用其父类的构造方法: 如果父类提供了构造方法,并且父类不拥有无参构造方法,则要求子类拥有相同结构的构造方法.即,子类构造方法的参数个数和类型必须和父类的构造方法一致 ...