This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def myAtoi(self, str): | |
""" | |
:type str: str | |
:rtype: int | |
""" | |
def in_range(num): | |
if num > 2 ** 31 - 1: return 2 ** 31 - 1 | |
if num < -2**31: return -2**31 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def isNumber(self, s): | |
""" | |
:type s: str | |
:rtype: bool | |
""" | |
#define state transition tables | |
states = [ | |
# no State (0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def highestSum(arr, k): | |
highest_sum = float('-inf') | |
n = len(arr) | |
# n must not be smaller than k | |
if n < k: | |
return -1 | |
# Compute sum of first window of size k | |
window_sum = sum([arr[i] for i in range(k)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def highestSum(arr, k): | |
highest_sum = float('-inf') | |
n = len(arr) | |
# n must not be smaller than k | |
if n < k: | |
return -1 | |
# subarray starts at i | |
for i in range(n - k + 1): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def highestSum(arr, k): | |
highest_sum = float('-inf') | |
n = len(arr) | |
# n must be not smaller than k | |
if n < k: | |
return -1 | |
left = 0 | |
right = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def longest_unique_substring(s): | |
# function to check if the characters in the substring are all unique or not | |
def is_all_unique(start_index, end_index): | |
character_set = set() | |
for z in range(start_index, end_index): | |
if s[z] in character_set: | |
return False | |
character_set.add(s[z]) | |
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
def longest_unique_substring(s): | |
longest_length = float('-inf') | |
n = len(s) | |
# empty string has the longest length is 0 | |
if n == 0: | |
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def binary_search(arr, target): | |
left = 0 | |
right = len(arr) - 1 | |
while left <= right: | |
mid = left + (right - left) // 2 | |
if arr[mid] == target: | |
return mid | |
elif arr[mid] < target: | |
left = mid + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_first_pos(arr, target): | |
left = 0 | |
right = len(arr) - 1 | |
first_position = -1 # keep track of the latest valid mid position | |
while left <= right: | |
mid = left + (right - left) // 2 | |
if arr[mid] == target: | |
first_position = mid | |
right = mid - 1 # continue searching to the left |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_last_pos(arr, target): | |
left = 0 | |
right = len(arr) - 1 | |
last_position = -1 # keep track of the latest valid mid position | |
while left <= right: | |
mid = left + (right - left) // 2 | |
if arr[mid] == target: | |
last_position = mid | |
left = mid + 1 # continue searching to the right |
OlderNewer