题目地址:https://leetcode-cn.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/

题目描述

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

Given a positive integer N, return true if and only if it is an Armstrong number.

Example 1:

Input: 153
Output: true
Explanation:
153 is a 3-digit number, and 153 = 1^3 + 5^3 + 3^3.

Example 2:

Input: 123
Output: false
Explanation:
123 is a 3-digit number, and 123 != 1^3 + 2^3 + 3^3 = 36.

Note:

  1. 1 <= N <= 10^8

题目大意

给你一个整数数组 A,请找出并返回在该数组中仅出现一次的最大整数。

如果不存在这个只出现一次的整数,则返回 -1。

解题方法

直接计算

先算k,然后判断即可。

C++代码如下:

class Solution {
public:
bool isArmstrong(int N) {
int k = 0;
int temp = N;
while (temp != 0) {
k++;
temp /= 10;
}
long long res = 0;
temp = N;
while (temp != 0) {
int mod = temp % 10;
res += pow(mod, k);
temp /= 10;
}
return res == N;
}
int pow(int N, int k) {
int res = 1;
while (k --) {
res *= N;
}
return res;
}
};

日期

2019 年 9 月 18 日 —— 今日又是九一八

最新文章

  1. 在MySQL的InnoDB存储引擎中count(*)函数的优化
  2. IOS应用内存释放机制
  3. mysql xml 参数
  4. Fragment的onResume
  5. nginx + Lua 实现自定义WAF
  6. 【转载】CMake 简介和 CMake 模板
  7. 基本API-StdIn.java
  8. OSI七层模型理解
  9. java中的移位运算符:&lt;&lt;,&gt;&gt;,&gt;&gt;&gt;总结(转)
  10. Flask知识点二
  11. java基本语法特殊点
  12. PAT1031:Hello World for U
  13. SAwUML – UML-based, contractual software architectures and their formal analysis using SPIN
  14. JAVA框架之Hibernate框架的学习步骤
  15. Servlet案例6:显示用户的上次访问时间
  16. 将音乐生成波浪图形,JavaScript Html5
  17. 22-Python3 输入和输出
  18. 动态BT跳转
  19. php date时间的获取
  20. Dataguard学习笔记

热门文章

  1. bcftools 提取vcf(snp/indel)文件子集
  2. Docker-Mysql-proxy Mysql Proxy实现读写分离
  3. 大数据学习day14-----第三阶段-----scala02------1. 元组 2.类、对象、继承、特质 3.函数(必须掌握)
  4. 零基础学习java------day9------多态,抽象类,接口
  5. 【Python】【Basic】【数据类型】运算符与深浅拷贝
  6. 搭建内网Yum源
  7. SpringAOP浅析
  8. Spring(1):Spring介绍
  9. Maven项目打包成war包并启动war包运行
  10. Spring 与 SpringBoot 的区别