lintcode easy 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 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

1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1

public class Solution {
    /**
     * @param n an integer
     * @return true if this is a happy number or false
     */

    boolean[] visited=new boolean[100];

    public boolean isHappy(int n) {
        if (n<visited.length&&visited[n])
            return false;

        if(n<visited.length)
        {
            visited[n]=true;
        }

       int sum=0;
       while(n > 0)
       {
           int d = n % 10;
           sum += d * d;
           n /= 10;
       }

       if (sum == 1)
        return true;

       return isHappy(sum);
    }
}

时间: 2024-12-25 08:05:56

lintcode easy Happy Number的相关文章

[LintCode] Create Maximum Number 创建最大数

Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digit

[lintcode easy]Recover rotated sorted array

Recover Rotated Sorted Array Given a rotated sorted array, recover it to sorted array in-place. Example [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] Challenge In-place, O(1) extra space and O(n) time. Clarification What is rotated array? For example, the orgin

[lintcode easy]Merge Sorted Array

Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Have you met this question in a real interview? Yes Which company asked you this question? Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay

[lintcode easy]Remove Nth Node From End of List

Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and return its head. Example Given linked list: 1->2->3->4->5->null, and n = 2. After removing the second node from the end, the linked list beco

[Lintcode easy]Longest Words

Longest Words Given a dictionary, find all of the longest words in the dictionary. Example Given { "dog", "google", "facebook", "internationalization", "blabla" } the longest words are(is) ["internati

[Lintcode] Kth Smallest Number in Sorted Matrix

Kth Smallest Number in Sorted Matrix Find the kth smallest number in at row and column sorted matrix. Have you met this question in a real interview? Yes Example Given k = 4 and a matrix: [ [1 ,5 ,7], [3 ,7 ,8], [4 ,8 ,9], ] return 5 Challenge O(k lo

[LintCode] 459 Closest Number in Sorted Array

DescriptionGiven a target number and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to the given target. Return -1 if there is no element in the array. NoticeThere can be duplicate elements in the array,

[lintcode easy]Add Binary

Add Binary Given two binary strings, return their sum (also a binary string).   Example a = 11 b = 1 Return 100 solution: 比较两个string的长度,将长读较小的string左边用0补齐. 设置进位标志flag.循环结束后如果进位标识大于0,则返回进位加上其他string:否则返回新string: char 0对应ASCII码表中30 1对应31....减去0所对应的值以后就

Lintcode: Kth Prime Number

Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. The eligible numbers are like 3, 5, 7, 9, 15 ... Example If k=4, return 9. Challenge O(n log n) or O(n) time 这是丑数问题(Ugly Number), 思路参看: http://www.cppblog.co