第 73 场双周赛
class Solution:
def mostFrequent(self, nums: List[int], key: int) -> int:
d = defaultdict(int)
cnt = res = 0
for i in range(1,len(nums)):
k = nums[i]
if nums[i-1] == key:
d[k] += 1
if d[k] > cnt:
res, cnt = k, d[k]
return res
class Solution {
public int mostFrequent(int[] nums, int key) {
int[] cnt = new int[1001];
int max = 0, res = 0;
for (int i = 1; i < nums.length; i++){
int k = nums[i];
if (nums[i-1] == key){
cnt[k]++;
if (cnt[k] > max){
max = cnt[k];
res = k;
}
}
}
return res;
}
}
class Solution:
def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:
'''
def comp(x):
q = [str(mapping[int(d)]) for d in str(x)]
return int(''.join(q))
return sorted(nums, key = comp)
'''
return sorted(nums, key = lambda x : int(''.join(str(mapping[int(d)]) for d in str(x))))
|