• [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;
        }
    }
  • 相关阅读:
    [No0000199]设计模式总结
    [No0000197]Windows用户都应该知道的运行命令
    [No000017F]如何监控注册表的修改
    [No0000196]一文读懂Java 11的ZGC为何如此高效
    [No0000195]NoSQL还是SQL?这一篇讲清楚
    [No0000194]聊聊 Chrome DevTools 中你可能不知道的调试技巧
    [No000018A]改善C#程序的建议11-20
    [No000018C]Vim清除上次的搜索高亮结果-Vim使用技巧(1)
    [No000018D]Vim快速注释/取消注释多行的几种方法-Vim使用技巧(2)
    [No000018E]Vim快速跳转任意行、任意列以及高亮显示当前行、当前列方法-Vim使用技巧(3)
  • 原文地址:https://www.cnblogs.com/airwindow/p/4778243.html
Copyright © 2020-2023  润新知