✅ 108. 將有序數組轉換為二叉搜索樹
https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/
描述
中序遍歷的逆過程
解答
py
錯在: Python沒有三目運算符(?😃,類函數不可調用
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
return nums == null ? null : buildTree(nums, 0, nums.len)#tt Python沒有三目運算符(?:)
#tt 而且py 自己定義的函數,也不是 像java 那里可以調用同 類 中的函數
def buildTree(nums: List[int], l: int, r: int) -> TreeNode:
if(l > r):
return null;
m = l + (r - l) / 2
root = nums[m]
root.left = buildTree(nums, l, m - 1)
root.right = buildTree(nums, m + 1, r)
return root
fix:
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
if not nums: # 注意要使用if not ,而不是 if nums == None
return None
else:
mid = len(nums) // 2;
tn = TreeNode(nums[mid])
nums1 = nums[0:mid]
nums2 = nums[mid+1: len(nums)]
tn.left = self.sortedArrayToBST(nums1)
tn.right = self.sortedArrayToBST(nums2)
return tn
'''執行用時 :
100 ms
, 在所有 Python3 提交中擊敗了
16.13%
的用戶
內存消耗 :
15.4 MB
, 在所有 Python3 提交中擊敗了
49.31%
的用戶'''
【tdo rev 0208】py知識:if not x:
和if x is not None:
和if not x is None:
使用
https://blog.csdn.net/sasoritattoo/article/details/12451359
代碼中經常會有變量是否為None的判斷,有三種主要的寫法:
第一種是if x is None
;
第二種是 if not x:
;
第三種是if not x is None
(這句這樣理解更清晰if not (x is None)
) 。
如果你覺得這樣寫沒啥區別,那么你可就要小心了,這里面有一個坑。先來看一下代碼:
>>> x = 1
>>> not x
False
>>> x = [1]
>>> not x
False
>>> x = 0
>>> not x
True
>>> x = [0] # You don't want to fall in this one.
>>> not x
False
在python中 None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元組()都相當於False ,即:
not None == not False == not '' == not 0 == not [] == not {} == not ()
因此在使用列表的時候,如果你想區分x[]和xNone兩種情況的話, 此時if not x:
將會出現問題:
>>> x = []
>>> y = None
>>>
>>> x is None
False
>>> y is None
True
>>>
>>>
>>> not x
True
>>> not y
True
>>>
>>>
>>> not x is None
>>> True
>>> not y is None
False
>>>
也許你是想判斷x是否為None,但是卻把x==[]
的情況也判斷進來了,此種情況下將無法區分。
對於習慣於使用if not x這種寫法的pythoner,必須清楚x等於None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元組()時對你的判斷沒有影響才行。
而對於if x is not None
和if not x is None
寫法,很明顯前者更清晰,而后者有可能使讀者誤解為if (not x) is None
,因此推薦前者,同時這也是谷歌推薦的風格
結論:
if x is not None
是最好的寫法,清晰,不會出現錯誤,以后堅持使用這種寫法。
使用if not x這種寫法的前提是:必須清楚x等於None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元組()時對你的判斷沒有影響才行。
✅ 344. 反轉字符串
https://leetcode-cn.com/problems/reverse-string
描述
編寫一個函數,其作用是將輸入的字符串反轉過來。輸入字符串以字符數組 char[] 的形式給出。
不要給另外的數組分配額外的空間,你必須原地修改輸入數組、使用 O(1) 的額外空間解決這一問題。
你可以假設數組中的所有字符都是 ASCII 碼表中的可打印字符。
示例 1:
輸入:["h","e","l","l","o"]
輸出:["o","l","l","e","h"]
示例 2:
輸入:["H","a","n","n","a","h"]
輸出:["h","a","n","n","a","H"]
解答
以下的 range()
這些函數 真的就是肌肉寫的,無腦記憶寫的,猜的。
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
for i in range(len(s) // 2):
c = s[i]
s[i] = s[len(s) - 1 - i]
s[len(s) - 1 - i] = c
其實,幸虧 range
函數是 右邊 開的:
>>>range(10) # 從 0 開始到 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
所以,當len(s) == 4
, we have len(s) // 2 == 2
, but using range(2)
will result: [0, 1]
, notice there has no 2
, so it's safe, cause we dont swap s[2]
and s[1]
(after we swapped s[1]
with s[2]
✅ 944. 刪列造序
https://leetcode-cn.com/problems/delete-columns-to-make-sorted
描述
看不懂題目的可以參考一下這句話:這道題本質就是判斷一個矩陣里,有多少列是降序排序的
解答
我的理解是,找到所有不 合適
的列,每找到 一個,就 cnt++
所以 重點是,如何在 矩陣中 的列 進行遍歷
when you use py: using zip
class Solution:
def minDeletionSize(self, A: List[str]) -> int:
ret = 0
for i in zip(*A):#success, use: zip(A) will fail, so what is * means for a list???
if list(i) != sorted(i):
ret += 1
return ret
'''執行用時 :
92 ms
, 在所有 Python3 提交中擊敗了
94.28%
的用戶
內存消耗 :
13.7 MB
, 在所有 Python3 提交中擊敗了
50.69%
的用戶'''
python zip
描述:
zip() 函數用於將可迭代的對象作為參數,將對象中對應的元素打包成一個個元組,然后返回由這些元組組成的列表。
如果各個迭代器的元素個數不一致,則返回列表長度與最短的對象相同,利用 *
號操作符,可以將元組解壓為列表。
zip 方法在 Python 2 和 Python 3 中的不同:在 Python 3.x 中為了減少內存,zip()
返回的是一個對象。如需展示列表,需手動 list()
轉換。
eg in py2:
>>>a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped = zip(a,b) # 打包為元組的列表
[(1, 4), (2, 5), (3, 6)]
>>> zip(a,c) # 元素個數與最短的列表一致
[(1, 4), (2, 5), (3, 6)]
>>> zip(*zipped) # 與 zip 相反,*zipped 可理解為解壓,返回二維矩陣式
[(1, 2, 3), (4, 5, 6)]
eg in py3
>>>a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped = zip(a,b) # 返回一個對象
>>> zipped
<zip object at 0x103abc288>
>>> list(zipped) # list() 轉換為列表
[(1, 4), (2, 5), (3, 6)]
>>> list(zip(a,c)) # 元素個數與最短的列表一致
[(1, 4), (2, 5), (3, 6)]
>>> a1, a2 = zip(*zip(a,b)) # 與 zip 相反,zip(*) 可理解為解壓,返回二維矩陣式
>>> list(a1)
[1, 2, 3]
>>> list(a2)
[4, 5, 6]
>>>
when u use c: dive deep into 2d array
int minDeletionSize(char** A, int ASize) {
int count = 0;
for(int i = 0;i < strlen(A[0]);i++){
for(int j = 0;j < ASize - 1;j++){
if(A[j][i] > A[j+1][i]){
count++;
break;
}
}
}
return count;
}
'''---
my '''
int minDeletionSize(char ** A, int ASize){
int cnt = 0;
int i = 0;
int j = 0;
int colNum = 0;
colNum = strlen(A[0]);
printf("colNum is %d", colNum);
for(; i < colNum; i++){
for(j = 0; j < ASize - 1; j++){
if(A[j][i] > A[j + 1][i]){
cnt++;
break;// only break inner for-loop
}
}
}
return cnt;
}
/*執行用時 :
32 ms
, 在所有 C 提交中擊敗了
26.53%
的用戶
內存消耗 :
9.3 MB
, 在所有 C 提交中擊敗了
57.43%
的用戶*/
✅ 181. 超過經理收入的員工
https://leetcode-cn.com/problems/employees-earning-more-than-their-managers
描述
Employee 表包含所有員工,他們的經理也屬於員工。每個員工都有一個 Id,此外還有一列對應員工的經理的 Id。
+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+
給定 Employee 表,編寫一個 SQL 查詢,該查詢可以獲取收入超過他們經理的員工的姓名。在上面的表格中,Joe 是唯一一個收入超過他的經理的員工。
+----------+
| Employee |
+----------+
| Joe |
+----------+
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/employees-earning-more-than-their-managers
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
解答
select e1.Name as Employee from Employee as e1 ,Employee as e2 where e1.ManagerId=e2.Id AND e1.Salary>e2.Salary
”和自己的xxx比”這種問題基本都是自連接問題。
SELECT E1.Name AS Employee FROM Employee as E1,Employee as E2
WHERE E1.ManagerId=E2.Id AND E1.Salary>E2.Salary;
疑問是: E1,Employee as E2
這里, select xxx from Table, WTF clause here
solve with 最后的官方解釋 tt6
實際上是:
select E1.Name as Employee(tt: the final result employee's name)
from (tt7)
Employee as E1,
Employee as E2
where
E1.ManagerId = E2.Id(tt: 其實在這里,上面的tt7 已經搞出來了一個臨時表
,那么這個臨時表里就有E1,E2,我們在where and
語句里直接用 E1, E2 對其進行篩選即可,留下的E1
我們就打印出來了E1.name, 而E2 沒干啥而已。)
and
E1.Salary > E2.Salary;
sql 的 as
as 后面跟着的 是一個別名
正常來說,想查詢該表,那么sql語句如下
select * from user
給別人看,自然是需要很多的時間來理解和熟悉,那么as的作用就提現出來了
select
username as 賬號 ,
password as 密碼,
mingzi as 名字,
zhengjianhao as 證件號,
dianhua as 電話,
zhuceriqi as 注冊日期,
zhuangtai as 狀態,
quanxian as 權限,
shengyutianshu as 剩余天數
from user
as 后面跟着的 是一個別名
【todo 理解 sql as 0208】
# Write your MySQL query statement below
SELECT
Name Employee # wtf Employee 可是 表名啊!!!
FROM
Employee AS a
WHERE
Salary > (SELECT
Salary
FROM
Employee
WHERE
Id = a.Managerid)
我認為, 這個官方的答案 令人滿意 清晰了:
tt6
SELECT
*
FROM
Employee AS a,
Employee AS b
WHERE
a.ManagerId = b.Id
AND a.Salary > b.Salary
;
---imp:(cause we only need `name`)
SELECT
a.Name AS 'Employee'
FROM
Employee AS a,
Employee AS b
WHERE
a.ManagerId = b.Id
AND a.Salary > b.Salary
;