-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode.py
166 lines (119 loc) · 3.28 KB
/
leetcode.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
1. Two Sum
Easy
9481
294
Favorite
Share
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
d = {}
for i, n in enumerate(nums):
if n in d:
return [d[n], i]
else:
d[target - n] = i
2. Add Two Numbers
Medium
4316
1044
Favorite
Share
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.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
dummy = l3 = ListNode(0)
carry = 0
while l1 is not None or l2 is not None or carry:
add = carry
if l1 is not None:
add += l1.val
l1 = l1.next
if l2 is not None:
add += l2.val
l2 = l2.next
l3.next = ListNode(add%10)
carry = add//10
l3 = l3.next
return dummy.next
3. Longest Substring Without Repeating Characters
Medium
4560
227
Favorite
Share
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: 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.
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
i = 0
j = 0
cur_set = set()
max_len = 0
while i < len(s) and j < len(s):
if s[j] not in cur_set:
cur_set.add(s[j])
j += 1
max_len = max(max_len, j-i)
else:
cur_set.remove(s[i])
i += 1
return max_len
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
max_len = 0
d = {}
start = 0
for i, char in enumerate(s):
if char not in d:
d[char] = i
else:
start = max(start, d[char] +1)
d[char] = i
max_len = max(max_len, i-start+1)
return max_len