class Solution(object):
def compareVersion(self, version1, version2):
"""
:type version1: str
:type version2: str
:rtype: int
"""
v1Arr = version1.split(".")
v2Arr = version2.split(".")
len1 = len(v1Arr)
len2 = len(v2Arr)
lenMax = max(len1, len2)
for x in range(lenMax):
v1Token = 0
if x < len1:
v1Token = int(v1Arr[x])
v2Token = 0
if x ...- read more
164. Maximum Gap
read more# -*- coding:utf8 -*- # 基数排序 class Solution(object): def maximumGap(self, nums): """ :type nums: List[int] :rtype: int """ n = len(nums) if n < 2: return 0 count = 0 maxnum = max(nums) while maxnum > 0: count += 1 maxnum /= 10 for k in range(count): # 最大数有多少位就有多少轮 s = [[] for _ in range(10)] for i ...
163. Missing Ranges
read more# Given a sorted integer array where the range of elements are [lower, upper] inclusive, # return its missing ranges. # # For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, # return ["2", "4->49", "51->74", "76->99"]. class Solution(object): def findMissingRanges(self, nums, lower, upper): """ :type nums ...
162. Find Peak Element
read moreclass Solution(object): def findPeakElement(self, nums): """ :type nums: List[int] :rtype: int """ size = len(nums) return self.search(nums, 0, size - 1) def search(self, nums, start, end): if start == end: return start if start + 1 == end: return nums.index(max(nums[start], nums[end])) mid = (start + end) / 2 ...
161. One Edit Distance
read more# Given two strings S and T, determine if they are both one edit distance apart. class Solution(object): def isOneEditDistance(self, s, t): """ :type s: str :type t: str :rtype: bool """ l1 = len(s) l2 = len(t) if not s and not t: return 0 if not s: return l2 ...
160. Intersection of Two Linked Lists
read more# Definition for singly-linked list. class ListNode(object): def __init__(self, x): self.val = x self.next = None class Solution(object): def getIntersectionNode(self, headA, headB): """ :type head1, head1: ListNode :rtype: ListNode """ tmpa = headA tmpb = headB counta = 0 taila = None while tmpa: taila = tmpa counta += 1 tmpa = tmpa.next countb = 0 ...
159. Longest Substring with At Most Two Distinct Characters
read more# Given a string, find the length of the longest substring T # that contains at most 2 distinct characters. # # For example, Given s = "eceba", # # T is "ece" which its length is 3. # class Solution: # @param s, a string # @return an integer def lengthOfLongestSubstringTwoDistinct(self, s): if len(s) == 0: return 0 ...
158. Read N Characters Given Read4 II
read more# The API: int read4(char *buf) reads 4 characters at a time from a file. # # The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. # # By using the read4 API, implement the function int read ...
157. Read N Characters Given Read4
read more# The API: int read4(char *buf) reads 4 characters at a time from a file. # # The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. # # By using the read4 API, implement the function int read ...
156. Binary Tree Upside Down
read more# Given a binary tree where all the right nodes are either leaf nodes with a sibling # (a left node that shares the same parent node) or empty, flip it upside down and # turn it into a tree where the original right nodes turned into left leaf nodes. # Return the new ...