计数
Problem: 3289. 数字小镇中的捣蛋鬼
[TOC]
思路
计数
Code
执行用时分布41ms击败100.00%;消耗内存分布16.45MB击败100.00%
###Python3
class Solution:
def getSneakyNumbers(self, nums: List[int]) -> List[int]:
return [x for x, c in Counter(nums).items() if c == 2]
Problem: 3289. 数字小镇中的捣蛋鬼
[TOC]
计数
执行用时分布41ms击败100.00%;消耗内存分布16.45MB击败100.00%
###Python3
class Solution:
def getSneakyNumbers(self, nums: List[int]) -> List[int]:
return [x for x, c in Counter(nums).items() if c == 2]
Problem: 100579. 判断操作后字符串中的数字是否相等 I
[TOC]
按题意模拟计算。
执行用时分布0ms击败100.00%;消耗内存分布7.70MB击败100.00%
###C
bool hasSameDigits(char* s) {
int len = strlen(s), i = 0;
while (i < len) s[i ++] &= 0x0f;
while (-- len > 1)
for (i = 0; i < len; ++ i)
s[i] = (s[i] + s[i + 1]) % 10;
return s[0] == s[1];
}
###Python3
class Solution:
def hasSameDigits(self, s: str) -> bool:
s = list(map(int, s))
for _ in range(len(s) - 2):
s = [(x + y) % 10 for x, y in pairwise(s)]
return s[0] == s[1]
Problem: 100162. 最大频率元素计数
[TOC]
直接一次遍历计数。
执行用时分布0ms击败100.00%;消耗内存分布6.35MB击败100.00%
###C
int maxFrequencyElements(int* nums, int numsSize) {
int cnt[101] = {0}, ma = 0, c = 0;
for (int i = 0; i < numsSize; ++ i)
if (++ cnt[nums[i]] > ma) ma = cnt[nums[i]], c = 1;
else if (cnt[nums[i]] == ma) ++ c;
return ma * c;
}
###Python3
class Solution:
def maxFrequencyElements(self, nums: List[int]) -> int:
cnt = list(Counter(nums).values())
return max(cnt) * cnt.count(max(cnt))
您若还有不同方法,欢迎贴在评论区,一起交流探讨! ^_^
↓ 点个赞,点收藏,留个言,再划走,感谢您支持作者! ^_^