Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. [#3]
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
题意:找出字符串中没重复字符的最长子串的长度。
>>> tmpstr = lambda s:[s[i:j] for i in range(len(s)) for j in range(i+1,len(s)+1)] >>> result = lambda s:max([len(i) for i in tmpstr(s) if len(i)==len(set(i))]) >>> substr = lambda s:[i for i in tmpstr(s) if len(i)==result(s)==len(set(i))] >>> s1 = 'abcabcbb' >>> result(s1) 3 >>> substr(s1) ['abc', 'bca', 'cab', 'abc'] >>> s2 = 'bbbbb' >>> result(s2) 1 >>> substr(s2) ['b', 'b', 'b', 'b', 'b'] >>> s3 = 'pwwkew' >>> result(s2) 1 >>> substr(s3) ['wke', 'kew']
注: 匿名函数tmpstr(s)中用到的推导式,可以求出字符串、列表等序列的所有子序列:
>>> s='abcd' >>> [s[i:j] for i in range(len(s)) for j in range(i+1,len(s)+1)] ['a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd'] >>> 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]] >>>
记牢这个“子序列推导式 ”,解题中经常可以用到它。
Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
You may assume nums1 and nums2 cannot be both empty. [#4]
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
中位数概念:一组经排序后的数据,元素奇数个时取最中间的那个,偶数个时取最中间2个数的平均值。
>>> median = lambda n:n[len(n)//2] if len(n)%2 else (n[len(n)//2-1]+n[len(n)//2])/2 >>> result = lambda m,n:(median(m)+median(n))/2 >>> >>> nums1 = [1, 3] >>> nums2 = [2] >>> result(nums1, nums2) 2.0 >>> >>> nums1 = [1, 2] >>> nums2 = [3, 4] >>> result(nums1, nums2) 2.5 >>>
Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. [#7]
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
题意:给定一个32位有符号整数,反转各位上的数字。
>>> revnum = lambda n:int(str(abs(n))[::-1])*(1 if n>0 else -1) >>> revnum(123) 321 >>> revnum(-123) -321 >>> revnum(120) 21 >>>
如不允许把数字转字符串处理,则用%和//逐位取出数字再作运算:
>>> def Palindrome(number): if number<0: return -Palindrome(-number) res = [] while number != 0: res.append(number%10) number //= 10 return sum([j*10**i for i,j in enumerate(res[::-1])]) >>> Palindrome(123) 321 >>> Palindrome(-123) -321 >>> Palindrome(120) 21 >>> Palindrome(0) 0 >>>
相关单词
string : 字符串
英 [strɪŋ] 美 [strɪŋ]
n.一串;线;细绳;带子;一系列;一连串;一批
vt.悬挂;系;扎;用线(或细绳等)串,把…连在一起;给…装弦
adj.由弦乐器组成的;弦乐器的;线织的;线的
substring : 子串 a string that is part of a longer string
求子串;子字串;取子串;截取子串;取子字符串
前缀sub- 表示“在下面,次一等”或顺位在后的,次等顺序的,即为“附属的”
median : 中值
英 [ˈmiːdiən] 美 [ˈmiːdiən]
adj.中值的;中间的;中间值的;在中间的;通过中点的
n.中位数;(三角形的)中线
词根-med- 中间
后缀-ian …的
respectively : 分别地
英 [rɪˈspektɪvli] 美 [rɪˈspektɪvli]
adv.分别地;分别;各自;顺序为;依次为
前缀re- 一再
词根-spect- 看
后缀-ive 表有...性质的、有...作用的、有...倾向的、属于...的
overall : 总体的
英 [ˌəʊvərˈɔːl , ˈəʊvərɔːl] 美 [ˌoʊvərˈɔːl , ˈoʊvərɔːl]
adj.全面的;综合的;总体的
adv.全部;总计;一般来说;大致上;总体上
n.外套;罩衣;工装连衣裤;工装服
complexity : 复杂度
英 [kəmˈpleksəti] 美 [kəmˈpleksəti]
n.复杂性;难懂;难题;难以理解的局势
前缀com- 加强
词根-plex- 重叠,折叠
后缀-ity 具备某种性质,状况