head -10 file.txt | tail -1
just add one
@abhishek.bailore94 how many elements in row i
@abhishek.bailore94 said in Maybe shortest c++ solution:
r[i].resize(i + 1); //What does this step do here?
Nice ! Thanks!
Python version:
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
all_rows = [None] * numRows
for i in range(numRows):
all_rows[i] = [None] * (i+1)
all_rows[i][0] = all_rows[i][i] = 1
for j in range(1, i):
all_rows[i][j] = all_rows[i-1][j-1] + all_rows[i-1][j]
return all_rows
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = [] # last in first out
left, right = '({[', ')}]'
for cha in s:
if cha in left:
stack.append(cha) # stack.pop()
if cha in right:
if len(stack)<1:
return False
left_cha = stack.pop()
if left.index(left_cha) != right.index(cha):
return False
return stack == []
Use dictionary and divmod
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
dic = {"M":1000,"CM":900,"D":500,"CD":400,"C":100,"XC":90,"L":50,"XL":40,"X":10,"IX":9,"V":5,"IV":4,"I":1}
dic_keys = sorted(dic.keys(), key=dic.get, reverse=True)
result = ''
for k in dic_keys:
quotient, num = divmod(num, dic[k]) # num = remainder
if quotient != 0:
result += k*quotient
if num == 0:
return result
return result