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
Credits:
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.
分析
此题目,只要理解了快乐数的判定条件,便很简单了。
快乐数(happy number)有以下的特性:在给定的进位制下,该数字所有数位(digits)的平方和,得到的新数再次求所有数位的平方和,如此重复进行,最终结果必为1。
以十进位为例:
2 8 → 2^2+8^2=68 → 6^2+8^2=100 → 1^2+0^2+0^2=1
3 2 → 3^2+2^2=13 → 1^2+3^2=10 → 1^2+0^2=1
3 7 → 3^2+7^2=58 → 5^2+8^2=89 → 8^2+9^2=145 → 1^2+4^2+5^2=42 → 4^2+2^2=20 → 2^2+0^2=4 → 4^2=16 → 1^2+6^2=37……
因此28和32是快乐数,而在37的计算过程中,37重覆出现,继续计算的结果只会是上述数字的循环,不会出现1,因此37不是快乐数。
不是快乐数的数称为不快乐数(unhappy number),所有不快乐数的数位平方和计算,最後都会进入 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 的循环中。
因此,只要循环计算过程中,结果为该数最初值或4,则必然为不快乐数。
AC代码
class Solution {
public:
bool isHappy(int n) {
if (n <= 0)
return false;
if (n == 1)
return true;
int sum = n;
while (sum != 1)
{
int tmp = sum;
sum = 0;
//求各个位平方和
while (tmp != 0)
{
sum += pow(tmp % 10, 2);
tmp /= 10;
}//while
if (sum == n || sum == 4)
return false;
}//while
if (sum == 1)
return true;
else
return false;
}
};
LeetCode(202) Happy Number的更多相关文章
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(306) Additive Number
题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...
- LeetCode(65) Valid Number
题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- LeetCode(260) Single Number III
题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...
- LeetCode(268) Missing Number
题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...
- LeetCode(136) Single Number
题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...
- LeetCode(9)Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- Leetcode(4)寻找两个有序数组的中位数
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
随机推荐
- 《Python网络爬虫之三种数据解析方式》
引入 回顾requests实现数据爬取的流程 指定url 基于requests模块发起请求 获取响应对象中的数据 进行持久化存储 其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需要进行指 ...
- JavaScript引擎基本原理:Shapes和Inline Caches
原文链接: JavaScript engine fundamentals:Shapes and line Cahes 这篇文章描述了一些在js引擎中通用的关键点, 并不只是V8, 这个引擎的作者(Be ...
- Integer一类的比较问题
总体主要分为两个方面 ①比较的是值 一.基本数据类型与引用数据类型进行比较时,引用数据类型会进行拆箱(自动拆装箱需要在jdk1.5以上),然后与基本数据类型进行值的比较 举例: int i = 12; ...
- 106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树
给定一棵树的中序遍历与后序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出中序遍历 = [9,3,15,20,7]后序遍历 = [9,15,7,20,3]返回如下的二叉树: ...
- Mysql 事务隔离级别(图文详解)
本文由 SnailClimb 和 BugSpeak 共同完成. 事务隔离级别(图文详解) 什么是事务? 事物的特性(ACID) 并发事务带来的问题 事务隔离级别 实际情况演示 脏读(读未提交) 避免脏 ...
- javaScript中的闭包原理 (译)
这篇文章通过javaScript代码解释了闭包的原理,来让编程人员理解闭包.它不是写给大牛或使用功能性语言进行编程的程序员的.一旦意会了其核心概念,闭包理解起来并不难.然而,你不可能通过阅读任何有关闭 ...
- echarts 添加Loading 等待。
capturedsDetailsEcharts: function(id) { if (!id) { id = mini.get("chnNameCaptureds").getVa ...
- 简单的UDP程序
接受端: package com.dcz.udp; import java.io.IOException; import java.net.DatagramPacket; import java.ne ...
- 涉及到弹出层的opacity样式问题
最近遇到一个弹出层在Chrome中重复的问题,观察发现是opacity引起的以下是代码及现象<!DOCTYPE html><html> <head> ...
- android textview添加滚动条
给textview添加滚动条 方式一: xml代码: //设置滚动条的方向 android:scrollbars="vertical" java中设置 tView=(TextVie ...