Q1 easy题目链接:https://leetcode.com/problems/reverse-string/
leetcode 344
讲解链接:
看到题目的第一思路:
可以用库函数吗
代码随想录之后的想法和总结:
如果题目关键的部分直接用库函数就可以解决,建议不要使用库函数。比如这道题不能直接用reverse函数
如果库函数仅仅是 解题过程中的一小部分,并且你已经很清楚这个库函数的内部实现原理的话,可以考虑使用库函数。比如这道题可以用swap函数,也就是不太关键的部分可以用
这道题目和反转链表的思路接近,唯一区别是字符串(数组)当中内存连续分布,所以不用考虑指针指向这些细节问题,只要从头尾两个位置开始交换指针,然后下一个头尾继续交换,直到没有交换的空间就可以完成反转了
遇到的困难:
暂无
可以记录备用的固定代码方法模版:
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
left,right = 0, len(s)-1 #首先定义左右双指针
while left < right:
s[left],s[right] = s[right],s[left]#头尾交换位置
left += 1 #左右双指针不断收缩直到接下来的每一个头尾都交换位置
right -= 1
时间复杂度以及空间复杂度:
time:
O(n),n是字符串长度,因为只有遍历了字符串一遍这一个时间复杂度
space:
O(1),因为在原地交换,没有创造额外空间
Q2 easy题目链接:https://leetcode.com/problems/reverse-string-ii/
leetoce 541
讲解链接:
看到题目的第一思路:
因为这道题目反转规则更加复杂,如何按照不同情况处理遇到几个字符反转前几个的特定规定呢
题目规则写的其实不是很清楚:可以概括为-每隔2k个反转前k个,尾数不够k个就全部反转,尾数够k个但不够2k也是反转前k个
代码随想录之后的想法和总结:
1 字符串遍历的时候,可以不用常用思路一个一个跳,可以成段成段去跳,跳出惯性思维
2 在这道题目里,先为凑足2k的情况进行反转操作,然后用continue来进入下一次循环(如果凑足的情况),然后再写上不足k的情况,如何处理
遇到的困难:
关于if(i+k <= s.size)这里,为什么?
- 为了确保当i+k在数组范围内,才能进行下一步,i 至i+k这个区间进行反转的操作
还有注意要把string 换成list才能进行修改的操作
以及最后用join()转换回string
可以记录备用的固定代码方法模版:
class Solution:
def reverseStr(self, s: str, k: int) -> str:
def reverse_substring(text):#首先定义一个reverse函数来代替库函数使用
left, right = 0, len(text) - 1
while left < right:
text[left], text[right] = text[right], text[left]
left += 1
right -= 1
return text
res = list(s)#convert String to List:
#使用range(start, end, step)来确定需要调换的初始位置,并且以2k为区间反转前k个字符
for i in range(0, len(s), 2 * k):
res[i: i + k] = reverse_substring(res[i: i + k])
return ''.join(res) #join the List Back to a String:
时间复杂度以及空间复杂度:
time:
- Each iteration takes O(k) time, and there areO(n / k)iterations.
- Therefore, the total time complexity is O(n/k)×O(k)=O(n).
space:
- The primary space usage is due to the list conversion, which is O(n).
- The temporary lists created by slicing are limited by the number of iterations and the maximum slice size kkk, but since the slicing happens in sequence and not all at once, the overall space complexity remains dominated by O(n).
Q3题目链接:替换数字 | 代码随想录
卡玛网
讲解链接:
看到题目的第一思路:
暂无
代码随想录之后的想法和总结:
1首先扩展原数组变成新数组的长度,这样之后有空间去填充字符
2 从后往前填充的思路很棒,避免了从前向后填充元素时,每次添加元素都要将添加元素之后的所有元素向后移动的问题。
并且使用了双指针的方法,用来把旧数组里面的字符赋给新数组,当遇到数字时逐一替换成‘number',
遇到的困难:
暂无
可以记录备用的固定代码方法模版:
def main():
while True:
try:
s = input()
count = 0 # Count the number of digits
for char in s:
if char.isdigit():
count += 1
# Expand the size of the string
s = list(s)
s.extend([''] * (count * 5))
s_old_index = len(s) - 1 - (count * 5)
s_new_index = len(s) - 1
# Replace digits with "number" from the end
while s_old_index >= 0:
if s[s_old_index].isdigit():
s[s_new_index] = 'r'
s[s_new_index - 1] = 'e'
s[s_new_index - 2] = 'b'
s[s_new_index - 3] = 'm'
s[s_new_index - 4] = 'u'
s[s_new_index - 5] = 'n'
s_new_index -= 6
else:
s[s_new_index] = s[s_old_index]
s_new_index -= 1
s_old_index -= 1
print("".join(s))
except EOFError:
break
if __name__ == "__main__":
main()
时间复杂度以及空间复杂度:
time:
1 Counting the Digits:O(n), where n is the length of the string.
2 Expanding the String:appending to a list in Python is O(1)Ofor each append operation, so overall this operation is O(n).
3:Replacing Digits with "number":
Overall, O(n)
space:
O(n) in terms of the input string size and O(n+5c)for the expanded string.
今日收获,学习时长:
1对于线性数据结构,填充或者删除,后序处理会高效的多
2非关键部分可以用库函数,不能直接用库函数代替逻辑,或者是可以自己写一个函数代替库函数
3 对于遍历字符串需要反转或者进行其他操作,可以分段按照区间去遍历或者跳过,不用非得一个一个遍历