标签:comm javadoc http 使用 工作 har 读书 strong self
Dec 30, 2019 ~ Jan 5, 2020
Problem 88 Merge Sorted Array (合并两个有序数组) 题目链接
题目描述:给定两个有序数组 nums1,nums2,其长度分别为m,n。假设 nums1 有足够的空间(m+n),将 nums2 中的数组添加到已有的 nums1 数组中。不返回任何值。举例如下:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
合并后的 nums1 =?[1,2,2,3,5,6]
思路:在我做题时发现,实际输入的 nums1 后面会多 n 个0,因此要先去掉这些多余的0。而后便是从头开始遍历 nums1 和 nums2,若 nums2 某位置的元素小于等于 nums1 某位置的元素,那么则把 nums2 的相应元素插入到 nums1 的位置前面,若 nums2 某位置的元素大于 nums1 某位置的元素,那么 nums1 向前前进一个元素。最后,判断若是 nums1 先到达了末尾,那么把剩余的 nums2 元素都插入到 nums1 中。
通过的代码如下
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
if n == 0:
pass
elif m == 0:
# 去除末尾多余的0
for tmp in range(n):
nums1.pop()
for i in range(n):
nums1.append(nums2[i])
else:
for tmp in range(n):
nums1.pop()
i = j = 0
while i != m and j != n:
if nums1[i] >= nums2[j]:
nums1.insert(i, nums2[j])
i += 1
j += 1
# 因为插入了新元素,nums1长度要增加一,即m=m+1
m += 1
else:
i += 1
if i == m:
for k in range(j, n):
nums1.append(nums2[k])
本周继续 Review 每个程序员需要知道的97件事(英文名:97 Things Every Programmer Should Know)。原文链接。下面是本周的5个小内容:
Python中字符串方法str.lower(),str.upper(),str.title(),str.capitalize()的功能如下:
s0 = 'an apple A DAY keeps the doctor away.'
s1 = s0.lower()
s2 = s0.upper()
s3 = s0.title()
s4 = s0.capitalize()
print('s0 = ',s0)
print('s1 = ',s1)
print('s2 = ',s2)
print('s3 = ',s3)
print('s4 = ',s4)
'''
输出结果如下:
s0 = an apple A DAY keeps the doctor away.
s1 = an apple a day keeps the doctor away.
s2 = AN APPLE A DAY KEEPS THE DOCTOR AWAY.
s3 = An Apple A Day Keeps The Doctor Away.
s4 = An apple a day keeps the doctor away.
'''
在 Review 部分中,尽早部署(Deploy Early and Often)的意义和做一个 demo 一样;便利和灵活性比较难以把握,盲目的追求开发时的便利性,很难适应变化性。但是,一味地追求灵活性会大大增加开发的难度;注释的内容要聚焦于WHAT your code does,而不是HOW,如有必要可以解释下WHY。
标签:comm javadoc http 使用 工作 har 读书 strong self
原文地址:https://www.cnblogs.com/mengxinayan/p/12288025.html