[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安装Jdk,CentOS安装Jdk
Linux安装Jdk,CentOS安装Jdk >>>>>>>>>>>>>>>>>>>& ...
- SharePoint SiteCollection Administrator
到网上去找怎么取到一个站点的sitecollection Administrator, 如果设置了一个站点的 sitecollection Administrator, 那么通过: SPSite ...
- P2P金融的概念理解
P2P金融又叫P2P信贷.其中,P2P是 peer-to-peer 或 person-to-person 的简写, 意思是:个人对个人. P2P金融指个人与个人间的小额借贷交易,一般需要借助电子商务专 ...
- ORACLE如何停止一个JOB
ORACLE如何停止一个JOB1 相关表.视图2 问题描述为同事解决一个因为网络连接情况不佳时,执行一个超长时间的SQL插入操作.既然网络状况不好,就选择了使用一次性使用JOB来完成该插入操作.在JO ...
- C#中如何正确的操作字符串?
字符串应该是所有编程语言中使用最频繁的一种基础数据类型.如果使用不慎,我们就会为一次字符串的操作所带来的额外性能开销而付出代价.本条建议将从两个方面来探讨如何规避这类性能开销: 1. 确保尽量少的装箱 ...
- bootstrap上传表单的时候上传的数据默认是0 一定要小心
bootstrap上传表单的时候上传的数据默认是0 一定要小心
- HTML5-javascript屏幕旋转事件:onorientationchange
屏幕旋转事件:onorientationchange 添加屏幕旋转事件侦听,可随时发现屏幕旋转状态(左旋.右旋还是没旋) 判断屏幕是否旋转 function orientationChange() { ...
- AS3.0函数定义的方法
在AS3.0中函数的定义有两种方法: 函数语句定义法: function 函数名(参数1:参数类型,参数2:参数类型):返回值类型{ 函数折行的语句 } function testAdd(a:int, ...
- interview:about Oracle表空间
Oracle表空间 SQL Server数据库与Oracle数据库之间最大的区别要属表空间设计.Oracle数据库开创性地提出了表空间的设计理念,这为Oracle数据库的高性能做出了不可磨灭的贡献.可 ...
- linux下安装busybox
1.获取busybox源码并解压,这里使用天嵌提供的“busybox-1.16.0.tar.bz2” #tar xvf busybox-.tar.bz2 -C / 解压的目的地址实际上是:/opt/E ...