From c3ba5dfc4cec9f854c31cadc3877ff90896cd1a3 Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Wed, 10 Jun 2020 09:04:48 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E6=95=B0=E7=BB=84=E7=9B=B8=E5=85=B3.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blogs/Algorithm/剑指 Offer/数组相关.md | 69 ++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/blogs/Algorithm/剑指 Offer/数组相关.md b/blogs/Algorithm/剑指 Offer/数组相关.md index ef795ba..7883f54 100644 --- a/blogs/Algorithm/剑指 Offer/数组相关.md +++ b/blogs/Algorithm/剑指 Offer/数组相关.md @@ -27,3 +27,72 @@ class Solution { } ``` +[11. 旋转数组的最小数字](https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/) + +```java +class Solution { + + public int minArray(int[] numbers) { + int i = 0, j = numbers.length - 1; + while (i < j) { + int m = (i + j) / 2; + if (numbers[m] > numbers[j]) { + i = m + 1; + } else if (numbers[m] < numbers[j]) { + j = m; + } else { + j--; + } + } + return numbers[i]; + } +} +``` + +[21. 调整数组顺序使奇数位于偶数前面](https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/) + +```java +class Solution { + + public int[] exchange(int[] nums) { + int i = 0, j = nums.length - 1; + while (i < j) { + if (nums[i] % 2 == 1 && nums[j] % 2 == 0) { + i++; + j--; + } else if (nums[i] % 2 == 1 && nums[j] % 2 == 1) { + i++; + } else if (nums[i] % 2 == 0 && nums[j] % 2 == 1) { + int temp = nums[i]; + nums[i++] = nums[j]; + nums[j--] = temp; + } else { + j--; + } + } + return nums; + } +} +``` + +```java +class Solution { + + public int[] exchange(int[] nums) { + int i = 0, j = nums.length - 1, temp; + while (i < j) { + while (i < j && (nums[i] & 1) == 1) { + i++; + } + while (i < j && (nums[j] & 1) == 0) { + j--; + } + temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + return nums; + } +} +``` +