Java for 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
解题思路:

HashMap,JAVA实现如下:

    public boolean isHappy(int n) {
        HashMap<Integer,Boolean> hm=new HashMap<Integer,Boolean>();
        while(!hm.containsKey(n)&&n!=1){
        	hm.put(n, true);
        	n=HappyTime(n);
        }
        return n==1;
    }
   static public int HappyTime(int n){
    	int res=0;
    	while(n!=0){
    		res+=(n%10)*(n%10);
    		n/=10;
    	}
    	return res;
    }
时间: 2024-12-29 11:42:09

Java for LeetCode 202 Happy Number的相关文章

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

leetcode 202. Happy Number 判断一个数是否是“Happy Number” ---------- java

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

Java for LeetCode 179 Largest Number

Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of an i

40. 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

Java for LeetCode 009 Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. 解题思路: 由于题目中给定不能使用额外空间开销,因此不能转为String类型,我们需要一位一位处理. Java代码如下: static public boolean isPalindrome(int x) { if (x < 0) return false; int temp = x; int beginIndex = 0, endIndex =

Java for LeetCode 065 Valid Number

Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be ambiguous.

LeetCode 202 Happy Number(开心数)(vector、unordered_set)

翻译 写一个算法来决定一个数是否是"开心"的. 开心数是被如下步骤所定义的数: 从所有正整数开始,用其每个数字的平方和来替代这个数,不断重复这个过程直到最后的结果为1(此时它就停止了), 或者它在一个不包含1的周期内无限循环. 这些在这个过程中以1结尾的数就是开心数. 例如:19是开心数. 12+92=82 82+22=68 62+82=100 12+02+02=1 原文 Write an algorithm to determine if a number is "happ

Java for LeetCode 137 Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解题思路一: 一个int的长度是32,因此可以开一个长度为32的数组,表示

Java for LeetCode 136 Single Number

Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解题思路: 按位异或运算,出现两次的都归零了,剩下的就是出现一次的了,JAVA实现如下