日誌3月18日

2021-10-22 14:10:35 字數 2876 閱讀 9128

題目:

給你單鏈表的頭指標 head 和兩個整數 left 和 right ,其中 left <= right 。請你反轉從位置 left 到位置 right 的鍊錶節點,返回 反轉後的鍊錶

class

solution

(object):

defreversebetween

(self, head, left, right)

: count =

1 dummy = listnode(0)

dummy.

next

= head

pre = dummy

while pre.

next

and count < left:

pre = pre.

next

count +=

1 cur = pre.

next

tail = cur

while cur and count <= right:

nxt = cur.

next

cur.

next

= pre.

next

pre.

next

= cur

tail.

next

= nxt

cur = nxt

count +=

1return dummy.

next

class

solution

:def

reversebetween

(self, head: listnode, left:

int, right:

int)

-> listnode:

dummy = listnode(0)

res = dummy

dummy.

next

= head

p = head

i =1while i < left:

dummy = dummy.

next

p = p.

next

i +=

1while i < right:

tmp = p.

next

p.next

= tmp.

next

tmp.

next

= dummy.

next

dummy.

next

= tmp

i +=

1return res.

next

給你兩個整數陣列 nums 和 index。你需要按照以下規則建立目標陣列:

class

solution

(object):

defcreatetargetarray

(self, nums, index)

: target_array=

for i in

range

(len

(nums)):

target_array.insert(index[i]

,nums[i]

)return target_array

class

solution

(object):

defcreatetargetarray

(self, nums, index):if

not nums:

return

length =

len(nums)

for i in

range

(length)

:for j in

range

(i):

if index[j]

>= index[i]

: index[j]+=1

result =[0

]* length

for i in

range

(length)

: result[index[i]

]= nums[i]

return result

給定乙個大小為 n 的陣列,找到其中的多數元素。多數元素是指在陣列**現次數 大於 ⌊ n/2 ⌋ 的元素。

在這裡插入**片
class

solution

:def

majorityelement

(self, nums: list[

int])-

>

int:

counts = collections.counter(nums)

return

max(counts.keys(

), key=counts.get)

class

solution

:def

createtargetarray

(self, nums: list[

int]

, index: list[

int])-

> list[

int]

: target =

for n, i in

zip(nums, index)

: target = target[

:i]+

[n]+ target[i:

]return target

日誌11月18日

今天學習資料庫查詢操作 import mysqldb 開啟資料庫連線 db mysqldb.connect localhost testuser test123 testdb charset utf8 使用cursor 方法獲取操作游標 cursor db.cursor sql 查詢語句 sql s...

3月18日每週總結

目前遇到的問題模型 貪心演算法需要對資料先按一定規則排序 從問題中抽象出貪心標準的數學模型是解題關鍵 區間問題 一連串起始位置,終點位置,求最多進行幾個活動 安放雷達 搬運桌子 疊加區間 一般都要設結構體,兩個屬性值,然後將問題抽象轉化。截止時間問題 設乙個date陣列,寫乙個finddate函式,...

日誌3月5日

題目 請你僅使用兩個棧實現先入先出佇列。佇列應當支援一般佇列的支援的所有操作 push pop peek empty class myqueue object def init self self.stack1 self.stack2 defpush self,x def pop self if n...