實現函數 ToLowerCase(),該函數接收一個字符串參數 str,並將該字符串中的大寫字母轉換成小寫字母,以後返回新的字符串。python
示例 1:數組
輸入: "Hello" 輸出: "hello"
示例 2:app
輸入: "here" 輸出: "here"
示例 3:函數
輸入: "LOVELY" 輸出: "lovely"
大寫變小寫 ,內置函數lower()
class Solution: def toLowerCase(self, str: str) -> str: s = str.lower() return s
國際摩爾斯密碼定義一種標準編碼方式,將每一個字母對應於一個由一系列點和短線組成的字符串, 好比: "a"
對應 ".-"
, "b"
對應 "-..."
, "c"
對應 "-.-."
, 等等。編碼
爲了方便,全部26個英文字母對應摩爾斯密碼錶以下:翻譯
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
給定一個單詞列表,每一個單詞能夠寫成每一個字母對應摩爾斯密碼的組合。例如,"cab" 能夠寫成 "-.-..--...",(即 "-.-." + "-..." + ".-"字符串的結合)。咱們將這樣一個鏈接過程稱做單詞翻譯。code
返回咱們能夠得到全部詞不一樣單詞翻譯的數量。排序
例如: 輸入: words = ["gin", "zen", "gig", "msg"] 輸出: 2 解釋: 各單詞翻譯以下: "gin" -> "--...-." "zen" -> "--...-." "gig" -> "--...--." "msg" -> "--...--." 共有 2 種不一樣翻譯, "--...-." 和 "--...--.".
這題目看上去很難,真的作的話很簡單,先轉換摩斯密碼形式,再用SET去重就能夠了
class Solution: def uniqueMorseRepresentations(self, words: List[str]) -> int: 1 dic = { 'a': ".-", 'b': "-...", 'c': "-.-.", 'd': "-..", 'e': ".", 'f': "..-.", 'g': "--.", 'h': "....", 'i': "..", 'j': ".---", 'k': "-.-", 'l': ".-..", 'm': "--", 'n': "-.", 'o': "---", 'p': ".--.", 'q': "--.-", 'r': ".-.", 's': "...", 't': "-", 'u': "..-", 'v': "...-", 'w': ".--", 'x': "-..-", 'y': "-.--", 'z': "--.." }; new_word = [] x = '' cont = 0 for a in range(len(words)): new_word.append([]) for a in range(len(words)): for b in range(len(words[a])): x += dic[words[a][b]] cont += 1 if cont == len(words[a]): new_word[a] = x cont = 0 x = '' return len(set(new_word))
給定一個按非遞減順序排序的整數數組 A
,返回每一個數字的平方組成的新數組,要求也按非遞減順序排序。leetcode
示例 1:字符串
輸入:[-4,-1,0,3,10] 輸出:[0,1,9,16,100]
示例 2:
輸入:[-7,-3,2,3,11] 輸出:[4,9,9,49,121]
提示:
1 <= A.length <= 10000
-10000 <= A[i] <= 10000
A
已按非遞減順序排序。裏面數開平方且有序怕列
class Solution: def sortedSquares(self, A: List[int]) -> List[int]: return sorted([ i**2 for i in A ])
有更加好的思路,或者解題方法評論區留言謝謝