iT邦幫忙

2

leetcode 365天 #Day111

  • 分享至 

  • xImage
  •  

耶黑~快速寫完的夜晚
Yes


  1. Check Distances Between Same Letters (easy)
    https://leetcode.com/problems/check-distances-between-same-letters/
    Submission:https://leetcode.com/submissions/detail/854066411/
    You are given a 0-indexed string s consisting of only lowercase English letters, where each letter in s appears exactly twice. You are also given a 0-indexed integer array distance of length 26.

Each letter in the alphabet is numbered from 0 to 25 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, ... , 'z' -> 25).

In a well-spaced string, the number of letters between the two occurrences of the ith letter is distance[i]. If the ith letter does not appear in s, then distance[i] can be ignored.

Return true if s is a well-spaced string, otherwise return false.

檢查兩個一樣的字母,距離有沒有跟他所給的distance一樣

from collections import defaultdict
class Solution:
    #s裡面的每個字母都必定出現兩次
    #distance裡面儲存了每個字母所必須相隔的距離
    #對了,其實可以用ord處理就好
    def checkDistances(self, s: str, distance: List[int]) -> bool:
        indexDict = {'a':0,'b':1,'c':2,'d':3,'e':4,'f':5,'g':6,'h':7,'i':8,'j':9,
                     'k':10,'l':11,'m':12,'n':13,'o':14,'p':15,'q':16,'r':17,'s':18,
                     't':19,'u':20,'v':21,'w':22,'x':23,'y':24,'z':25}
        rec = defaultdict(list)
        for i in range(len(s)):
            rec[s[i]].append(i)
        for i,v in rec.items():
            if distance[indexDict[i]] != v[1]-v[0]-1:
                return 0
        return 1

  1. Intersection of Three Sorted Arrays
    https://leetcode.com/problems/intersection-of-three-sorted-arrays/
    Submission:https://leetcode.com/submissions/detail/854061860/
    Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays.
class Solution:
    #strictly increasing
    #不會有重複的
    def arraysIntersection(self, arr1: List[int], arr2: List[int], arr3: List[int]) -> List[int]:
        return sorted(set(arr1) & set(arr2) & set(arr3))

  1. Partitioning Into Minimum Number Of Deci-Binary Numbers(medium)
    https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/
    Submission:https://leetcode.com/submissions/detail/854058502/
    A decimal number is called deci-binary if each of its digits is either 0 or 1 without any leading zeros. For example, 101 and 1100 are deci-binary, while 112 and 3001 are not.

Given a string n that represents a positive decimal integer, return the minimum number of positive deci-binary numbers needed so that they sum up to n.
用1010101所組成的數字組成n

class Solution:
    #由0跟1組成的數字組成n
    def minPartitions(self, n: str) -> int:
        nSet = set(n)
        return max(nSet)

  1. Sort Characters By Frequency
    https://leetcode.com/problems/sort-characters-by-frequency/
    Submission:https://leetcode.com/submissions/detail/854057180/
    Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.

Return the sorted string. If there are multiple answers, return any of them.

class Solution:
    #依照出現的次數排序(從出現次數多到少)
    def frequencySort(self, s: str) -> str:
        sC = Counter(s)
        numList = sorted(sC.items(), key = lambda x:-x[1])
        ans = "".join([i[0]*i[1] for i in numList])
        return ans
    
        

以上就是今天了練習感謝大家QQ


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言