- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址:https://leetcode.com/problems/happy-number/open in new window
Total Accepted: 36352 Total Submissions: 109782 Difficulty: Easy
Write an algorithm to determine if a number is "happy".
Ahappy 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:
Input: 19
Output: true
Explanation:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
判断一个数字是不是开心的数字,所谓开心数字,就是把它的每一位数字求平方和之后构成新数字,然后继续这个操作,看最后能不能到1.
使用递归的方法。
我自己的算法,10以下的Happy Number 只有 1和7 ,如果一个数计算到只有个位数时,如果计算到十位以下,这个数是1或7,返回true,否则,返回false。
public static boolean isHappy(int n) {
int ans = 0;
if (n == 1 || n == 7) {
return true;
} else if (n > 1 && n < 10) {
return false;
} else {
String numString = "" + n;
char numChar[] = numString.toCharArray();
for (char aNumChar : numChar) {
ans += (aNumChar - '0') * (aNumChar - '0');
}
}
return isHappy2(ans);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
方法一改进:
没必要10以下的数字啊,1到7之间的都是false。直接判断数到1和7之间 就false就好了。
7通过计算也回到1。
public static boolean isHappy(int n) {
int ans = 0;
if (n == 1) {
return true;
} else if (n > 1 && n < 7) {
return false;
} else {
String numString = "" + n;
char numChar[] = numString.toCharArray();
for (char aNumChar : numChar) {
ans += (aNumChar - '0') * (aNumChar - '0');
}
}
return isHappy5(ans);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
同计算循环小数一样, 如果出现循环, 则无需继续计算,直接返回false即可.
每次计算时,把已经计算数放到一个集合里面,在计算过程中如果出现循环(集合里已经有这个数字),返回false。否则一直计算。
class Solution(object):
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
visited = set()
while n not in visited:
visited.add(n)
nx = 0
while n != 0:
nx += (n % 10) ** 2
n //= 10
if nx == 1:
return True
n = nx
return False
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
迭代的C++代码如下:
class Solution {
public:
bool isHappy(int n) {
unordered_set<int> visited;
visited.insert(n);
while (n != 1) {
int pre = n;
int next = 0;
while (pre) {
next += (pre % 10) * (pre % 10);
pre /= 10;
}
n = next;
if (visited.count(n))
break;
visited.insert(n);
}
return n == 1;
}
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
The net closely but buckle not forever. 十指紧扣却扣不住天长地久。 Can your self-esteem and my stubborn reconci
Shout it from the sea. 来自深海的呼喊 I've been hdding their cry to helpless. 我曾抱着自己哭到无助 My happiness is
我正在尝试使用 x = 4 和 pritn x 实现一些语言,使用 haskell 构建快乐到目前为止,我已经定义了这样的语法 terms : term
我正在尝试使用 Happy(Haskell 工具)进行解析器,但收到一条消息错误:“未使用的规则:11 和未使用的终端:10”,我不知道这意味着什么。另一方面,我真的不确定规则语句中 $i 参数的使用
在 Haskell 中探索解析库我遇到了这个项目:haskell-parser-examples .运行一些示例,我发现运算符优先级存在问题。使用 Parsec 时效果很好: $ echo "3*2+
蛋糕不仅需要美味,外观也很重要。既好吃又好看的蛋糕才能够让人赏心悦目,送上一组好看的生日蛋糕图片给大家,希望你们会喜欢。
题目地址:https://leetcode.com/problems/happy-number/open in new window Total Accepted: 36352 Total Subm
我正在用 Happy 构建一个解析器,并注意到这是 online documentation : Like yacc, we include %% here, for no real reason.
我正在使用 happy.js 来验证表单。对于此表单,我需要用户提交五个可能的密码之一,如果用户未正确提交任何密码,则表单必须失败。 不知道我在这里做错了什么。感谢任何帮助。代码如下: 前面:
我正在基于 this BNF definition 在 happy 上制作一个简单的命题逻辑解析器命题逻辑语法,这是我的代码 { module FNC where import Data.Char i
我目前正在使用 happy 解析器生成器。 其他解析器生成器可以提供不错的消息,例如“意外的结束行,预期的‘然后’”。很高兴我只得到当前的 token 和错误的位置。 您能给我一个如何获取上述错误消息
我正在尝试安装 Fay。 Fay 依赖于 pretty-show,所以我正在安装它。当我运行 cabal install pretty-show 时,出现以下错误: Resolving depende
我的代码可以工作,但计数不正确。例如,它将 22 和 208 视为 happy numbers ,但他们不是。乘法结果也是错误的:42之后不是20,而是45 function isHappy(
我正在用 Happy 编写一个 JavaScript 解析器我需要匹配一个正则表达式。我不想完全解析正则表达式,只是将其存储为字符串。 我的 AST 的相关部分如下所示: data PrimaryEx
我正在尝试使用 Happy.js 在客户端验证表单。 基本验证工作正常,但我有一个稍微复杂的部分,我需要验证它有点复杂。本质上,有一个是/否单选框,如果是,则显示更多输入标签。仅当单选按钮为是时才需要
这里我有一个函数,它应该将一个数字 n 放入 disHappy(n) 以检查是否所有 [n-0) 中的 n 个很高兴。 Happy Numbers wikipedia 如果我只运行 happyChec
题目地址:https://leetcode-cn.com/problems/longest-happy-string/ 题目描述 如果字符串中不含有任何 'aaa','bbb' 或 'ccc' 这
我正在为一种实验语言进行语义分析。我正在使用 Alex 和 Happy 来生成词法分析器和解析器(实际上我正在使用 BNFC 工具来生成 Alex 和 Happy 文件)。每当出现语义错误(例如类型错
我正在做一个学校项目,我必须在 Haskell 中使用 Alex + Happy 构建一个简单语言的解释器。 浏览完文档后,我了解了大部分内容,但希望看到有关使用这些工具的完整示例。 最佳答案 不是构
我有以下(大量精简的)快乐语法 %token '{' { Langle } '}' { Rangle } '..' { DotDot } '::' { ColonC
我是一名优秀的程序员,十分优秀!