找出数组中重复的数字。 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。 来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
AC代码class Solution:
def findRepeatNumber(self, nums: List[int]) -> int:
easylist = [0 for _ in range(len(nums))]
for i in nums:
easylist[i] += 1
if easylist[i] >= 2:
return i
妙解
class Solution {
public:
int findRepeatNumber(vector& nums) {
int n = nums.size();
for(int i = 0; i
关注
打赏
热门博文
- 【Leetcode】剑指Offer 34:二叉树中和为某一值的路径
- 【Leetcode】剑指Offer 33:二叉搜索树的后序遍历序列
- 【Leetcode】剑指Offer 32-III: 从上到下打印二叉树 III
- 【Leetcode】剑指Offer 32-II: 从上到下打印二叉树 II
- 【Leetcode】剑指Offer 32-I:从上到下打印二叉树
- 【Leetcode】剑指Offer 31:栈的压入、弹出序列
- 【Leetcode】剑指Offer 30:包含min函数的栈
- 【Leetcode】剑指Offer 29:顺时针打印矩阵
- 【Leetcode】剑指Offer 28:对称的二叉树
- 【Leetcode】剑指Offer 27:二叉树的镜像