Last active
December 19, 2019 06:50
-
-
Save re4388/d5631af8f0bffd20bce712acfeb7d89d to your computer and use it in GitHub Desktop.
ms100-not-sure-which-one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' 给定一个字符串, | |
要求把字符串前面的若干个字符移动到字符串的尾部,如把字符串“abcdef”前面的2个字符'a'和'b'移动到字符串的尾部, | |
使得原字符串变成字符串“cdefab”。 | |
请写一个函数完成此功能, | |
要求对长度为n的字符串操作的时间复杂度为 O(n),空间复杂度为 O(1)。 ''' | |
input = 'abcdef' | |
r = input[2:] + input[0] + input[1] | |
print(r) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' 有个长度为2n的数组{a1,a2,a3,...,an,b1,b2,b3,...,bn},希望排序后{a1,b1,a2,b2,....,an,bn},请考虑有无时间复杂度o(n),空间复杂度0(1)的解法。 ''' | |
a1 = ['a1', 'a2', 'a3', 'a4', 'b1', 'b2', 'b3', 'b4'] | |
# seperate 2 list via prefix | |
list1 = [ele for ele in a1 if ele.startswith('a')] | |
print(list1) | |
list2 = [ele for ele in a1 if ele.startswith('b')] | |
print(list2) | |
# loop them one by one to add to third list | |
x = [*sum(zip(list1, list2), ())] | |
print(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' 单词翻转题:输入“I am a student.”,则输出“student. a am I”。 ''' | |
input = 'I am a student' | |
# seperate sentence into each word | |
list_ = input.split(" ") | |
list_.reverse() | |
print(list_) | |
result = ' '.join(list_) | |
print(result) | |
# put them into a list | |
# read that list in reverse oder and concate them up |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment