【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
0with the letterO, and the digit1with 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
numrepresenting a decimal integerN, return the Hexspeak representation ofNif 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 ...
随机推荐
- Akka入门
原文:http://doc.akka.io/docs/akka/2.3.6/intro/getting-started.html 预备知识 AKKA要求你的计算机已经安装了Java1.6或更高版本. ...
- 上课笔记:awk
awk [单独的编程语言解释器]1.awk介绍 全称:Aho Weinberger Kernaighan 三个人的首字母缩写: 1970年第一次出现在Unix机器上,后来在开源领域使用它: 所以,我 ...
- 2019中山纪念中学夏令营-Day4[JZOJ]
Begin (题目的排序方式:难易程度) 什么是对拍: 对拍是一种在写完程序后,验证自己程序是不是正解的比较方便的方法. 实现过程: 对同一道题,再打一个暴力程序,然后用一些大数据等跑暴力程序来进行验 ...
- JS中this的4种绑定规则
this ES6中的箭头函数采用的是词法作用域. 为什么要使用this:使API设计得更简洁且易于复用. this即不指向自身,也不指向函数的词法作用域. this的指向只取决于函数的调用方式 thi ...
- 剑指Offer 1-41 代码(python实现)
今天主要写了一下offer 1-41题,余下的稍后整理 1 """ 1 镜像二叉树: 递归 """ def mirror(root): if ...
- SQL数据库字段数据类型详细说明
这里先总结数据类型.MySQL中的数据类型大的方面来分,可以分为:日期和时间.数值,以及字符串.下面就分开来进行总结. 日期和时间数据类型 MySQL数据类型 含义 date 3字节,日期,格式:20 ...
- 代码调试console对象的花式玩法
转自阮一峰http://www.ruanyifeng.com/home.html console.log(),console.info(),console.debug() console.log方法用 ...
- js 重要函数
1. Array.some some() 方法用于检测数组中的元素是否满足指定条件(函数提供) 如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测.如果没有满足条件的元素,则返 ...
- 2.bat文件的基本用途
一.调用可执行的jar文件. 详见:https://www.cnblogs.com/lukelook/p/8683352.html
- Linux中的grep 命令
介绍grep文本处理命令,它也可以解释正则. 常用选项: -E :开启扩展(Extend)的正则表达式. -i :忽略大小写(ignore case). -v :反过来(invert),只打印没有匹配 ...