Skip to content

Instantly share code, notes, and snippets.

View luojiyin1987's full-sized avatar
💭
I may be slow to respond.

luo jiyin luojiyin1987

💭
I may be slow to respond.
View GitHub Profile
@luojiyin1987
luojiyin1987 / 如何把 tuple 轉成 dictionary.py
Created August 14, 2018 01:44
如何把 tuple 轉成 dictionary
tlst = [('a1','b1','q1','p1'),
('a2','b2','q2','p2'),
('a3','b3','q3','p3')]
names = 'area brand question price'.split()
lst = [{name:value for name, value in zip(names, t)} for t in tlst]
print(lst)
class Solution:
def findRadius(self, houses, heaters):
"""
:type houses: List[int]
:type heaters: List[int]
:rtype: int
"""
ans = 0
heaters.sort()
for house in houses:
@luojiyin1987
luojiyin1987 / Count and Say.py
Created August 13, 2018 10:25
Count and Say
class Solution:
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
s = '1'
for i in range(1, n):
s = self.cal(s)
@luojiyin1987
luojiyin1987 / Maximize Distance to Closest Person.py
Created August 13, 2018 09:05
Maximize Distance to Closest Person
class Solution:
def maxDistToClosest(self, seats):
"""
:type seats: List[int]
:rtype: int
"""
hasSeats = []
len_seats = len(seats)
count = 0
@luojiyin1987
luojiyin1987 / License Key Formatting.py
Created August 13, 2018 07:19
License Key Formatting
class Solution:
def licenseKeyFormatting(self, S, K):
"""
:type S: str
:type K: int
:rtype: str
"""
S =''.join(S.split('-'))
S = str.upper(S)
temp = ''
@luojiyin1987
luojiyin1987 / Largest Number At Least Twice of Others.py
Created August 12, 2018 11:09
Largest Number At Least Twice of Others
class Solution(object):
def dominantIndex(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maxn = max(nums)
for num in nums:
if num != maxn and 2 * num > maxn:
return -1
@luojiyin1987
luojiyin1987 / Longest Word in Dictionary.py
Created August 12, 2018 10:24
Longest Word in Dictionary
class Solution(object):
def longestWord(self, words):
"""
:type words: List[str]
:rtype: str
"""
wSet =set([''])
ans = ''
for word in sorted(words):
@luojiyin1987
luojiyin1987 / Longest Continuous Increasing Subsequence.py
Created August 12, 2018 10:00
Longest Continuous Increasing Subsequence
class Solution(object):
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums_length = len(nums)
if nums_length <=1:
return nums_length
@luojiyin1987
luojiyin1987 / Find Smallest Letter Greater Than Target.py
Created August 12, 2018 09:41
Find Smallest Letter Greater Than Target
class Solution(object):
def nextGreatestLetter(self, letters, target):
"""
:type letters: List[str]
:type target: str
:rtype: str
"""
letters = set(map(ord, letters))
for x in range(1, 27):
candidate = ord(target) + x
@luojiyin1987
luojiyin1987 / Min Cost Climbing Stairs.py
Created August 11, 2018 09:17
Min Cost Climbing Stairs
class Solution:
def minCostClimbingStairs(self, cost):
"""
:type cost: List[int]
:rtype: int
"""
size = len(cost)
dp = [cost[0], cost[1]]
for x in range(2, size):
dp.append(min(dp[x -1], dp[x-2]) + cost[x])