diff --git a/scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_287_Array.scala b/scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_287_Array.scala index 1d0e66dbe0591a73bad55625b6afc1c383d95427..48029485a9f012f6da437eb27ce2bb4c405cb2d3 100644 --- a/scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_287_Array.scala +++ b/scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_287_Array.scala @@ -1,75 +1,75 @@ package io.github.dreamylost /** - * 找出数组中重复的数,数组值在 [1, n] 之间 - * + * 找出数组中重复的数,数组值在 [1, n] 之间 + * * 287. Find the Duplicate Number (Medium) - * + * * 要求不能修改数组,也不能使用额外的空间。 - * + * * @author 梦境迷离 - * @time 2018年7月16日 - * @version v1.0 - */ + * @time 2018年7月16日 + * @version v1.0 + */ object Leetcode_287_Array { - def main(args: Array[String]) { - val nums = Array(1, 2, 3, 3, 4, 5, 6) - val ret = Leetcode_287_Array.findDuplicate2(nums) - print(ret) + def main(args: Array[String]) { + val nums = Array(1, 2, 3, 3, 4, 5, 6) + val ret = Leetcode_287_Array.findDuplicate2(nums) + print(ret) - } + } - /** - * 二分查找解法: - */ - val findDuplicate = (nums: Array[Int]) ⇒ { - var l = 1 - var h = nums.length - 1 - while (l <= h) { - val mid = l + (h - l) / 2 - var cnt = 0 - for (i <- 0 until nums.length) { - if (nums(i) <= mid) - cnt = cnt + 1 - } - if (cnt > mid) - h = mid - 1 - else - l = mid + 1 - } - l + /** + * 二分查找解法: + */ + val findDuplicate = (nums: Array[Int]) ⇒ { + var l = 1 + var h = nums.length - 1 + while (l <= h) { + val mid = l + (h - l) / 2 + var cnt = 0 + for (i <- 0 until nums.length) { + if (nums(i) <= mid) + cnt = cnt + 1 + } + if (cnt > mid) + h = mid - 1 + else + l = mid + 1 } + l + } - /** - * 双指针解法,类似于有环链表中找出环的入口 - * + /** + * 双指针解法,类似于有环链表中找出环的入口 + * * 有环找入口:慢指针走过的路程是: - * + * * S_slow = |OA| +|AB| = x + y. - * 快指针走过的路程是: - * S_fast = |OA| + |AB| + |BA| + |AB| = x + y + z + y. - * 又因为快指针的速度是慢指针的两倍,所以在相同时间内快指针走过的路程是慢指针的两倍,所以 - * S_fast = 2 * S_slow - * 即2(x + y) = x + y + z + y. - * 求得z = x. - * 所以说明 - * |BA| = |OA|. - * 所以在两个指针相遇后,将慢指针移到O点起始位置,即链表头指针位置,快指针仍然在B点。 - * 然后它们一起向前移动,每次移动一个位置,由于|BA| = |OA|, 所以他们最终肯定会在A点相遇,A点这个相遇点就是环的入口点。 - */ - def findDuplicate2(nums: Array[Int]): Int = { - var slow = nums(0) - var fast = nums(nums(0)) - while (slow != fast) { - slow = nums(slow) - fast = nums(nums(fast)) - } - fast = 0 - while (slow != fast) { - slow = nums(slow) - fast = nums(fast) - } - slow + * 快指针走过的路程是: + * S_fast = |OA| + |AB| + |BA| + |AB| = x + y + z + y. + * 又因为快指针的速度是慢指针的两倍,所以在相同时间内快指针走过的路程是慢指针的两倍,所以 + * S_fast = 2 * S_slow + * 即2(x + y) = x + y + z + y. + * 求得z = x. + * 所以说明 + * |BA| = |OA|. + * 所以在两个指针相遇后,将慢指针移到O点起始位置,即链表头指针位置,快指针仍然在B点。 + * 然后它们一起向前移动,每次移动一个位置,由于|BA| = |OA|, 所以他们最终肯定会在A点相遇,A点这个相遇点就是环的入口点。 + */ + def findDuplicate2(nums: Array[Int]): Int = { + var slow = nums(0) + var fast = nums(nums(0)) + while (slow != fast) { + slow = nums(slow) + fast = nums(nums(fast)) + } + fast = 0 + while (slow != fast) { + slow = nums(slow) + fast = nums(fast) } -} \ No newline at end of file + slow + } +} diff --git a/scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_80_Array.scala b/scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_80_Array.scala index 88ba4d46065dc63e458ee812a98d72dc602f2fa8..b6e86b895c0f1d0dcb7c48c525ddfc4bdfcb4ff0 100644 --- a/scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_80_Array.scala +++ b/scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_80_Array.scala @@ -8,14 +8,14 @@ package io.github.dreamylost */ object Leetcode_80_Array { - def removeDuplicates(nums: Array[Int]): Int = { - var i = 0 - for (n <- nums) { - if (i < 2 || n > nums(i - 2)) { - nums(i) = n - i += 1 - } - } - i + def removeDuplicates(nums: Array[Int]): Int = { + var i = 0 + for (n <- nums) { + if (i < 2 || n > nums(i - 2)) { + nums(i) = n + i += 1 + } } -} \ No newline at end of file + i + } +}