给定一个正整数,请你判断这个数是不是快乐数。
快乐数:对于一个正整数,每次把他替换为他每个位置上的数字的平方和,如果这个数能变为 1 则是快乐数,如果不可能变成 1 则不是快乐数。
例如:正整数 19
转换过程为 1*1+9*9=82 , 8*8+2*2=68,6*6+8*8=100,1*1+0*0+0*0=1 ,所以他是快乐数。
数据范围:输入的正整数满足
19
true
111
false
package main //import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return bool布尔型 */ func happynum( n int ) bool { //如果不能变成1,就会陷入循环 cnt:=map[int]bool{} for n!=1{ if _,ok:=cnt[n];ok{ return false } cnt[n]=true n=handle(n) } return true } func handle(n int)int{ ans:=0 for n>0{ ans+=(n%10)*(n%10) n/=10 } return ans }
bool happynum(int n ) { int a[10]; while (1) { int count = 0; bool yes = false; for (int i = 0; i < 10; i++) { a[i] = n % 10; if (a[i] == 0) count++; if (a[i] == 1) yes = true; n = n / 10; } if (count == 9) { if (yes) return true; else return false; } n = 0; for (int i = 0; i < 10; i++) { n += a[i] * a[i]; } } }
//快慢指针法 // class Solution { // public: // /** // * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 // * // * // * @param n int整型 // * @return bool布尔型 // */ // int SquareSum(int n) // { // int sum=0; // while(n) // { // sum+=pow(n%10, 2); // n/=10; // } // return sum; // } // bool happynum(int n) // { // // write code here // int slow=n, fast=SquareSum(n); // while(fast!=1&&fast!=slow) // { // slow=SquareSum(slow); // fast=SquareSum(SquareSum(fast)); // } // return fast==1; // } // }; //哈希表 class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return bool布尔型 */ int SquareSum(int n) { int sum=0; while(n) { sum+=pow(n%10, 2); n/=10; } return sum; } bool happynum(int n) { // write code here unordered_set<int> arr; while(1) { int sum=SquareSum(n); if(sum==1) return true; if(arr.find(sum)!=arr.end()) return false; else arr.insert(sum); n=sum; } } };
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return bool布尔型 */ bool happynum(int n) { if(n<10&&n!=1)return false; string s=to_string(n); unordered_map<string, bool>st; while(s!="1"){ long long ans=0; for(auto c:s){ ans+=pow(c-'0',2); } s=to_string(ans); if(st[s])return false; st[s]=true; } return true; } };
# -*- coding: utf-8 -*- # coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param n int整型 # @return bool布尔型 # class Solution: """ 题目: https://www.nowcoder.com/practice/293b9ddd48444fa493dd17da0feb192d?tpId=196&tqId=40522&rp=1&ru=/exam/oj&qru=/exam/oj&sourceUrl=%2Fexam%2Foj%3FjudgeStatus%3D3%26page%3D1%26pageSize%3D50%26search%3D%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196&difficulty=undefined&judgeStatus=3&tags=&title= 算法: 模拟法 复杂度: 时间复杂度:O(M), M为总的循环次数 空间复杂度:O(1) """ def happynum(self, n): # write code here while n >= 10: num, n = n, 0 while num: n += (num % 10) ** 2 num /= 10 return n == 1 if __name__ == "__main__": sol = Solution() # n = 19 n = 111 res = sol.happynum(n) print res
public boolean happynum (int n) { // write code here boolean b=false; ArrayList arrayList=new ArrayList(); if (n<0){ b= false; } else { while (true){ while (n>0){ int a=n%10; arrayList.add(a); n=n/10; } n=0; for (int i=0;i<arrayList.size();i++){ int a=(int)arrayList.get(i); n= n+a*a; } arrayList.removeAll(arrayList); if(n==1) { b=true; break; } if(n==4) { break; } } } return b; }