LeetCode OJ 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 + 9= 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

happy number是把一个数的各个位置的数字的平方相加,直到值为1为止。如果一个数是happy number,那么最终的结果一定会是1,如果不是,这个过程将一直持续下去,并且会重复出现以前曾经出现过的数。例如2。

22 = 4

42 = 16

12 + 62 = 37

32 + 72 = 58

52 + 82 = 89

82 + 92 = 145

12 + 42 + 52 = 42

42 + 22 = 20

22 + 02 = 4

···

所以2不是一个happy number。我们的思路就是记录这个过程中出现过的每一个数,如果某些数字在过程中重复出现,则该数肯定不是happy number。如果最后结果是1,则该数是happy number。代码如下:

 1 public class Solution {
 2     public boolean isHappy(int n) {
 3         List<Integer> list = new ArrayList<>();
 4         while (n != 1) {
 5             if (list.contains(n)) {
 6                 return false;
 7             }
 8             list.add(n);
 9             n = toArrayAndSum(n);
10         }
11         return true;
12     }
13
14     public static int toArrayAndSum(int n) {
15         int sum = 0;
16         while (n > 0) {
17             int x = n % 10;
18             n = n / 10;
19             sum = sum + x * x;
20         }
21             return sum;
22     }
23 }
时间: 2024-11-10 08:21:56

LeetCode OJ 202. Happy Number的相关文章

LeetCode OJ 之 Single Number III (唯一的数字-三)

题目: Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The or

&lt;LeetCode OJ&gt; 374. Guess Number Higher or Lower

We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number is higher or lower. You call a pre-defined API guess(int num) wh

【LeetCode】202. Happy Number

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 proc

LeetCode OJ: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

LeetCode OJ:Ugly Number(丑数)

Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 is ty

LeetCode OJ 之 Ugly Number (丑数)

题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 i

LeetCode OJ 之 Ugly Number II (丑数-二)

题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treated a

LeetCode OJ:Missing Number (丢失的数)

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you implement it usi

[LeetCode OJ] Max Points on a Line—Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

//定义二维平面上的点struct Point { int x; int y; Point(int a=0, int b=0):x(a),y(b){} }; bool operator==(const Point& left, const Point& right) { return (left.x==right.x && left.y==right.y); } //求两个点连接成的直线所对应的斜率 double line_equation(const Point&