Zhuanli&Blog


  • 首页

  • 标签

  • 分类

  • 归档

leetcode 4. Median of Two Sorted Arrays

发表于 2018-03-10 | 分类于 leetcode

There are two sorted arrays nums1 and nums2 of size m and n respectively.

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

Example 1:


nums1 = [1, 3]
nums2 = [2]
The median is 2.0

Example 2:


nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
m,n = len(nums1),len(nums2)
if m > n:
nums1,m,nums2,n = nums2,n,nums1,m

begin ,end = 0,m

while begin <= end:
i = (begin+end)/2
j = (m+n+1)/2 - i
if i > 0 and nums1[i-1] > nums2[j] :
#i is too large
end = i - 1
elif i < m and nums1[i] < nums2[j-1] :
#i is too small
begin = i + 1
else:#i is ok
if i == 0:max_left = nums2[j-1]
elif j == 0:max_left = nums1[i-1]
else:
max_left = max(nums1[i-1],nums2[j-1])

if (m + n)%2 == 1:
return max_left

if i == m:min_right = nums2[j]
elif j == n:min_right = nums1[i]
else:
min_right = min(nums2[j],nums1[i])
return (min_right+max_left)/2.0

leetcode 3. Longest Substring Without Repeating Characters

发表于 2018-03-10 | 分类于 leetcode

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
#dp[i] = dp[i-1]+1 ,if s[i] != s[j] when i-dp[i-1]<= j <i
#dp[i] = i-j ,if s[i] == s[j] when i-dp[i-1]<= j <i
dp = [1]*len(s)
for i in range(1,len(s)):
for j in range(i-dp[i-1],i):
flag = 0
if s[i] == s[j]:
dp[i] = i - j
break
elif j == i-1:
dp[i] = dp[i-1] + 1
max_len = 0
print(dp)
for i in dp:
if max_len < i:
max_len = i

return max_len

leetcode 2. Add Two Numbers

发表于 2018-03-10 | 分类于 leetcode

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

Explanation: 342 + 465 = 807.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class ListNode2(ListNode):
def __init__(self, x):
self.val = x
self.next = None

def append(self, dataOrNode):
item = None
if isinstance(dataOrNode, ListNode):
item = dataOrNode
else:
item = ListNode(dataOrNode)

if not self:
self = item
else:
node = self
while node.next:
node = node.next
node.next = item

class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
list1 = []
list2 = []
while l1:
list1.append(l1.val)
l1 = l1.next

while l2:
list2.append(l2.val)
l2 = l2.next
int1 = 0
for i in range(len(list1)):
int1 += (10**i)*list1[i]

int2 = 0
for i in range(len(list2)):
int2 += (10**i)*list2[i]

int_target = int1 + int2
p = ListNode2(int_target % 10)
int_target = int_target // 10
while int_target:
p.append(int_target % 10)
int_target = int_target // 10
return p

leetcode 61. Rotate List

发表于 2018-03-10 | 分类于 leetcode

Given a list, rotate the list to the right by k places, where k is non-negative.

Example:

Given 1->2->3->4->5->NULL and k = 2,

return 4->5->1->2->3->NULL.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class ListNode2(ListNode):
def __init__(self,x):
self.val = x
self.next = None

def append(self, dataOrNode):
item = None
if isinstance(dataOrNode, ListNode):
item = dataOrNode
else:
item = ListNode(dataOrNode)

if not self:
self = item

else:
node = self
while node.next:
node = node.next
node.next = item

class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
res = []
root = head
if head == None:return None
while root:
res.append(root.val)
root = root.next
k = k%len(res)
if k == 0:return head
res = res[len(res)-1-k::-1] + res[:len(res)-1-k:-1]
res = res[::-1]
p = ListNode2(res[0])
for i in res[1:]:
p.append(i)
return p
1…1920

zhuanli

194 日志
9 分类
41 标签
GitHub
© 2018 — 2019 zhuanli
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.4