202. Happy Number
来源:互联网 发布:网络舞曲dj串烧视频 编辑:程序博客网 时间:2024/06/14 05:40
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
public class Solution {public boolean isHappy(int n) {Set<Integer> set = new HashSet<Integer>();int quadraticSum = 0;// set集合有个特性,在向set集合中添加元素时,如果存在,返回false,如果不存在,返回true// 所以以set再能否添加数据为循环条件while (set.add(n)) {quadraticSum = getQuadraticSum(n);if ( quadraticSum == 1 ) {return true;}n = quadraticSum;}return false;}// 用来计算一个数按位被拆开后的平方和public int getQuadraticSum(int n) {int sum = 0;while ( n != 0) {int temp = n % 10;sum += Math.pow(temp, 2);n = n / 10;}return sum;}}
阅读全文
0 0
- 202. Happy Number
- [LeetCode]202.Happy Number
- 【LeetCode】202.happy number
- LeetCode 202. Happy Number
- [leetcode] 202.Happy Number
- 【leetcode】202. Happy Number
- 202.Happy Number
- [leetcode] 202. Happy Number
- 202. Happy Number LeetCode
- 202. Happy Number
- leetCode 202. Happy Number
- 202. Happy Number
- 202. Happy Number
- 202. Happy Number
- [LeetCode]202. Happy Number
- leetcode 202. Happy Number
- 202. Happy Number
- 202. Happy Number
- 如何将Oracle 当前日期加一天、一分钟
- 使用tensorflow serving 启动模型时,报如下错误:The TensorFlow library wasn't compiled to use SSE4.1 instructions
- python 隐藏平方级__字符串
- octave-计算数据
- 纹理文本
- 202. Happy Number
- spring事务管理几种方式
- 共享存储之ISCSI 以及使用
- kettle安装以及运行注意事项
- 商品开放平台1
- MyBatis一对多和多对一
- eclipse 中提示tomcat 的端口被占用了 后的最快捷解决方法 很多时候运行tomcat 的时候总是会提示tomcat 的端口被占用 但是任务管理器里面还找不到是哪个端口被占用了 因此很多人就
- MongoDB学习笔记(一) MongoDB介绍及安装
- c++多线程重点难点(六)CriticalSection