LeetCode之旅(18)-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~20的算了一下,发现没有总结出规律,不过不满足条件的数字是会循环出现,这样就可以判断如在等于一之前循环了,就不满足条件。(另外像这种平方的数字,不太好总结规律)
代码
public class Solution {
public int getHappy(int n){
int sum =0;
while(n!=0){
sum += (n%10) * (n%10);
n = n/10;
}
return sum;
}
public boolean isHappy(int n) {
HashSet<Integer> happySet = new HashSet<Integer>();
while(n!=1){
if(happySet.contains(n))
return false;
happySet.add(n);
n = getHappy(n);
}
return true;
}
}
LeetCode之旅(18)-Happy Number的更多相关文章
- leetcode之旅(11)-Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- LeetCode之旅(13)-Valid Anagram
题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- LeetCode之旅(17)-Ugly Number
题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...
- leetCode之旅(14)-Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- LeetCode 287. Find the Duplicate Number (找到重复的数字)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- LeetCode之“排序”:Largest Number
题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
随机推荐
- blob2clob/clob2blob研究
一.两种方法实现 blob到clob的转换 CREATE OR REPLACE FUNCTION blob2clob(v_blob_in IN BLOB) RETURN CLOB IS v_fi ...
- 【一天一道LeetCode】#237. Delete Node in a Linked List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- JVM的内存区域模型
首先要明白一个概念,就是JVM的内存区域划分与java的内存区域模型是两个不同的概念,前者指的是在java中jvm会将一个程序划分为哪些块来存储对应的数据,后者是一个更宏观上的j概念,指的是java线 ...
- 纯HTML5APP与原生APP的差距在哪?
笔者写过一些纯H5的APP,虽然开发起来的确很快很舒服,但和原生比起来纯H5APP还是有很多问题,主要聚集在以下几个方面: 1.动画 动画有很多种,比如侧边栏菜单的滑入滑出.元素的响应动画.页面切换之 ...
- Leetcode_58_Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Eclipse插件 - FindBugs 检查代码隐藏的 Bug
简介 FindBugs 是一个在 Java 程序中查找 bug 的程序,它可以查找可能出错的代码,注意 FindBugs 是检查 Java 字节码,也就是*.class文件.其实准确的 ...
- iOS中 KVO 键值观察者
KVO Key-Value-Obsever 键值观察者 1.首先要有一个观察者,此时被观察者是自己找一个观察者观察自己的key值对应的value值有没有改变,如果改变了就可以做一些响应的操作 创建一个 ...
- C++对象模型(五):The Semantics of Data Data语义学
本文是<Inside the C++ Object Model>第三章的读书笔记.主要讨论C++ data member的内存布局.这里的data member 包含了class有虚函数时 ...
- ViewPager 实现 Galler 效果, 中间大图显示,两边小图展示
正常情况下, ViewPager 一页只能显示一项数据, 但是我们常常看到网上,特别是电视机顶盒的首页经常出现中间大图显示两端也都露出一点来,这种效果怎么实现呢?先上一张效果图: 大家第一眼肯定想到了 ...
- shell 常用正则表达式
"^\d+$" //非负整数(正整数 + 0) "^[0-9]*[1-9][0-9]*$" //正整数 "^((-\d+)|(0+))$" ...