第八周python作業 LeetCode 訓練題

2021-08-19 13:39:36 字數 1978 閱讀 7195

1

、兩數之和

給定乙個整數陣列和乙個目標值,找出陣列中和為目標值的兩個數。

你可以假設每個輸入只對應一種答案,且同樣的元素不能被重複利用。

示例:

給定 nums = [2, 7, 11, 15], target = 9

因為 nums[0] + nums[1] = 2 + 7 = 9

所以返回 [0, 1]

**:

class solution:

def twosum(self, nums, target):

""":type nums: list[int]

:type target: int

:rtype: list[int]

"""self.nums = nums

self.target = target

l = len(self.nums)

for i in range(0,l):

a = target-self.nums[i]

if a in self.nums:

b = self.nums.index(a)

if i!=b:

return i,b

else:

continue

2、

兩個排序陣列的中位數

給定兩個大小為m 和

n 的有序陣列

nums1

nums2

請找出這兩個有序陣列的中位數。要求演算法的時間複雜度為

o(log (m+n))

。示例 1:

nums1 = [1, 3]

nums2 = [2]

中位數是 2.0

示例 2:

nums1 = [1, 2]

nums2 = [3, 4]

中位數是 (2 + 3)/2 = 2.5

**:

class solution:

def findmediansortedarrays(self, nums1, nums2):

""":type nums1: list[int]

:type nums2: list[int]

:rtype: float

"""l = len(nums1)+len(nums2)

num =

while nums1 and nums2:

if nums1[0]

3. 

無重複字元的最長子串

給定乙個字串,找出不含有重複字元的最長子串的長度。

示例:給定"abcabcbb",沒有重複字元的最長子串是"abc",那麼長度就是3。

給定"bbbbb",最長的子串就是"b",長度是1。

給定"pwwkew",最長子串是"wke",長度是3。請注意答案必須是乙個子串,"pwke"是 子串行  而不是子串。

**:class solution:

def lengthoflongestsubstring(self, s):

max_num = 0

ll=len(s)

for i in range(0,ll):

l=0s1=""

for j in range(i,len(s)):

if s[j] in s1:

break

else:

s1+=s[j]

l+=1

if l>max_num:

max_num = l

return max_num

第八周作業

1 理解窗體的檔案含義及組織結構 如 form1.cs form1.designer.cs form1.resx 控制項的屬性 方法和事件。2 完全用 的方式在form1.cs檔案中建立乙個文字標籤物件label1,用 設定label1的parent location name text autos...

第八周作業

1 顯示統計占用系統記憶體最多的程序,並排序 2 編寫指令碼,使用 for 和 while 分別實現 192.168.0.0 24 網段內,位址是否能夠 ping 通,若 ping 通則輸出 success 若 ping 不通則輸出 fail 3 每週的工作日 1 30,將 etc 備份至 back...

第八周作業

本週是團隊專案的最後一周,我們的團隊專案也完成了大部分的工作。下面是我們近兩周的工作內容,以及我在這個團隊專案中的總結與心得體會。我們小組所進行的專案是仿照手遊 球球大作戰 製作3d的pc版遊戲。到目前為止,我們已經完成了大部分文件與編碼工作,還差測試文件的成型 其它文件的細節修改與一些bug的修補...