Re: [閒聊] 每日LeetCode已回收
※ 引述《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];
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 39.12.49.239 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1676950215.A.CAE.html
→
02/21 11:30,
2年前
, 1F
02/21 11:30, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 243 之 719 篇):