Description:

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
public class Solution {
public boolean isHappy(int n) {
ArrayList<String> tList = new ArrayList<>();
String sn = new String(n+"");
while(true) {
String t = calcu(sn);
if(t.equals("1")) {
return true;
}
if(tList.contains(t)) {
return false;
}
tList.add(t);
sn = t;
} }
public static String calcu(String str) {
int t = 0;
for(int i=0; i<str.length(); i++) {
t += Integer.parseInt(str.charAt(i)+"") * Integer.parseInt(str.charAt(i)+"");
}
return new String(t+"");
}
}

LeetCode——Happy Number的更多相关文章

  1. C#版 - Leetcode 191. Number of 1 Bits-题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  2. [leetcode]200. Number of Islands岛屿个数

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  3. [leetcode]694. Number of Distinct Islands你究竟有几个异小岛?

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  4. [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  5. [LeetCode] 694. Number of Distinct Islands

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  6. 力不从心 Leetcode(ugly number heap) 263, 264,313

    Leetcode ugly number set (3 now) new ugly number is generated by multiplying a prime with previous g ...

  7. [LeetCode] 200. Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. [LeetCode] 305. Number of Islands II 岛屿的数量 II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  9. [LeetCode] 323. Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  10. LeetCode#476 Number Complement - in Swift

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

随机推荐

  1. GB2312汉字区位码、交换码和机内码转换方法 (ZT)

    GB2312汉字区位码.交换码和机内码转换方法 (ZT) 为了适应计算机处理汉字信息的需要,1981年我国颁布了GB2312国家标准.该标准选出6763个常用汉字(其中,一级常用汉字3755个,二级汉 ...

  2. contiki list 链表

    1 相关宏和数据结构 1.1 LIST_CONCAT #define LIST_CONCAT2(s1, s2) s1##s2 #define LIST_CONCAT(s1, s2) LIST_CONC ...

  3. uboot中fb实现

    帧缓冲区fb在内存中,要实现fb同步显示需要设定DMA操作. 设定LCD的DMA操作,要在开始操作LCD之前. common/lcd.c中定义lcd_init() --> driver/vide ...

  4. kafka操作清单

    1. 查看topic个数 ./kafka-topics.,dwtest-data3:,dwtest-data4: 2. 查看topic的生产者与消费者消息 ./kafka-topics.,dwtest ...

  5. R语言两种方式求指定日期所在月的天数

                 R语言两种方式求指定日期所在月的天数 days_monthday<-function(date){ m<-format(date,format="%m& ...

  6. 如何去掉drwxr-xr-x@中的@符号Linux文件扩展信息

    如何去掉drwxr-xr-x@中的@符号Linux文件扩展信息ls -lart drwxrwxrwx@ 10 rlanffy staff 340B 3 6 2015 files-rwxrwxrwx@ ...

  7. 【C#】【MySQL】C# 查询数据库语句@Row:=@Row+1以及执行存储过程失败解决方案

    如何实现数据库查询产生虚拟的一列序号的功能: ) )AS r; 该语句可以实现产生虚拟的一列数据在MySQL中运行没有问题. 但是在C#里面调用去出现了错误"Parameter '@ROW' ...

  8. Java String 学习

    String, 首先,String有字面值常量的概念,这个字面值常量是在编译期确定下来的,类加载时直接存入常量池(注意,常量池是类的常量池,类与类之间隔离). 而运行时生成的字符串,是不在常量池中的. ...

  9. 编译 boost 库(win7+boost1.60+vs2008)

    参见:http://blog.csdn.net/u013074465/article/details/42532527 下载boost安装包 https://sourceforge.net/proje ...

  10. web 开发之酷炫--- 酷炫展示

    http://www.cnblogs.com/dsxniubility/p/4588560.html