[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 ...
随机推荐
- apache、mod_jk负载均衡与tomcat集群
最近需要搭建apache和tomcat的集群,实现静态网站直接通过apache访问,动态网站转交给tomcat处理,实现负载均衡和tomcat集群配置. apache安装 wget http://ap ...
- like的性能问题
使用like'%匹配的文字%',无法优化,因为索引起不到作用. 不过like'匹配的文字%',索引起作用.
- Intent.Action
1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始.比较常用. Input:nothing Outpu ...
- SqlSugar-事务操作
一.事务操作实例 特别说明: 1.特别说明:在事务中,默认情况下是使用锁的,也就是说在当前事务没有结束前,其他的任何查询都需要等待 2.ReadCommitted:在正在读取数据时保持共享锁,以避免脏 ...
- iOS navigationBar导航栏底部与self.view的分界线的隐藏
ios开发中经常碰到各种需求,比如要求导航栏的颜色和self.view的颜色一样,当我们直接设置navigationBar的颜色和view一样时,我们会发现navigationBar还会有一条分割线留 ...
- 转 C#开发微信门户及应用(2)--微信消息的处理和应答
微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习下微信的相关开发,也就成为计划的安排事情之一了.本系列文章希望从一个循序渐进的角度上,全面介绍微 ...
- 网页登陆校验码C#版代码
[DefaultProperty("Text")] [ToolboxData("<{0}:ValidateCode runat=server></{0} ...
- cx_Oracle使用方法二
下载地址: https://pypi.python.org/pypi/cx_Oracle/5.2.1, 下载的时候注意数据库版本和操作系统环境. 技术手册: http://cx-oracle.read ...
- 关于【键鼠<局域网>共享软件:synergy】install
Installation 另外,本人在centos6.5环境下作为server运行时,遇到一个问题,synergy1.5随着系统升级居然变成了1.3X,所以如果遇到类似问题,请您先用 rpm -qa ...
- 通过Servlet的response绘制页面验证码
java部分 package com.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; ...