leetcode中国-LeetCode:LeetCode题解,Python

时间:2021-06-29 20:43:35
【文件属性】:
文件名称:leetcode中国-LeetCode:LeetCode题解,Python
文件大小:95KB
文件格式:ZIP
更新时间:2021-06-29 20:43:35
系统开源 leetcode中国 LeetCode 题解 数组与矩阵 把数组中的 0 移到末尾 解题思路: 方法一: 首先想到的是可以利用冒泡的思想,将等于 0 的元素依次挪到最后。这里有个优化,每次都判断下是否有元素交换,如果没有交换则可以直接退出循环。冒泡的时间复杂度为 O(n2)。 class Solution(object): def moveZeroes(self, nums): n = len(nums) for i in range(n-1): swap = False for j in range(n-i-1): if nums[j] == 0: nums[j], nums[j+1] = nums[j+1], nums[j] swap = True if not swap: break return nums 方法二: 利用指针,先把不等于 0 的元素往前挪,剩余的数字全部赋值 0。时间复杂度接近 O(n)。 class Solution(object): def moveZeroes(self, nums): i = 0 for num in nums: if num != 0:
【文件预览】:
LeetCode-main
----分治()
--------generate_trees.py(1KB)
--------diff_way_compute.py(1KB)
----栈()
--------daily_temperatures.py(940B)
--------min_stack.py(1KB)
--------next_greater_elements.py(1KB)
--------match_brackets.py(858B)
----README.md(63KB)
----排序()
--------topk_frequent.py(2KB)
--------sort_colors.py(1KB)
--------find_top_k.py(1KB)
--------frequency_sort.py(941B)
----二分法()
--------my_sqrt.py(1KB)
--------kth_smallest.py(874B)
--------single_non_duplicate.py(920B)
--------find_min.py(1KB)
--------next_greatest_letter.py(1KB)
--------binary_search.py(508B)
----双指针()
--------check_has_cycle.py(1KB)
--------square_sum.py(1KB)
--------three_sum.py(1KB)
--------valid_palindrome.py(1KB)
--------reverse_vowels.py(1KB)
--------merge_list.py(2KB)
--------find_longest_word.py(1KB)
--------two_sum.py(1KB)
--------find_duplicate.py(1KB)
----深度优先搜索()
--------islands_num.py(2KB)
--------firend_circle_num.py(1KB)
--------max_area.py(2KB)
--------surrounded_regions.py(2KB)
--------search_matrix.py(2KB)
--------pacific_atlantic.py(3KB)
----滑动窗口()
--------slide_window.py(902B)
--------count_binary_substrings.py(1KB)
----广度优先搜索()
--------num_Squares.py(1KB)
--------ladder_length_2.py(37KB)
--------shortest_path.py(1KB)
--------search_matrix.py(1KB)
--------ladder_length.py(38KB)
----回溯算法()
--------permute_unique.py(1KB)
--------subsets.py(926B)
--------palindrome_split.py(1KB)
--------combine.py(836B)
--------restore_ipaddress.py(2KB)
--------word_search.py(2KB)
--------permute.py(756B)
--------subsets_with_duplicate.py(1KB)
--------combine_sum.py(1KB)
--------combine_sum2.py(2KB)
--------combine_sum3.py(970B)
--------letter_combinations.py(1KB)
--------binary_tree_path.py(1KB)
----链表()
--------split_list.py(2KB)
--------reorder_oddeven_list.py(1KB)
--------getIntersectionNode.py(669B)
--------two_sum_list.py(2KB)
--------swap_node.py(951B)
--------mergeTwoLists.py(825B)
--------delete_duplicates.py(531B)
--------reverseList.py(455B)
--------removeNthFromEnd.py(792B)
--------palindrome_list.py(885B)

网友评论