力扣算法题之[202. 快乐数]
文章涉及本人版权问题,随意引用等侵权问题必究!
题目如下:
BF Solution :
class Solution {
public boolean isHappy(int n) {
// if(n == 1)return true;
String s = String.valueOf(n);
int temp = 0, count = 0;
while(temp != 1) {
temp = 0;
for (int i = s.length() - 1; i >= 0; --i){
int a = Integer.parseInt("" + s.charAt(i));
temp += Math.pow(a, 2);
}
System.out.println("temp: " + temp);
count++; // 作为一个虚拟边界,防止死循环
s = String.valueOf(temp);
if(count>6)break;
}
System.out.println("count: " + count);
if(count>6)return false;
return true;
}
}
后序:想着能否更快速对[1,2147483647]范围内快乐数进行计数操作,于是有了以下代码,如有问题,欢迎各位大佬指出——
import java.util.*;
// https://leetcode-cn.com/problems/happy-number/
// 编写一个算法来判断一个数 n 是不是快乐数。
//
//「快乐数」定义为:
//
//对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
//然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。
//如果 可以变为 1,那么这个数就是快乐数。
//如果 n 是快乐数就返回 true ;不是,则返回 false 。
//
//来源:力扣(LeetCode)
//链接:https://leetcode-cn.com/problems/happy-number
//著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
/*
* 输入:n = 19
* 输出:true
* 解释:
* 12 + 92 = 82
* 82 + 22 = 68
* 62 + 82 = 100
* 12 + 02 + 02 = 1
*
* 输入:n = 2
* 输出:false
*/
// 快乐数
public class find_happy_number {
// save all proved number in round as the happy number to accelerate search speed of
// the total number in the range of [1, 2147483647] in the valiable of 'totalHappyNumber'
public static List<Integer> totalHappyNumber = new ArrayList<>(); // not need use data type double or long, invalid
public static List<Integer> happyNum = new ArrayList<>(); // temp save the happy number in round
public static List<Integer> totalnorhappyNum = new ArrayList<>(); // save all the nor happy number in round
// true : n -> 快乐数
// false: n -> 非快乐数
public static boolean isHappyNumber(int n) {
// happyNum.add(n);
// if(n == 1)return true;
String s = String.valueOf(n);
int temp = 0, count = 0;
while(temp != 1) {
temp = 0;
for (int i = s.length() - 1; i >= 0; --i){
int a = Integer.parseInt("" + s.charAt(i));
temp += Math.pow(a, 2);
}
count++; // 作为一个虚拟边界,防止死循环
if (HAPPYNUMBER.contains(temp))break;
// System.out.println("temp: " + temp);
// happyNum.add(temp);
s = String.valueOf(temp);
if(count>6)break;
// 6是后台测试用例的临界值,即可以看出后台所使用的测试用例中n如果是快乐数,均会在6次循环之内会收敛到1
// 或者说明所有的快乐数均会在6次循环之内会收敛到1(未证)
}
// System.out.println("count: " + count);
if(count>6) {
// for (int i : happyNum) {
// if (!totalnorhappyNum.contains(i)) {
// totalnorhappyNum.add(i);
// }
// }
// happyNum.clear();
return false;
}
// for (int i : happyNum) {
// if (!totalHappyNumber.contains(i)) {
// totalHappyNumber.add(i);
// }
// }
return true;
}
public static void main(String[] args) {
for(int i : ha) {
HAPPYNUMBER.add(i);
}
int count = 0;
for (int i = 1; i <= 2147483646; ++i) {
// if (totalnorhappyNum.contains(i)) {
System.out.println(i + " 已经不是快乐数.");
// continue;
// }
// if (totalHappyNumber.contains(i)) {
// count++;
// System.out.println(i + " 已经是快乐数." + " " + count);
// continue;
// }
if (isHappyNumber(i)) {
count++;
System.out.println("No." + count+" " + i + " 是快乐数");
} else {
// System.out.println(i + " 不是快乐数.");
}
}
if (isHappyNumber(2147483647)) { // half: 1073741823.5
System.out.println("2147483647 是快乐数.");
count++;
} else {
System.out.println("2147483647 不是快乐数");
}
// judge whether the max number of int value '2147483647' is "happy number" or not
// if(totalHappyNumber.contains(2147483647)) {
// System.out.println("2147483647 已经是快乐数.");
continue;
// }
// else if (isHappyNumber(2147483647)) { // half: 1073741823.5
// System.out.println("2147483647 是快乐数.");
// count++;
// } else {
// System.out.println("2147483647 不是快乐数.");
// }
System.out.println("在 [1,2147483647] 区间内是快乐数的总数为: " + count);
// Object[] a = totalHappyNumber.toArray();
// Arrays.sort(a);
// System.out.println("a: " + a.toString());
// System.out.println(totalHappyNumber.toString());
// for (int i : totalHappyNumber) {
// System.out.println(i);
// }
// System.out.println(Math.pow(2, 31));
// // 2147483648 is out of range of System.out.println(int), but 2147483647 is not out of range.
// System.out.println("2147483648");
}
// [1, 7, 49, 97, 130, 10, 13, 19, 82, 68, 100,
// 23, 28, 31, 32, 44, 70, 79, 86, 91, 94, 103, 109, 129,
// 133, 139, 167, 176, 188, 190, 192, 193, 203, 208, 219, 226, 230, 236, 239] // 1-260
static int[] ha = {1, 7, 49, 97, 130, 10, 13, 19, 82, 68, 100,
23, 28, 31, 32, 44, 70, 79, 86, 91, 94, 103, 109, 129,
133, 139, 167, 176, 188, 190, 192, 193, 203, 208, 219, 226, 230, 236, 239};
public static List<Integer> HAPPYNUMBER = new ArrayList<Integer>();
}