这两道题明显是一对。。。放一起 题目一 Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 大意: 就是把一个1~3999的阿拉伯数字,变成罗马数字表示。 题目二 Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 大意: 就是把一个1~3999的罗马数字,表示成阿拉伯数字。 […]

题目 Write a function to find the longest common prefix string amongst an array of strings. 大意: 写一个函数,找到一组字符串的公共最长前缀。 思路 我的:定义一个不断更新的变量,存储当前的最长前缀,循环时加以一定的优化,比如如果下一个字符串比当前的前缀还长,那么可以截断前缀后在进行比较。 参考的:这道题在discuss上看到了一些运用了python语法的解法,用到了zip,reduce,set等python特有的语法,顺便贴出。 代码 原创(Python): class Solution(object): def longestCommonPrefix(self, strs): “”” :type strs: List[str] :rtype: str “”” if len(strs) == 0: return “” if len(strs) == 1: return strs[0] <!–more–> com_str = list(strs[0]) for i in range(1, […]

题目 Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string “23” Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”]. Note: Although the above answer is in lexicographical order, your answer could […]

题目 Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. 大意: 有n个非负整数,代表地势图的高度,如图,计算下雨后这个地形能存多少雨水。 思路 这道题和第10题非常像,分别从最左边和最右边开始,定义两个游标,移动到过程中,记录左边的最大值和右边的最大值,然后用两个最大值中的小值减去地形高度,就是所积的雨水值(类似第10题思路)。 关于两个游标怎么移动的问题,同第10题思路,还是移动当前最大值较小的边,比如:当前右边最大值right_max为10,左边最大值left_max为5,如果移动right_index,那么左边的最大值只能保持不变,不管right_max变不变,两个最大值的较小一个也还会是left_max,没有可能增加积水量,所以可能会存在错误。(详细可结合代码看) 代码 Python class Solution(object): def trap(self, height): “”” :type height: List[int] :rtype: int <!–more–> “”” if […]

这几天折腾伽利略开发板,写下一些吐槽,以便以后再看到这篇博客,仍然能记起这些值得吐槽的地方。。。也希望看到这篇博客的人能够因此少些吐槽( ╯□╰ )。。网上其实是有不少资源的,但是相对其他更热门的板子,显然找资料并不是和喝白开水一样简单,由于我表达能力较差,所以准备采用简述+大量站外链接的形式,给出一个希望不会让人误入歧途的引导。。。 介绍 Intel Galileo(点击查看中文官网),是一个兼容Arduino的x86平台的开源硬件产品。个人感觉应当注意的是,特点就是兼容Arduino和x86,这样我们既能够利用丰富的Arduino软硬件及社区资源,又能够在上边运行linux甚至windows系统,做出更复杂的系统。 相关介绍: 《x86 版的 Arduino 来了,Intel Galileo 开发板的体验、分析和应用【超长文多图】》(http://www.ifanr.com/388835 ) 《系出名門:Intel Galileo的十大特性》(http://www.leiphone.com/news/201406/intel-galileo.html ) (更多请自行百度/google) “Arduino模式” 略(自行百度) Arduino? no,Linux! Galileo可以在SD卡中装入完整版Linux镜像,一旦在装有完整版Linux镜像的SD卡插入时启动,会进入所安装的完整版Linux系统,否则,则会进入烧写入flash的裁剪版微型Linux系统。而只有安装了完整版Linux,Galileo才不仅仅只是一个Arduino。下图为Galileo开机启动过程: 参考: 《x86 版的 Arduino 来了,Intel Galileo 开发板的体验、分析和应用【超长文多图】》(http://www.ifanr.com/388835 )

题目 The string “PAYPALISHIRING” is written in a zigzag pattern on a given >number of rows like this: (you may want to display this pattern in a >fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: “PAHNAPLSIIGYIR” Write the […]

题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 大意:就是把数字倒过来。。。 思路和吐槽 只有吐槽:用Python做简直是太简单了,简直就是在作弊,,,如果我十天之内没用c++再做一遍,我肯定是白做了这道题 代码 Python class Solution(object): def reverse(self, x): “”” :type x: int :rtype: int “”” if x < 0: symbol = -1 else: symbol = 1 x = abs(x) str_x = str(x) ans_list = list(str_x) […]

题目: Implement atoi to convert a string to an integer. 大意:就是手动实现C语言里常用的atoi函数。。。 思路和吐槽: 我最讨厌这种了,完全的信息不对称。。要讨论的情况只有不断提交才知道出题人什么意思,我怎么知道你要求的是什么,况且这是算法题又不是工程题。。。。幸亏这道题比较简单,多试几次也不太费时。 代码 Python class Solution(object): def myAtoi(self, str): “”” :type str: str :rtype: int “”” str = str.strip() if not str: return 0 ans = [] neg = “-” pos = “+” ch = str[0] if ch == neg: c = -1 elif […]

题目 Determine whether an integer is a palindrome. Do this without extra space. 大意:判断一个整数是不是回文的。。 吐槽: 同第7题:用Python简直就是作弊啊。。。。。 代码 Python class Solution(object): def isPalindrome(self, x): “”” :type x: int :rtype: bool “”” x = str(x) x = x.strip() if x[::1] == x[::-1]: return True else: return False