3. No repeating characters longest substring
Given a string, you find out which does not contain a sub-length of the longest string of repeated characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: Because the longest substring of characters without repetition is "abc", so its length is 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: Because the longest substring is repeated characters without "b", so that its length is 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: Because the longest sub-string is repeated characters without "wke", so its length is 3.
Please note that your answer must be a substring of length, "pwke" is a sub-sequence, not a substring.
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/longest-substring-without-repeating-characters
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
class Solution:
def lengthOfLongestSubstring(self, s: str):
st = {}
i, ans = 0, 0
for j in range(len(s)):
if s[j] in st:
i = max(st[s[j],i])
ans = max(ans, j-i+1)
st[s[j]] = j + 1
return ans
141. The circular linked list
Given a list, the list is determined whether a ring.
To show the list given ring, we integer pos connected to the end of the list to represent the position of the list (index starts from 0). If pos is -1, the ring is not on this list.
Example 1:
Input: head = [3,2,0, -4] , pos = 1
Output: true
explanation: the list has a ring tail portion connected to a second node.
Example 2:
Input: head = [1,2], pos = 0
Output: true
explanation: the list has a ring, which is connected to the tail of the first node.
Example 3:
Input: head = [1], pos = -1
Output: false
interpretation: the list does not ring.
Advanced:
You can use O (1) (ie, constant) memory to solve this problem?
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/linked-list-cycle
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
class ListNode(object):
def __init__(self):
self.val = x
self.next = None
class Solution:
def hasCycle(self, head):
fast, slow = head, head
while fast and fast.next:
fast, slow = fast.next.next, slow.next
if fast == slow:
return True
return False
171. Excel table column number
Given a column name of the Excel spreadsheet returns the corresponding column number.
E.g,
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
示例 1:
Input: "A"
Output: 1
Example 2:
Input: "AB"
Output: 28
Example 3:
Enter: "ZY"
output: 701
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/excel-sheet-column-number
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
result = 0
for i in range(len(s)):
result *=26
result += ord(s[i]) - ord('A') + 1
return result
203. remove list elements
Delete the list is equal to a given value val all nodes.
Example:
Input: 1-> 2-> 6-> 3-> 4-> 5->. 6, Val =. 6 Output: 1-> 2-> 3-> 4-> 5
# 203 移出链表元素
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
dummy = ListNode(0)
dummy.next = head
prev, curr = dummy, dummy.next
while curr:
if curr.val == val:
prev.next = curr.next
else:
prev = curr
curr = curr.next
return dummy.next