[LeetCode#202] Roman to Integer
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的更多相关文章
- [LeetCode][Python]Roman to Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...
- 【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 ...
- 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 ,即 ...
- leetcode:Roman to Integer and Integer to Roman
2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- [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 ...
- 【leetcode】Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- 【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 ...
随机推荐
- Linux查看某个文件被哪些进程PID占用
比如查看test.sh这个文件被哪些进程占用,可以用: lsof | grep test.sh 或者 fuser -v test.sh
- 【原创教程】鲸吞HTML
首先,我们的angularJS课程分为三大模块: HTML/CSS/JS基础. angularJS详解. angualrJS的一些实用框架讲解. 其中,第一大模块的对象是对前端开发技术有点了解但不熟悉 ...
- js--小结②
- Navicat 看历史执行SQL
Navicat可以通过这个框口看手动操作所执行的代码操作
- CI框架篇之控制器篇--设置路由(1)
CodeIgniter 定义默认控制器 当你的网站不存在某个URI 或者 用户直接从根目录访问的时候,CodeIgniter 会加载默认控制器. 打开 application/config/route ...
- [Mime] MimeReader--读取Mime的帮助类 (转载)
点击下载 MimeReader.rar 这个类是关于MimeReader的帮助类看下面代码吧 /// <summary> /// 类说明:Assistant /// 编 码 人:苏飞 // ...
- 使用openrowset跨库查询
--insert fj_studentinfo--select *--from-- openrowset('SQLOLEDB','localhost';'sa';'password',dbname. ...
- Win7使用IIS通过域名访问本地程序(网页、css、js等)
一.目的:在本地浏览器里面,输入www.abc.com 可以访问我们本地搭建的网页程序 二.好处:在本地模拟,真实的访问,另外可以设置一些二级域名,例如static.abc.com域名用来存储像图片, ...
- JavaEE web.xml 中ContextLoaderListener的解析
ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在 ...
- ios专题 - 使用bundle文件管理资源
[原创]http://www.cnblogs.com/luoguoqiang1985 以前,自己写程序,图片等资源放得比较乱.后来,发现有个更好的方法来管理图片等资源文件 --bundle文件. 1) ...