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 ...
随机推荐
- 实验三 平面广告制作工具Photoshop基础--制作LOGO
实验三 平面广告制作工具Photoshop基础--制作LOGO [实验目的] ⑴.熟悉Photoshop的界面 ⑵.能利用photoshop处理简单的图像 [实验条件] ⑴.个人计算机一台 ⑵.个人 ...
- 多测师讲解接口测试 _HTTP常见的状态码归纳_高级讲师肖sir
100 Continue 初始的请求已经接受,客户应当继续发送请求的其余部分 101 Switching Protocols 服务器将遵从客户的请求转换到另外一种协议 200 OK 一切正常,对 ...
- 游戏2048的核心算法c#版本的实现
接触游戏有一段时间了,也写了一些东西,效果还不错,今天没事,我就把2048 c# 版本的实现贴出来,代码已经测试过,可以正常.完美运行.当然了,在网上有很多有关2048的实现方法,但是没有提出到类里面 ...
- day47 Pyhton 数据库Mysql 04
# 表结构 # 建表 - 表的增加 # create table # 删表 - 表的删除 # drop table # 改表 - 表的修改 # alter table 表名 # rename 新表名 ...
- 【C语言C++编程学习笔记】基础语法,第一个简单的实例编程入门教程!
C语言/C++编程学习:一个简单的实例 让我们来看一个简单的C语言程序.从下面的程序可以看出编写C语言程序的一些基本特征. 如果你能知道该程序将会在显示器上显示一些内容,那说明你还是知道一些的! ...
- 自定义常用input表单元素一:纯css 实现自定义checkbox复选框
最下面那个是之前写的 今天在做项目的时候发现,之前写的貌似还是有点多,起码增加的span标签可以去掉,这样保持和原生相同的结构最好的,仅仅是样式上的变化.今天把项目中的这个给更新上来.下面就直接还是 ...
- CPU 底层运算之乘法运算
CPU 运算加减法运算 假设计算 3+3 原码是0011 * 0011(以4位存贮单元,因为是原码,最高位不代表符号位) 1. 首先 判断 两个加数是否有 负数(减法) 如果有 负数 先将负数转 ...
- ZooKeeper的数据模型
ZooKeeper的数据模型 ZooKeeper提供的命名空间与标准的文件系统的命名空间非常类似:名称是由斜杠(/)分隔的一系列路径元素:ZooKeeper命名空间中的每个节点都由路径标识,如下图: ...
- 没事学学KVM(一)
学习KVM肯定要找来一台虚机来学习呀,通过VMware workstation创建虚机,现在的电脑CPU,包括INTER,AMD都支持,公司发的电脑CPU为inter,通过开启inter VT-X可在 ...
- dbvis 导出表结构 xls
1.dbvis 可以导出多种格式的文件,如SQL.XLS.TXT.HTML.JSON.CSV及XML. 需求场景(本场景是实际场景引申的场景,此处导出xls): 最近,遇到一个需求需要将表的结构数据导 ...