Problem:

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

Analysis:

This problem is actually very very easy!
The problem has actually described the algorithm very clearly! We just need to implement it! Choice: you want to do all those operations
82 : 8^2 + 2^2 = 68
in one step or not?
If it was done in one step, in one loop,
we need to first get each digit and sum the square of them together.
The digit operation is always hard to implement compared with other logic, we should not mix them together. Why not separte those two major operation out?
Step 1: Get the digit array of a integer.
private int[] get_array(int n) {
String str = String.valueOf(n);
int len = str.length();
int[] ret = new int[len];
for (int i = 0; i < len; i++) {
int digit_weight = (int)Math.pow(10, len-i-1);
ret[i] = n / digit_weight;
n = n % digit_weight;
}
return ret;
} Skill: firstly convert n into string type, then we can get the length information through the str.length().
String str = String.valueOf(n);
int len = str.length();
int[] ret = new int[len]; Step 2: Sum each digit of the int array together.
private int sum(int[] a) {
int ret = 0;
for (int i = 0; i < a.length; i++)
ret += a[i] * a[i];
return ret;
} Main:
According to the description, the number would end up with "1" or a circular digital sequence.
If the circular situation happens, we definitely not want to avoid the infinite loop.
Use our old friend : HashSet, we could easily achieve that point.
HashSet<Integer> hash_set = new HashSet<Integer> ();
while (!hash_set.contains(n)) {
hash_set.add(n);
n = sum(get_array(n));
if (n == 1)
return true; }
return false;

Solution:

public class Solution {
public boolean isHappy(int n) {
if (n < 0)
throw new IllegalArgumentException("The passed in n is negative!");
HashSet<Integer> hash_set = new HashSet<Integer> ();
while (!hash_set.contains(n)) {
hash_set.add(n);
n = sum(get_array(n));
if (n == 1)
return true;
}
return false;
} private int[] get_array(int n) {
String str = String.valueOf(n);
int len = str.length();
int[] ret = new int[len];
for (int i = 0; i < len; i++) {
int digit_weight = (int)Math.pow(10, len-i-1);
ret[i] = n / digit_weight;
n = n % digit_weight;
}
return ret;
} private int sum(int[] a) {
int ret = 0;
for (int i = 0; i < a.length; i++)
ret += a[i] * a[i];
return ret;
}
}

[LeetCode#202] Roman to Integer的更多相关文章

  1. [LeetCode][Python]Roman to Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...

  2. 【LeetCode】Roman to Integer & Integer to Roman

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  3. Leetcode#13. Roman to Integer(罗马数字转整数)

    题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...

  4. leetcode:Roman to Integer and Integer to Roman

    2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...

  5. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  6. leetcode:Roman to Integer(罗马数字转化为罗马数字)

    Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...

  7. [LeetCode] 13. Roman to Integer 罗马数字转化成整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  8. 【leetcode】Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  9. 【JAVA、C++】LeetCode 013 Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. JavaScript中一些你不一定知道的问题(持续更新中。。。。)

    一些js的问题与解析 1) ["1","2","3"].map(parseInt);的运行结果是? A.["1",&qu ...

  2. windows server 2003 系统重装蓝屏

    错误码:0X0000007B 这个代码和硬盘有关系,不过不用害怕,不是有坏道了,是设置问题或者病毒造成的硬盘引导分区错误.如果您在用原版系统盘安装系统的时候出这个问题,那说明您的机器配置还是比较新的, ...

  3. 分布式Web服务器架构(转)

    最开始,由于某些想法,于是在互联网上搭建了一个网站,这个时候甚至有可能主机都是租借的,但由于这篇文章我们只关注架构的演变历程,因此就假设这个时候已经是托管了一台主机,并且有一定的带宽了,这个时候由于网 ...

  4. ie8中parseInt字符型数值转换数值型问题

    今天在ie8中测试项目发现一个奇怪的问题,"08" "09" 强转竟然变成了: 后来发现ie8把"08" "09" 默认 ...

  5. Python:元组(tuple)

    #!/usr/bin/python3 #元组 tup1 = ('Google', 'Runoob', 1997, 2000) print(type(tup1)) print("tup1 &q ...

  6. Apache提示You don't have permission to access / on this server问题解决

    测试时遇到将一本地目录设置为一apache的虚拟主机,在httpd-vhosts.conf文件中进行简单设置,然后在hosts文件中将访问地址指向本地,启动apache,进行访问,却出现了You do ...

  7. CSS+Javascript的那些框架

    CSS CSS 制作框架 SASS http://www.oschina.net/p/sass Blueprint  http://www.oschina.net/p/blueprintcss Ela ...

  8. AS3.0函数定义的方法

    在AS3.0中函数的定义有两种方法: 函数语句定义法: function 函数名(参数1:参数类型,参数2:参数类型):返回值类型{ 函数折行的语句 } function testAdd(a:int, ...

  9. yum命令学习

    yum配置文件 /etc/yum.conf yum check-update检查一下有无更新 每天都要(设置定时任务todo) 1.列出所有可更新的软件清单---yum check-update 2. ...

  10. hw-text1

    Text 1 测试题 python是什么类型的语言? 解释型语言,是脚本语言 百娘(脚本语言是为了缩短传统的编写-编译-链接-运行(edit-compile-link-run)过程而创建的计算机编程语 ...