202. Happy Number【leetcode】java,hashSet,算法

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

题意:

写一个算法来判断一个数字是否“快乐”。
一个愉快的数字是由下面的过程定义的一个数字:从任何正整数开始,用它的数字的平方代替数字,重复这个过程直到数字等于1(它将停留在哪里),或者在一个不包括1的循环中循环。这个过程以1结尾的数字是快乐数字。
例子:19是一个快乐的数字。

 1 public class Solution {
 2     private int getHappyNumber(int n){
 3         int sum=0;
 4         while(n!=0){
 5             sum+=(n%10)*(n%10);
 6             n=n/10;
 7         }
 8         return sum;
 9     }
10     public boolean isHappy(int n) {
11         HashSet<Integer> hash=new HashSet<Integer>();
12         while(n!=1){
13             if(hash.contains(n))
14                 return false;
15             else{
16                 hash.add(n);
17                 n=getHappyNumber(n);
18             }
19         }
20         return true;
21     }
22 }
时间: 2024-12-05 04:14:34

202. Happy Number【leetcode】java,hashSet,算法的相关文章

Palindrome Number leetcode java

题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using

Valid Number leetcode java

题目: 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 ambigu

Letter Combinations of a Phone Number leetcode java

题目: 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"

Single Number leetcode java

问题描述: 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? 问题分析:要求采用线性时间复杂度,并且最好不使用多余的空间. 一.位操作 异

202. Happy Number Leetcode Python

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

Maximum Subarray leetcode java

题目: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have

LongestSubstringWithoutRepeatingCharacters - leetcode java

维护一个窗口,每次关注窗口中的字符串,在每次判断中,左窗口和右窗口选择其一向前移动.维护一个HashSet, 正常情况下移动右窗口,如果没有出现重复则继续移动右窗口,如果发现重复字符,则说明当前窗口中的串已经不满足要求,继续移动有窗口不可能得到更好的结果,此时移动左窗口,直到不再有重复字符为止,中间跳过的这些串中不会有更好的结果,因为他们不是重复就是更短.因为左窗口和右窗口都只向前,所以两个窗口都对每个元素访问不超过一遍,因此时间复杂度为O(2*n)=O(n),是线性算法.空间复杂度为HashS

Longest Substring Without Repeating Characters leetcode java

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s