Leetcode刷题——小技巧总结 Leetcode刷题——小技巧总结列表如何整体进行加减运算?方法一:使用列表推导式12345original_list = [10, 20, 30, 40, 50]value_to_subtract = 5new_list = [x - value_to_subtract for x in original_list]print(new_list) 方法二:使用map函数12345original_list = [10, 20, 30, 40, 50]value_to_subtract = 5new_list = list(map(lambda x: x - value_to_subtract, original_list))print(new_list) 往复运动=边界条件+step变量12345678910111213141516def convert(self, s: str, numRows: int) -> str: if numRows == 1 or numRows >= len(s): return s ans = [''] * numRows index, step = 0, 1 for char in s: ans[index] += char if index == 0: step = 1 elif index == numRows - 1: step = -1 index += step return ''.join(ans) zip函数的使用12345list1 = [1, 2, 3, 4]list2 = ['a', 'b', 'c']zipped = zip(list1, list2)print(list(zipped)) # 输出: [(1, 'a'), (2, 'b'), (3, 'c')] 列表逆序123456789101112# 示例列表words = ["apple", "banana", "cherry"]# 方法一:使用 reversed 函数reversed_words = reversed(words)result = ' '.join(reversed_words)print(result) # 输出: cherry banana apple# 方法二:使用切片操作reversed_words = words[::-1]result = ' '.join(reversed_words)print(result) # 输出: cherry banana apple #python Leetcode刷题——小技巧总结 https://maplelea1f.github.io/2024/09/02/Leetcode刷题——小知识点总结/ 作者 Maple 发布于 2024年9月2日 许可协议 VMware配置桥接模式联网 上一篇 Linux常用操作 下一篇