[闲聊] 每日Leetcode

楼主: Rushia (みけねこ的鼻屎)   2022-03-29 10:59:05
https://leetcode.com/problems/find-the-duplicate-number/
287. Find the Duplicate Number
Given an array of integers nums containing n + 1 integers where each integer
is in the range [1, n] inclusive.
There is only one repeated number in nums, return this repeated number.
You must solve the problem without modifying the array nums and uses only
constant extra space.
class Solution {
public int findDuplicate(int[] nums) {
for(int n:nums){
int index = n > 0 ? n :-n;
if(nums[index] < 0) return index;
nums[index] = - nums[index];
}
return -1;
}
}
我也是看报纸才知道 这题可以用二元搜寻 ㄉㄅㄑ

Links booklink

Contact Us: admin [ a t ] ucptt.com