Skip to content

Instantly share code, notes, and snippets.

@nniranjhana
Last active September 10, 2020 02:50
Show Gist options
  • Save nniranjhana/225133f4025f3c2ab6b3a3e77487f83f to your computer and use it in GitHub Desktop.
Save nniranjhana/225133f4025f3c2ab6b3a3e77487f83f to your computer and use it in GitHub Desktop.
crank leetcode
# https://leetcode.com/problems/palindrome-number/
class Solution:
def isPalindrome(self, x: int) -> bool:
x = str(x)
p = len(x)-1
for i in range(len(x)//2):
if(x[i]!=x[p]):
return False
p = p - 1
return True
# https://leetcode.com/problems/search-insert-position/
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
import bisect
index = bisect.bisect(nums, target, 0, len(nums))
if(nums[index-1]==target):
return index-1
else:
return index
# https://leetcode.com/problems/single-number/
class Solution:
def singleNumber(self, nums: List[int]) -> int:
num = nums[0]
for i in range(1, len(nums)):
num = num ^ nums[i]
return num
# https://leetcode.com/problems/valid-anagram/
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if(len(s)!=len(t)):
return False
s_, t_ = [0]*26, [0]*26
for (i, j) in zip(s, t):
s_[ord(i)-97] += 1
t_[ord(j)-97] += 1
if(s_ == t_):
return True
else:
return False
# https://leetcode.com/problems/reverse-string/
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
for i in range(len(s)//2):
temp = s[i]
s[i] = s[len(s)-i-1]
s[len(s)-i-1] = temp
return s
# https://leetcode.com/problems/delete-node-in-a-linked-list/
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
node.val = node.next.val
node.next = node.next.next
# https://leetcode.com/problems/missing-number/
class Solution:
def missingNumber(self, nums: List[int]) -> int:
sum = 0
for i in nums:
sum += i
l = len(nums)
return ((l*(l+1)//2) - sum)
# https://leetcode.com/problems/first-unique-character-in-a-string/
class Solution:
def firstUniqChar(self, s: str) -> int:
count = {}
for i in s:
if i in count.keys():
count[i] += 1
else: count[i] = 1
e = -1
for key, val in count.items():
if(val == 1):
e = key
break
if(e == -1): return e
for i in range(len(s)):
if(e == s[i]): return i
# https://leetcode.com/problems/top-k-frequent-elements/
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
rep = {}
for i in nums:
if i not in rep.keys():
rep[i] = 1
else:
rep[i] += 1
rep = {p: v for p, v in sorted(rep.items(), key=lambda item: item[1], reverse = True)}
rep = list(rep.keys())
maxk = []
for i in range(k):
maxk.append(rep[i])
return maxk
# https://leetcode.com/problems/shuffle-the-array/
class Solution:
def shuffle(self, nums: List[int], n: int) -> List[int]:
p1 = 0; p2 = n
ret = []
for i in range(n):
ret.append(nums[p1]); ret.append(nums[p2])
p1 = p1 + 1; p2 = p2 + 1
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment