观看覃超第二次分享之记录与总结

CMU金刚课程

15112 CMU 15-112: Fundamentals of Programming and Computer Science
15112

15213 – Introduction to Computing system.

15213

15410 – Operating System Design and Implementation
15410

15440 -Distributed Systems
15440

Machine Learning
Machine Learning

Deepleaning

切leetcode题3道

412

Fizz Buzz

描述：

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

n = 15,

Return: [
“1”,
“2”,
“Fizz”,
“4”,
“Buzz”,
“Fizz”,
“7”,
“8”,
“Fizz”,
“Buzz”,
“11”,
“Fizz”,
“13”,
“14”,

我的解决思路

class Solution(object):
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
l = []
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
l.append("FizzBuzz")
elif i % 3 == 0:
l.append("Fizz")
elif i % 5 == 0:
l.append("Buzz")
else:
l.append(str(i))

return l

if __name__ == '__main__':
sol = Solution()
n = 15
print(sol.fizzBuzz(n))


412

88

Merge Sorted Array

描述

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

注意

You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.

我的解决思路

class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
i = m - 1
j = n - 1
k = m + n - 1
while i >= 0 and j >= 0:
if nums1[i] > nums2[j]:
nums1[k] = nums1[i]
k -= 1
i -= 1
else:
nums1[k] = nums2[j]
k -= 1
j -= 1
while j >= 0:
nums1[k] = nums2[j]
k -= 1
j -= 1

if __name__ == '__main__':
sol = Solution()
nums1 = [0, 1, 4, 5, 6, 0, 0, 0, 0]
nums2 = [2, 3, 6, 7]
sol.merge(nums1, 5, nums2, 4)
print(nums1)



88

41

First Missing Positive

描述

Given an unsorted integer array, find the first missing positive integer.

举例

Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
Your algorithm should run in O(n) time and uses constant space.

我的解决思路

class Solution(object):
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
max_num = len(nums) + 1
nums_set = set(nums)
for i in range(1, max_num):
if i not in nums_set:
return i
return max_num

if __name__ == '__main__':
sol = Solution()
x = [10, 11, 12, 0, 0, 0]
y = [1, 2, 0]
print(sol.firstMissingPositive(y))


412

代码review

1.谷歌C++代码风格指南
2.谷歌Python代码风格指南
3.PEP 8 — Style Guide for Python Code
4.在线交流code
collabedit