【LeetCode】202 - Happy Number
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的更多相关文章
- 【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- 【一天一道LeetCode】#202. Happy Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 【刷题-LeetCode】202. Happy Number
Happy Number Write an algorithm to determine if a number n is "happy". A happy number is a ...
- 【Leetcode】179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
随机推荐
- 高性能jdbc封装工具 Apache Commons DbUtils 1.6(转载)
转载自原文地址:http://gao-xianglong.iteye.com/blog/2166444 前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更多的 ...
- CListView虚拟列表
首先说下虚拟列表出现的原因: 数据量比较小的时候,对于CListView控件可以直接使用InsertItem并配合SetItemText函数来插入并修改数据.这样操作很直接. 但是,如果数据量比较大了 ...
- solr在电商平台中的使用示例简析
来源:http://blog.csdn.net/yangbutao/article/details/9450463 在电商平台中搜索是非常重要的功能,主要包括有搜索词类目导航.自动提示和搜索排序功能 ...
- kettle创建资源库
手动修改一下这个表 R_VERSION INSERT INTO R_VERSION(ID_VERSION, MAJOR_VERSION, MINOR_VERSION, UPGRADE_DATE, IS ...
- orm 通用方法——GetOneModel 条件查询一个对象
数据连接层的方法封装成通用方法是很有必要,节省不必要的重复写代码. Golang的orm.xorm框架没有封装这些操作. 这里是一个查询单个对象的方法. 此处抛砖引玉,大家继续完善. 通用方法定义代码 ...
- Android 用户界面---拖放(Drag and Drop)(二)
拖拽事件监听器和回调方法 View对象既可以用实现View.OnDragListener接口的拖放事件监听器,也可以用View对象的onDragEvent(DragEvent)回调方法来接收拖拽事 ...
- web.xml中webAppRootKey
------------------------------------------------------------------------------------------------1. w ...
- 51nod1257 背包问题 V3
分数规划经典.开始精度1e-3/1e-4都不行,1e-5就A了 #include<cstdio> #include<cstring> #include<cctype> ...
- duilib库分析: 消息流程分析
转 看下CWindowWnd类与CPaintManagerUI类是咋进行消息分发的吧. 1. 先看下CPaintManagerUI类的MessageLoop函数: void CPaintManag ...
- iOS基础CGAffineTransform的简单使用
CoreGraphics框架中的CGAffineTransform类可用于设定UIView的transform属性,控制视图的缩放.旋转和平移操作: 另称放射变换矩阵,可参照线性代数的矩阵实现方式0. ...