注意,答案只是代表是他人寫的代碼,正確,但不一定能通過測試(比如超時),列舉出來只是它們擁有着獨到之處,雖然大部分確實比我的好
(發現最近光寫解答沒寫思路了)
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);
}