注意,答案只是代表是他人写的代码,正确,但不一定能通过测试(比如超时),列举出来只是它们拥有着独到之处,虽然大部分确实比我的好
(发现最近光写解答没写思路了)
344. Reverse String
题目
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
思路与解答
这道题我以前没做过嘛?
return s[::-1]
答案
这个方案是分治
class Solution(object):
def reverseString(self, s):
l = len(s)
if l < 2:
return s
return self.reverseString(s[l/2:]) + self.reverseString(s[:l/2])
原地互换法,可是还是有新列表了啊
class SolutionClassic(object):
def reverseString(self, s):
r = list(s)
i, j = 0, len(r) - 1
while i < j:
r[i], r[j] = r[j], r[i]
i += 1
j -= 1
return "".join(r)
emmm
return ''.join(reversed(s))
541. Reverse String II
题目
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
Example:
Input: s = “abcdefg”, k = 2
Output: “bacdfeg”
Restrictions:
The string consists of lower English letters only.
Length of the given string and k will in the range [1, 10000]
思路与解答
描述上复杂了一些
经过测试我发现我理解错题目了,擦
根据条件不同采取不同的方法
l,i = len(s),len(s)
r,nr= list(s),[]
while i > 0:
if i >= 2*k:
nr += list(reversed(r[l-i:l-i+k])) + r[l-i+k:l-i+2*k]
i = i - 2*k
if i <= 0:return "".join(nr)
elif k <= i < 2*k:
nr += list(reversed(r[l-i:l-i+k])) + r[l-i+k:]
return "".join(nr)
elif i < k:
nr += list(reversed(r[l-i:]))
return "".join(nr)
感觉有非常大的优化空间
l,i = len(s),len(s)
r,nr= list(s),[]
while i > 0:
if i >= 2*k:
nr += list(reversed(r[l-i:l-i+k])) + r[l-i+k:l-i+2*k]
elif k <= i < 2*k:
nr += list(reversed(r[l-i:l-i+k])) + r[l-i+k:]
elif i < k:
nr += list(reversed(r[l-i:]))
i = i - 2*k
return "".join(nr)
答案
牛逼,看穿了题目要求
def reverseStr(self, s, k):
s = list(s)
for i in xrange(0, len(s), 2*k):
s[i:i+k] = reversed(s[i:i+k])
return "".join(s)
最近好多递归,厉害
return s[:k][::-1] + s[k:2*k] + self.reverseStr(s[2*k:], k) if s else ""
同样是一行,读起来有些费劲
return "".join(s[i*k: i*k+k][::(i&1)*2-1] for i in xrange(len(s)/k + 1))
345. Reverse Vowels of a String
题目
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = “hello”, return “holle”.
Example 2:
Given s = “leetcode”, return “leotcede”.
Note:
The vowels does not include the letter “y”.
思路与解答
题目是什么意思???
试了一下是只反转字符串中的aeiou?
指定字符的反转,怎么写呢
找清位置,逐个反转
r = "aeiouAEIOU"
d = {}
ns =list(s)
for x in r:
d[x] = x
l = [i for i,j in enumerate(s) if j in d]
i,j = 0,len(l)-1
while i < j:
ns[l[i]],ns[l[j]]=ns[l[j]],ns[l[i]]
i += 1
j -= 1
return "".join(ns)
答案
这写的啥呀
return re.sub('(?i)[aeiou]', lambda m, v=re.findall('(?i)[aeiou]', s): v.pop(), s)
感觉不如我的,思想上差不多
class Solution(object):
def reverseVowels(self, s):
vowels = set(list("aeiouAEIOU"))
s = list(s)
ptr_1, ptr_2 = 0, len(s) - 1
while ptr_1 < ptr_2:
if s[ptr_1] in vowels and s[ptr_2] in vowels:
s[ptr_1], s[ptr_2] = s[ptr_2], s[ptr_1]
ptr_1 += 1
ptr_2 -= 1
if s[ptr_1] not in vowels:
ptr_1 += 1
if s[ptr_2] not in vowels:
ptr_2 -= 1
return ''.join(s)
13. Roman to Integer
题目
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
思路与解答
这题。。。罗马数字我就会从一数到五
规则蛮复杂的
希望正规点,没有第三条
提交过程中发现了第三条,啊啊啊啊啊
d = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}
sums = 0
ls =list(s)
for i in range(len(ls)-1):
if d[ls[i]] < d[ls[i+1]]:
sums -= d[ls[i]]
else:
sums += d[ls[i]]
return sums + d[ls[-1]]
第三条规则只要判断一下左边的是否比右边的小即可
答案
差不多
d = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
res = 0
pre = 0
for ch in s[::-1]:
num = d[ch]
if num >= pre:
res += num
else:
res -= num
pre = num
return res
189. Rotate Array
题目
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
[show hint]
Hint:
Could you do it in-place with O(1) extra space?
Related problem: Reverse Words in a String II
思路与解答
没太懂题的意思
就是把最后k个字符移到前面来?
while k > 0:
nums.insert(0,nums.pop())
k -= 1
5%啊。。。
直接用切片?主要是我忘了怎么用切片修改原list了
答案
哦对,是nums[:],我用nums当然修改不了
k = k % len(nums)
nums[:] = nums[-k:] + nums[:-k]
话说是不是我忘记给k取余所以太慢了?
。。。。并不是
虽然不是python ,但是很有趣。。。n是啥,nums长度?,这个函数又是啥,为什么要两个参数,为什么nums还加上n?和我预期的好像不太一样,算了
第一行反转所有元素,第二个反向k%n个元素需要旋转,第三个反向剩余。如果k大于n,则与k%n相同。你最好以一个例子来想
void rotate(int nums[], int n, int k) {
reverse(nums,nums+n);
reverse(nums,nums+k%n);
reverse(nums+k%n,nums+n);
}