Given an array of strings words and an integer k, return thekmost frequent strings.
Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
Example 1:
Input: words = [“i”,”love”,”leetcode”,”i”,”love”,”coding”], k = 2 Output: [“i”,”love”] Explanation: “i” and “love” are the two most frequent words. Note that “i” comes before “love” due to a lower alphabetical order.
Example 2:
Input: words = [“the”,”day”,”is”,”sunny”,”the”,”the”,”the”,”sunny”,”is”,”is”], k = 4 Output: [“the”,”is”,”sunny”,”day”] Explanation: “the”, “is”, “sunny” and “day” are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.
Constraints:
1 <= words.length <= 5001 <= words[i] <= 10 words[i] consists of lowercase English letters.
k is in the range [1, The number of **unique** words[i]]
Follow-up: Could you solve it in O(n log(k)) time and O(n) extra space?
deftopKFrequent(self, words: List[str], k: int) -> List[str]: freq_dict = collections.Counter(words) li = [(freq, word) for word, freq in freq_dict.items()] li.sort(key=lambda x : (-x[0], x[1])) return [pair[1] for pair in li[:k]]
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
SymbolValue I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, 2 is written as II in Roman numeral, just two one’s added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral.
Example 1:
Input: num = 3 Output: “III” Explanation: 3 is represented as 3 ones.
Example 2:
Input: num = 58 Output: “LVIII” Explanation: L = 50, V = 5, III = 3.
Example 3:
Input: num = 1994 Output: “MCMXCIV” Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
defintToRoman(self, num: int) -> str: INT_TO_ROMAN = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')] res = '' for n, symbol in INT_TO_ROMAN: count, num = num // n, num % n res += symbol * count return res
A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.
Construct a deep copy of the list. The deep copy should consist of exactly nbrand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.
For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.
Return the head of the copied linked list.
The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
val: an integer representing Node.val
random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.
Your code will only be given the head of the original linked list.
Example 1:
**Input:** head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
**Output:** [[7,null],[13,0],[11,4],[10,2],[1,0]]
Example 2:
**Input:** head = [[1,1],[2,1]]
**Output:** [[1,1],[2,1]]
Example 3:
**Input:** head = [[3,null],[3,0],[3,null]]
**Output:** [[3,null],[3,0],[3,null]]
Example 4:
**Input:** head = []
**Output:** []
**Explanation:** The given linked list is empty (null pointer), so return null.
Constraints:
0 <= n <= 1000
-10000 <= Node.val <= 10000
Node.random is null or is pointing to some node in the linked list.
题目大意:
复制含next和random的链表。
解题思路(推荐):
此法较容易实现。先复制next指针,然后利用HashMap存储旧新节点,来复制random指针。
注意事项:
复制next指针和Map中。clone题均用此法。
Random指针不空才copy
加it = it.next,否则死循环
如果创建新Node用while it.next表示用它的父节点,否则某个field赋值如random用while it
You are assigned to put some amount of boxes onto one truck. You are given a 2D array boxTypes, where boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]:
numberOfBoxes<sub>i</sub> is the number of boxes of type i.
numberOfUnitsPerBox<sub>i</sub>is the number of units in each box of the type i.
You are also given an integer truckSize, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize.
Return the maximum total number of units that can be put on the truck.
Example 1:
Input: boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 Output: 8 Explanation: There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 3) + (2 2) + (1 1) = 8.
defmaximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int: pairs = [(li[1], li[0]) for li in boxTypes] pairs.sort(reverse=True) res, i = 0, 0 for pair in pairs: res += pair[0] * min(pair[1], truckSize) truckSize -= pair[1] if truckSize <= 0: break return res
defisPalindrome2(self, x: int) -> bool: if x < 0: returnFalse rev, original = 0, x while x > 0: rev = rev * 10 + x % 10# 121 x = x // 10# 1 return rev == original