This file contains hidden or 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
# https://leetcode.com/problems/zigzag-conversion/ | |
def zigzag(strIn, numRows): | |
strLen = len(strIn) | |
strOut = "" | |
cycleLen = 2*numRows-2 | |
for row in range(numRows): | |
for index in range(row, strLen, cycleLen): | |
strOut = strOut + strIn[index] | |
inner_index = index + cycleLen - 2*row | |
if row != 0 and row!=numRows-1 and inner_index < strLen: |
This file contains hidden or 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
#Time Complexity: log base 2 of x | |
def brute_force_parity(x): | |
odd = False | |
while x>0: | |
odd ^= bool(x & 1) | |
x >>= 1 | |
return odd | |
#Time Complexity: number of 1 | |
def parity(x): |
This file contains hidden or 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 int2hex(x): | |
str = '' | |
while x>0: | |
remaining = x%16 | |
if remaining <10: | |
str = chr(ord('0')+remaining) + str | |
else: | |
str = chr(ord('a')+(remaining-10)) + str | |
x //= 16 | |
return ('0x'+str) if str else '0x0' |
This file contains hidden or 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
import heapq | |
def onlineMedian(minHeap, maxHeap, i): | |
heapq.heappush(minHeap, i) | |
if len(minHeap) > len(maxHeap)+1: | |
value = heapq.heappop(minHeap) | |
heapq.heappush(maxHeap, value*-1) | |
if len(maxHeap) == len(minHeap): | |
print('median: {}'.format( (maxHeap[0]*-1 + minHeap[0]) / 2 )) | |
else: |
This file contains hidden or 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 maxArea(height): | |
left = 0 | |
right = len(height)-1 | |
maxA = 0 | |
for n in range(len(height)-1,0,-1): | |
maxA = max(maxA, min(height[left], height[right]) * n) | |
if height[left] < height[right]: | |
left += 1 | |
else: |
This file contains hidden or 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 ListNode: | |
def __init__(self, x): | |
self.val = x | |
self.next = None | |
def rotateRight(head, k): | |
if head is None or k==0: | |
return head | |
node = head |
This file contains hidden or 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
''' | |
Hi, here's your problem today. This problem was recently asked by Microsoft: | |
You are given two 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. | |
Example: | |
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) | |
Output: 7 -> 0 -> 8 | |
Explanation: 342 + 465 = 807. | |
''' |
This file contains hidden or 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
''' | |
Hi, here's your problem today. This problem was recently asked by Microsoft: | |
Given a string, find the length of the longest substring without repeating characters. | |
Can you find a solution in linear time? | |
''' | |
class Solution: | |
def lengthOfLongestSubstring(self, s): | |
seen = set() |
This file contains hidden or 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
''' | |
Hi, here's your problem today. This problem was recently asked by AirBNB: | |
Given a sorted array, A, with possibly duplicated elements, find the indices of the first and last occurrences of a target element, x. Return -1 if the target is not found. | |
Example: | |
Input: A = [1,3,3,5,7,8,9,9,9,15], target = 9 | |
Output: [6,8] | |
Input: A = [100, 150, 150, 153], target = 150 |
This file contains hidden or 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
''' | |
Hi, here's your problem today. This problem was recently asked by Google: | |
Given a list of numbers with only 3 unique numbers (1, 2, 3), sort the list in O(n) time. | |
Example 1: | |
Input: [3, 3, 2, 1, 3, 2, 1] | |
Output: [1, 1, 2, 2, 3, 3, 3] | |
''' | |
def sortNums(nums): |