Problem500
概要:入力として単語のリストが与えられる. 英字キーボードの特定の一行だけで打てる単語を見つける.
Python3
愚直な方法.
class Solution: def findWords(self, words: List[str]) -> List[str]: res = [] d = {'Q': 0, 'W': 0, 'E': 0, 'R': 0, 'T': 0, 'Y': 0, 'U': 0, 'I': 0, 'O': 0, 'P': 0, 'q': 0, 'w': 0, 'e': 0, 'r': 0, 't': 0, 'y': 0, 'u': 0, 'i': 0, 'o': 0, 'p': 0, 'A': 1, 'S': 1, 'D': 1, 'F': 1, 'G': 1, 'H': 1, 'J': 1, 'K': 1, 'L': 1, 'a': 1, 's': 1, 'd': 1, 'f': 1, 'g': 1, 'h': 1, 'j': 1, 'k': 1, 'l': 1, 'Z': 2, 'X': 2, 'C': 2, 'V': 2, 'B': 2, 'N': 2, 'M': 2, 'z': 2, 'x': 2, 'c': 2, 'v': 2, 'b': 2, 'n': 2, 'm': 2} for word in words: isAns = True lw = len(word) if lw==0: continue elif lw==1: res.append(word) else: for i in range(lw-1): if d[word[i]]==d[word[i+1]]: continue else: isAns = False break if isAns: res.append(word) return res
Runtime: 36 ms, faster than 67.67% of Python3 online submissions for Keyboard Row. Memory Usage: 13.9 MB, less than 6.67% of Python3 online submissions for Keyboard Row.
Problem575
概要:与えられたキャンディのリストを二人に等しい数に分けるとき、最もたくさんの種類のキャンディーを持っている方は何種類のキャンディーを持っているか?
Python3
答えになりうる最大はキャンディーの半分の数。
class Solution: def distributeCandies(self, candies: List[int]) -> int: return int(min([len(set(candies)), len(candies)/2]))
Problem1002
概要:英単語がリストとして与えられるので、全てに共通して出現する文字を見つける. ooが全てに出現するなら o
o
も答えに含める.
Python3
はじめに最も短い単語を見つけて、その単語を対象にして文字が全ての単語に出現するかをチェック.
class Solution: def commonChars(self, A: List[str]) -> List[str]: res = [] wordLengthList = [len(a) for a in A] mostShortWord = A[wordLengthList.index(min(wordLengthList))] for ci in mostShortWord: for i, ai in enumerate(A): if ci in ai: A[i] = ai.replace(ci, '', 1) continue else: ci = '' break if not ci=='': res.append(ci) return res
Runtime: 52 ms, faster than 85.77% of Python3 online submissions for Find Common Characters. Memory Usage: 13.8 MB, less than 5.55% of Python3 online submissions for Find Common Characters.
Problem1025
概要:N%2==0
を返せばいいとわかる. (問題文)
Rust
impl Solution { pub fn divisor_game(n: i32) -> bool { return n%2==0; } }
Runtime: 0 ms, faster than 100.00% of Rust online submissions for Divisor Game. Memory Usage: 2.4 MB, less than 100.00% of Rust online submissions for Divisor Game.
Problem1051
class Solution { public: int heightChecker(vector<int>& heights) { int res = 0; auto sortedh {std::vector<int>(heights)}; std::sort(sortedh.begin(), sortedh.end()); for(size_t i=0; i<sortedh.size(); i++) if(sortedh[i]!=heights[i]) res++; return res; } };
Problem1078
概要:テキストが渡され、さらに二つの単語が渡される。テキスト中に二つの単語が連続している箇所が存在するとき、その次にきた単語を返す.
Python
愚直に一つずつチェック。wordi==first
の時のみ words[i+1]==second
が評価される.
class Solution: def findOcurrences(self, text: str, first: str, second: str) -> List[str]: res = [] words = text.split(" ") if len(words)<2: return res else: for i, wordi in enumerate(words[:-2]): if wordi==first and words[i+1]==second: res.append(words[i+2]) else: continue return res
class Solution: def findOcurrences(self, text: str, first: str, second: str) -> List[str]: text = text.split(" ") textlen = len(text) res = [] if textlen>1: for i in range(textlen-2): if text[i]==first and text[i+1]==second: res.append(text[i+2]) return res
Runtime: 40 ms, faster than 28.77% of Python3 online submissions for Occurrences After Bigram. Memory Usage: 14 MB, less than 100.00% of Python3 online submissions for Occurrences After Bigram.