题目如下:

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 letter O, and the digit 1 with the letter I.  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 integer N, return the Hexspeak representation of N 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的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  5. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  6. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  7. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  8. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  9. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

随机推荐

  1. OpenGL_构建GLFW与第一个程序

    参考教程:https://learnopengl-cn.github.io/ 这个教程已经给出了很详细的资料,当然我这里是对细节的展示(在Windows上). 首先,你需要准备 VS2017 : ht ...

  2. 【Linux开发】linux设备驱动归纳总结(八):3.设备管理的分层与面向对象思想

    linux设备驱动归纳总结(八):3.设备管理的分层与面向对象思想 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  3. 【Linux开发】linux设备驱动归纳总结(四):1.进程管理的相关概念

    linux设备驱动归纳总结(四):1.进程管理的相关概念 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  4. MSF魔鬼训练营-5.3 MS08-067安全漏洞实战

    msf > search ms08_067 Matching Modules ================    Name                                 D ...

  5. Linux学习笔记(15)Linux字符集(locale,LANG,LC_ALL)

    关键词:linux系统修改编码,linux字符集问题, 目录  零.什么是locale 一.locale的详细内容 二.理解locale的设置 三 具体设定locale的方法(zh_CN.UTF-8. ...

  6. Ubuntu - Ubuntu应用记录(2)

    1.安装Ubuntu16.04的一种分区分案(240G固态硬盘小例) 1.创建boot分区(引导分区)-> 512M ->逻辑分区->空间起始位置->Ext4日志文件系统-&g ...

  7. 【图论好题】ABC #142 Task F Pure

    题目大意 给定一个 $N$ 个点 $M$ 条边的有向图 $G$,无重边.自环.找出图 $G$ 的一个导出子图(induced subgraph) $G'$,且 $G'$ 中的每个点的入度和出度都是 1 ...

  8. 福建工程学院第十四届ACM校赛J题题解

    第六集,想不到你这个浓眉大眼的都叛变革命了 题意: 给你两个只包含01的字符串S和T,问你在允许一次错误的情况下,T是否能成为S的子串 思路: 这个问题的解法挺多,我是用fft匹配的,也比较简单,针对 ...

  9. 关于记录log日志的几种方法

    最近在记录日志的时候总结了几种方式: 1.使用log4j2记录 2.使用log4j记录 3.使用logback配置,记录前使用 private  final Logger logger = Logge ...

  10. 对ArrayList中的Person对象按照先年龄从大到小,相同年龄的再按照姓名(姓名是英文的)的字母顺序进行排序.

    ListDemo2.java ----------------- package com.fs.test; import java.util.ArrayList; import java.util.C ...