Palindrome Number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. [#9]
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
题意:判断一个整数是否为回文数? 后续:能否不转成字符串就解决问题。
>>> result = lambda n:n==int(str(n)[::-1]) if n>=0 else False >>> result(121) True >>> result(-121) False >>> result(10) False >>>
如不允许转字符串,可以用%和//逐位取出数字再作判断。
>>> def isPalindrome(number): if number<0: return False res = [] while number != 0: res.append(number%10) number //= 10 return res==res[::-1] >>> isPalindrome(121) True >>> isPalindrome(-121) False >>> isPalindrome(10) False >>> isPalindrome(0) True >>>
Container With Most Water
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai).n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. [#11]
Note: You may not slant the container and n is at least 2.
The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
题意:找出序列中的元素所能围成矩形的最大面积值。
再次用到“子序列推导式”,只是本题不需要单个元素的子序列,很简单只要把后一个range()的第一参数i+1改成i+2:
>>> s=[1,2,3] >>> [s[i:j] for i in range(len(s)) for j in range(i+1,len(s)+1)] [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]] >>> [s[i:j] for i in range(len(s)) for j in range(i+2,len(s)+1)] [[1, 2], [1, 2, 3], [2, 3]] >>>
到得的子序列只要把首尾两元素的小者乘以(子序列长度-1)就是面积,其面积的最大值就是最终答案:
>>> maxArea = lambda s:max([min(t[0],t[-1])*(len(t)-1) for t in [s[i:j] for i in range(len(s)) for j in range(i+2,len(s)+1)]]) >>> L = [1,8,6,2,5,4,8,3,7] >>> maxArea(L) 49 >>>
Roman to Integer
Roman numerals are represented by seven different symbols: I , V , X , L , C , D and M .
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII , which is simply X + II . The number twenty seven is written as XXVII , which is XX + V + II .
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII . Instead, the number four is written as IV . Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX .
There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. [#13]
题意:把罗马数字的字符串转换成一个十进制整数。
>>> def roman2int(n): last = total = 0 for i in range(len(n)): c = n[len(n)-(i+1):len(n)-i] num = c in 'IXCM' and 10**'IXCM'.index(c) or c in 'VLD' and 5*10**'VLD'.index(c) total += num*(-1 if num < last else 1) last = num return total >>> roman2int('III') 3 >>> roman2int('IV') 4 >>> roman2int('IX') 9 >>> roman2int('LVIII') 58 >>> roman2int('MCMXCIV') 1994 >>> roman2int('MMXXI') 2021 >>>