Lettcode_202_Happy Number

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/45396585

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

思路:

(1)题意为判断给定的整数是否为一个“快乐的数”,所谓快乐的数需要满足一下几个条件:将该整数的每个位上的数字的平方相加得到一个新的整数,循环对新的整数进行上述操作,如果最后所得整数收敛于1,则这样的数字为一个“快乐的数”。

(2)首先,判断0肯定不是一个“快乐的数”;其次,对初始数字的每个位上数的平方相加,循环进行前面的操作;需要注意的是,对于可能会出现死循环的数字需要进行判断,需要判断循环的次数,这里通过测试得到循环次数为4,即如果4次循环过程中,每一次所得数字中都不收敛于1,则判断该数字不是一个“快乐的数”,否则,则是一个“快乐的数”;最后,对于那些循环得到的结果为1的数字,直接返回true,即该数字是一个“快乐的数”。

(3)详情见下方代码。希望本文对你有所帮助。

算法代码实现如下:

/**
 * @author liqq
 */
public class HappyNumber {
	public static boolean isHappy(int n) {
		if (n == 0)
			return false;
		return testhappy(0, n);
	}

	private static boolean testhappy(int number, int n) {
		int sum = n;
		int add = 0;
		boolean hasone = false;
		while (sum != 0) {
			int square = (sum % 10) * (sum % 10);
			add += square;
			sum = sum / 10;
			if (sum % 10 == 1) {
				hasone = true;
			}
		}
		number++;

		int test = add;
		while (test != 0) {
			if (test % 10 == 1) {
				hasone = true;
			}
			test = test / 10;
		}

		if (hasone == false && number > 4)
			return false;

		if (add != 1 || add % 10 != 1) {
			boolean istrue = testhappy(number, add);
			if (istrue)
				return true;
		} else {
			return true;
		}
		return false;
	}
}
时间: 2024-08-25 16:40:00

Lettcode_202_Happy Number的相关文章

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

实现一个函数clone,使JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制

实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number.String.Object.Array.Boolean)进行值复制. 1 /** 对象克隆 2 * 支持基本数据类型及对象 3 * 递归方法 */ 4 function clone(obj) { 5 var o; 6 switch (typeof obj) { 7 case "undefined": 8 break; 9 case "string": o = obj + &q

解决sqoop报错Invalid number; item = ITEM_UNICODE

报错栈: java.sql.SQLException: Invalid number; item = ITEM_UNICODE at com.intersys.jdbc.SysList.getInt(SysList.java:1735) at com.intersys.jdbc.CacheResultSet.getInt(CacheResultSet.java:247) at org.apache.sqoop.lib.JdbcWritableBridge.readInteger(JdbcWrit

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

Minimum Inversion Number 【线段数】

Problem DescriptionThe inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of

171. Excel Sheet Column Number

Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 static publi

hdu 5898 odd-even number 数位DP

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 716    Accepted Submission(s): 385 Problem Description For a number,if the length of continuous odd digits is even and the length

Leetcode 202. Happy Number

202. Happy Number Total Accepted: 78171 Total Submissions: 208635 Difficulty: Easy 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 t