_Gia_Cat_Luong_
Senior Member
Sau bài hqua khó tụt quần thì hnay được nghỉ xả hơi, độ khó tầm 300 bài code thiếu nhi thôi:
https://leetcode.com/submissions/detail/773922402/
https://leetcode.com/submissions/detail/773922402/
tăng độ khó, tìm regex split được ko nhỉSau bài hqua khó tụt quần thì hnay được nghỉ xả hơi, độ khó tầm 300 bài code thiếu nhi thôi:
https://leetcode.com/submissions/detail/773922402/
import re
pattern = re.compile('I[VX]|X[LC]|C[DM]|[IVXLCDM]')
codec = {'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1}
class Solution:
def romanToInt(self, s: str) -> int:
return sum(codec[r[-1]] - (len(r) - 1) * codec[r[0]] for r in pattern.findall(s))
return sum(starmap(lambda x, y: x if x >= y else -x , pairwise([p for p in map(lambda c: {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}[c], s)]))) + {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}[s[-1]]
hay vậy mà giờ toy mới nhận raCác cặp mà cứ số bé trước số lớn thì trừ, không thì cộng.
int romanToInt(char * s){
for (int n = 0, k[] = {[0]=0, ['I']=1, ['V']=5, ['X']=10, ['L']=50, ['C']=100, ['D']=500, ['M']=1000};; ++s) if (*s) n += k[s[0]] >= k[s[1]] ? k[s[0]] : -k[s[0]]; else return n;
}
from collections import Counter
class Solution:
def firstUniqChar(self, s: str) -> int:
return s.find([key for key, val in Counter(s+"A").items() if val == 1][0])
Ngon.Bài hôm nay 1 dòng python là đủ,
Python:from collections import Counter class Solution: def firstUniqChar(self, s: str) -> int: return s.find([key for key, val in Counter(s+"A").items() if val == 1][0])
return s.find(next((s for s, c in Counter(s).items() if c == 1), "#"))
Ngon đấy fenNgon.
Leetcode nó import Counter mặc định rồi fen.
Dùng next thì cái list comprehension nó sẽ dừng khi gặp chữ cái đầu tiên count = 1. ^^
return len(set(''.join(m[ord(ch) - ord('a')] for ch in w) for w in words))
bỏ cái m vào thì mới là 1 dòng chứBài hôm nay lại one line:
Python:return len(set(''.join(m[ord(ch) - ord('a')] for ch in w) for w in words))
Dậy sớm thế bác. ^^Bài hôm nay lại one line:
Python:return len(set(''.join(m[ord(ch) - ord('a')] for ch in w) for w in words))
return len(set([reduce(lambda p, c: p + [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."][ord(c)-97], w, "") for w in words]))
cheat tí thôi mà, kakakakabỏ cái m vào thì mới là 1 dòng chứ
Bài nay có bác nào 1 line được JS bài hôm nay không biểu diễn cho em với.
Đang newbie JS còn chưa khai thác được syntax, quen kiểu code truyền thống nhìn không ngầu.
return reduce(lambda acc, e: (e[0], acc[1] + e[1]) if acc[1] < (len(arr)+1)//2 else acc, enumerate(sorted((val for key, val in Counter(arr).items()), reverse=True)))[0]+1
Vâng em cảm ơn bác.Em cũng tính tham khảo để cho nó rành syntax thôi chứ trước em cũng có nghe chuyện người thật việc thật; ông đấy giải bài thì giỏi lắm, mà vào coding interview toàn ham làm code ngắn, 1 line bị downgrade xuống làm tester.https://leetcode.com/submissions/detail/776035496/
Cũng chả hiểu thì 1-line có gì mà học, newbie thì khuyên là đừng dính vào, chỉ có hại thôi.