Leetcode 演算法題07

2021-08-10 08:24:42 字數 3476 閱讀 7452

169. majority element

輸入乙個列表,找出其中出現次數超過列表長度一半的數

我的**:

class solution(object):

def majorityelement(self, nums):

""":type nums: list[int]

:rtype: int

"""count = collections.counter(nums)

for i in count:

if count[i] > len(nums)/2:

return i

大神的**:既然佔了**一半,排序後中間的那個數肯定就是所求

class solution(object):

def majorityelement(self, nums):

""":type nums: list[int]

:rtype: int

"""return sorted(nums)[len(nums)/2]

167. two sum ii - input array is sorted

給乙個排好序的列表和目標數,找出列表中相加得到目標數的兩個數的索引

input:

numbers=, target=9

output:

index1=1, index2=2

我的**:第一次超時了,沒有記錄,這次是想了個不超時的思路,寫得多了點

class solution(object):

def twosum(self, numbers, target):

""":type numbers: list[int]

:type target: int

:rtype: list[int]

"""number = collections.counter(numbers)

print(number)

keys = list(number.keys())

keys.sort()

print(keys)

for i in range(len(keys)):

if keys[i] > target:

return false

add = 0

while i+add 1:

ans1 = sum(list(map(lambda x:number[x],keys[:i])))+1

ans2 = ans1+1

return [ans1,ans2]

大神的**:自愧不如

# two-pointer

def twosum1(self, numbers, target):

l, r = 0, len(numbers)-1

while l < r:

s = numbers[l] + numbers[r]

if s == target:

return [l+1, r+1]

elif s < target:

l += 1

else:

r -= 1

# dictionary

def twosum2(self, numbers, target):

dic = {}

for i, num in enumerate(numbers):

if target-num in dic:

return [dic[target-num]+1, i+1]

dic[num] = i

# binary search

def twosum(self, numbers, target):

for i in xrange(len(numbers)):

l, r = i+1, len(numbers)-1

tmp = target - numbers[i]

while l <= r:

mid = l + (r-l)//2

if numbers[mid] == tmp:

return [i+1, mid+1]

elif numbers[mid] < tmp:

l = mid+1

else:

r = mid-1

387. first unique character in a string

輸入乙個字串,找到第乙個在字串中沒有重複出現過的字母的索引

examples:

s = "leetcode"

return 0.

s = "loveleetcode",

return 2.

我的**:

class solution(object):

def firstuniqchar(self, s):

""":type s: str

:rtype: int

"""a=collections.counter(s)

ans =

for i in a:

if a[i] == 1:

if ans == :

return -1

return min([s.find(j) for j in ans])

其他思路:

def firstuniqchar(self, s):

""":type s: str

:rtype: int

"""letters='abcdefghijklmnopqrstuvwxyz'

index=[s.index(l) for l in letters if s.count(l) == 1]

return min(index) if len(index) > 0 else -1

237. delete node in a linked list

刪除節點

我的**:感覺有時候出現的問題好蠢

# definition for singly-linked list.

# class listnode(object):

# def __init__(self, x):

# self.val = x

# self.next = none

class solution(object):

def deletenode(self, node):

""":type node: listnode

:rtype: void do not return anything, modify node in-place instead.

"""node.val ,node.next= node.next.val,node.next.next

領扣刷題 leetcode 07

給出乙個 32 位的有符號整數,你需要將這個整數中每位上的數字進行反轉。示例 2 示例 3 假設我們的環境只能儲存得下 32 位的有符號整數,則其數值範圍為 231,231 1 請根據這個假設,如果反轉後整數溢位那麼就返回 0。includeint reverse int x int main 21...

LeetCode 演算法題

給定乙個整數陣列 nums 和乙個目標值 target,請你在該陣列中找出和為目標值的那 兩個 整數,並返回他們的陣列下標。你可以假設每種輸入只會對應乙個答案。但是,你不能重複利用這個陣列中同樣的元素。示例 給定 nums 2,7,11,15 target 9 因為 nums 0 nums 1 2 ...

Leetcode演算法題

56.合併區間 給出乙個區間的集合,請合併所有重疊的區間。示例 1 輸入 intervals 1,3 2,6 8,10 15,18 輸出 1,6 8,10 15,18 解釋 區間 1,3 和 2,6 重疊,將它們合併為 1,6 示例 2 輸入 intervals 1,4 4,5 輸出 1,5 解釋 ...