【leetcode】1271. Hexspeak
题目如下:
A decimal number can be converted to its Hexspeak representation by first converting it to an uppercase hexadecimal string, then replacing all occurrences of the digit
0
with the letterO
, and the digit1
with the letterI
. Such a representation is valid if and only if it consists only of the letters in the set{"A", "B", "C", "D", "E", "F", "I", "O"}
.Given a string
num
representing a decimal integerN
, return the Hexspeak representation ofN
if it is valid, otherwise return"ERROR"
.Example 1:
Input: num = "257"
Output: "IOI"
Explanation: 257 is 101 in hexadecimal.Example 2:
Input: num = "3"
Output: "ERROR"Constraints:
1 <= N <= 10^12
- There are no leading zeros in the given string.
- All answers must be in uppercase letters.
解题思路:转成十六进制后,把0/1分别替换成O/I,然后检查字符串中是否包含 "A", "B", "C", "D", "E", "F", "I", "O" 以外的字符。
代码如下:
class Solution(object):
def toHexspeak(self, num):
"""
:type num: str
:rtype: str
"""
num = hex(int(num))[2:]
num = num.upper()
num = num.replace('','O')
num = num.replace('', 'I')
valid = ["A", "B", "C", "D", "E", "F", "I", "O"]
for i in num:
if i not in valid:return "ERROR"
return num
【leetcode】1271. Hexspeak的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- webdriervAPI(操作cookie)
from selenium import webdriver driver = webdriver.Chorme() driver.get("http://www.baidu.co ...
- Python input/output boilerplate for competitive programming
The following code is my submission for Codeforces 1244C The Football Season. import io import sys i ...
- 手写Spring MVC
闲及无聊 又打开了CSDN开始看一看有什么先进的可以学习的相关帖子,这时看到了一位大神写的简历装X必备,手写Spring MVC. 我想这个东西还是有一点意思的 就拜读了一下大佬的博客 通读了一遍相关 ...
- 使用TableSnapshotInputFormat读取Hbase快照数据
根据快照名称读取hbase快照中的数据,在网上查了好多资料,很少有资料能够给出清晰的方案,根据自己的摸索终于实现,现将代码贴出,希望能给大家有所帮助: public void read(org.apa ...
- loj 2336「JOI 2017 Final」绳
loj 首先,所有位置最多被染色一次,因为要染多次的话,还不如一开始就染成最终的颜色.并且你可以一开始就染好色 因为最终长度为2,那么如果染完后这个序列可以被折完,那么首先最多只有两种颜色,还有就是要 ...
- L2Dwidget.js L2D网页动画人物添加
hexo 添加live2d看板动画 https://www.jianshu.com/p/3a6342e16e57 首先贴出官网代码 官网地址配置:https://www.npmjs.com/packa ...
- 进程管理工具之supervisor[详解]
原文链接:https://blog.csdn.net/weixin_42390791/article/details/88866237 一.问题背景1.背景 如何才能让一个进程摆脱终端,获得相对 ...
- mybatis-generator的功能扩展
项目代码地址:https://github.com/whaiming/java-generator 我在原有的基础上扩展了和修改了一些功能: 1.增加获取sqlServer数据库字段注释功能 2.Ma ...
- 访问接口错误,com.netflix.client.ClientException: Load balancer does not have available server for client: panfeng-item-service
com.netflix.client.ClientException: Load balancer does not have available server for client: panfeng ...
- Delphi 参数的传递