zoukankan      html  css  js  c++  java
  • [LeetCode] 1134. Armstrong Number

    Given an integer n, return true if and only if it is an Armstrong number.

    The k-digit number n is an Armstrong number if and only if the kth power of each digit sums to n.

    Example 1:

    Input: n = 153
    Output: true
    Explanation: 153 is a 3-digit number, and 153 = 13 + 53 + 33.
    

    Example 2:

    Input: n = 123
    Output: false
    Explanation: 123 is a 3-digit number, and 123 != 13 + 23 + 33 = 36.

    Constraints:

    • 1 <= n <= 108 

    阿姆斯特朗数。

    假设存在一个 k 位数 N,其每一位上的数字的 k 次幂的总和也是 N,那么这个数是阿姆斯特朗数。

    给你一个正整数 N,让你来判定他是否是阿姆斯特朗数,是则返回 true,不是则返回 false。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/armstrong-number
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这是一道带锁的题,只是为了留一个记录以免以后看不到题目。思路不难,直接给代码。

    时间O(n) - 数字的位数

    空间O(1)

    Java实现

     1 class Solution {
     2     public boolean isArmstrong(int n) {
     3         String num = String.valueOf(n);
     4         int len = num.length();
     5 
     6         int nn = n;
     7         long res = 0;
     8         while (nn != 0) {
     9             res += Math.pow(nn % 10, len);
    10             nn /= 10;
    11         }
    12         return (int) res == n;
    13     }
    14 }

    LeetCode 题目总结

  • 相关阅读:
    django 在保存数据前进行数据校验
    itertools
    python 发送请求
    python 异常处理
    python 对redis 键值对的操作
    python 对redis key的基本操作
    python 操作redis数据
    一只青蛙一次可以跳1阶或者2阶,n阶,有多少种到达终点的方式。
    Django 自定义扩展命令
    关于函数可变参数
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14965353.html
Copyright © 2011-2022 走看看