Re: [闲聊] 每日LeetCode

楼主: DDFox (冒险者兼清洁工)   2023-02-21 11:30:12
※ 引述《umi0912umi (大空スバル的香肠)》之铭言:
: 540. Single Element in a Sorted Array
: 题目:
: 给一个排列过的int阵列,里面的元素都正好出现2次,
: 除了某一元素只会出现一次,找出他并回传他的值,
: 需要在O(logn)时间及O(1)空间内完成
: Example 1:
: Input: nums = [1,1,2,3,3,4,4,8,8]
: Output: 2
: Example 2:
: Input: nums = [3,3,7,7,10,11,11]
: Output: 10
思路:
最近在看OCP,所以看到阵列就想试试看用酷酷的lambda
想了一下没想到,就改用递回来做了
==================
class Solution {
public int singleNonDuplicate(int[] nums) {
if (nums.length > 1 && nums[0] == nums[1]) {
return singleNonDuplicate(Arrays.copyOfRange(nums, 2, nums.length));
} else {
return nums[0];
}
}
}
楼主: DDFox (冒险者兼清洁工)   2022-02-21 11:30:00
结果时间复杂度还是请ai算的哈我好烂

Links booklink

Contact Us: admin [ a t ] ucptt.com