Skip to content

Commit 4275747

Browse files
committed
update
1 parent f23d878 commit 4275747

18 files changed

+884
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
25+
src/main/java/com/shuzijun/leetcode/editor/cn/doc/solution/*.*
26+
src/main/java/com/shuzijun/leetcode/editor/cn/doc/submission/*.*

.idea/leetcode-pro/editor.xml

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.shuzijun.leetcode.editor.cn;
2+
3+
/**
4+
* 已知一个长度为 <code>n</code> 的数组,预先按照升序排列,经由 <code>1</code> 到 <code>n</code> 次 <strong>旋转</strong> 后,得到输入数组。例如,原数组 <code>nums = [0,1,2,4,5,6,7]</code> 在变化后可能得到:
5+
* <ul>
6+
* <li>若旋转 <code>4</code> 次,则可以得到 <code>[4,5,6,7,0,1,2]</code></li>
7+
* <li>若旋转 <code>7</code> 次,则可以得到 <code>[0,1,2,4,5,6,7]</code></li>
8+
* </ul>
9+
*
10+
* <p>注意,数组 <code>[a[0], a[1], a[2], ..., a[n-1]]</code> <strong>旋转一次</strong> 的结果为数组 <code>[a[n-1], a[0], a[1], a[2], ..., a[n-2]]</code> 。</p>
11+
*
12+
* <p>给你一个元素值 <strong>互不相同</strong> 的数组 <code>nums</code> ,它原来是一个升序排列的数组,并按上述情形进行了多次旋转。请你找出并返回数组中的 <strong>最小元素</strong> 。</p>
13+
*
14+
* <p> </p>
15+
*
16+
* <p><strong>示例 1:</strong></p>
17+
*
18+
* <pre>
19+
* <strong>输入:</strong>nums = [3,4,5,1,2]
20+
* <strong>输出:</strong>1
21+
* <strong>解释:</strong>原数组为 [1,2,3,4,5] ,旋转 3 次得到输入数组。
22+
* </pre>
23+
*
24+
* <p><strong>示例 2:</strong></p>
25+
*
26+
* <pre>
27+
* <strong>输入:</strong>nums = [4,5,6,7,0,1,2]
28+
* <strong>输出:</strong>0
29+
* <strong>解释:</strong>原数组为 [0,1,2,4,5,6,7] ,旋转 4 次得到输入数组。
30+
* </pre>
31+
*
32+
* <p><strong>示例 3:</strong></p>
33+
*
34+
* <pre>
35+
* <strong>输入:</strong>nums = [11,13,15,17]
36+
* <strong>输出:</strong>11
37+
* <strong>解释:</strong>原数组为 [11,13,15,17] ,旋转 4 次得到输入数组。
38+
* </pre>
39+
*
40+
* <p> </p>
41+
*
42+
* <p><strong>提示:</strong></p>
43+
*
44+
* <ul>
45+
* <li><code>n == nums.length</code></li>
46+
* <li><code>1 <= n <= 5000</code></li>
47+
* <li><code>-5000 <= nums[i] <= 5000</code></li>
48+
* <li><code>nums</code> 中的所有整数 <strong>互不相同</strong></li>
49+
* <li><code>nums</code> 原来是一个升序排序的数组,并进行了 <code>1</code> 至 <code>n</code> 次旋转</li>
50+
* </ul>
51+
* <div><div>Related Topics</div><div><li>数组</li><li>二分查找</li></div></div><br><div><li>👍 512</li><li>👎 0</li></div>
52+
*/
53+
54+
public class Q153FindMinimumInRotatedSortedArray {
55+
public static void main(String[] args) {
56+
Q153FindMinimumInRotatedSortedArray.Solution solution = new Q153FindMinimumInRotatedSortedArray().new Solution();
57+
int[] nums = {4, 5, 6, 7, 0, 1, 2};
58+
System.out.println(solution.findMin(nums));
59+
}
60+
61+
//leetcode submit region begin(Prohibit modification and deletion)
62+
class Solution {
63+
public int findMin(int[] nums) {
64+
int low = 0;
65+
int high = nums.length - 1;
66+
while (low < high) {
67+
int pivot = low + (high - low) / 2;
68+
if (nums[pivot] < nums[high]) {
69+
high = pivot;
70+
} else {
71+
low = pivot + 1;
72+
}
73+
}
74+
return nums[low];
75+
}
76+
}
77+
//leetcode submit region end(Prohibit modification and deletion)
78+
79+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.shuzijun.leetcode.editor.cn;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/**
8+
<p>给定一个整数数组 <code>nums</code> 和一个整数目标值 <code>target</code>,请你在该数组中找出 <strong>和为目标值 </strong><em><code>target</code></em> 的那 <strong>两个</strong> 整数,并返回它们的数组下标。</p>
9+
10+
<p>你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。</p>
11+
12+
<p>你可以按任意顺序返回答案。</p>
13+
14+
<p> </p>
15+
16+
<p><strong>示例 1:</strong></p>
17+
18+
<pre>
19+
<strong>输入:</strong>nums = [2,7,11,15], target = 9
20+
<strong>输出:</strong>[0,1]
21+
<strong>解释:</strong>因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
22+
</pre>
23+
24+
<p><strong>示例 2:</strong></p>
25+
26+
<pre>
27+
<strong>输入:</strong>nums = [3,2,4], target = 6
28+
<strong>输出:</strong>[1,2]
29+
</pre>
30+
31+
<p><strong>示例 3:</strong></p>
32+
33+
<pre>
34+
<strong>输入:</strong>nums = [3,3], target = 6
35+
<strong>输出:</strong>[0,1]
36+
</pre>
37+
38+
<p> </p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>2 <= nums.length <= 10<sup>4</sup></code></li>
44+
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
45+
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
46+
<li><strong>只会存在一个有效答案</strong></li>
47+
</ul>
48+
49+
<p><strong>进阶:</strong>你可以想出一个时间复杂度小于 <code>O(n<sup>2</sup>)</code> 的算法吗?</p>
50+
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li></div></div><br><div><li>👍 11546</li><li>👎 0</li></div>
51+
*/
52+
53+
public class Q1TwoSum{
54+
public static void main(String[] args) {
55+
Solution solution = new Q1TwoSum().new Solution();
56+
int[] nums = {2, 7, 11, 15};
57+
int target = 9;
58+
System.out.println(Arrays.toString(solution.twoSum(nums, target)));
59+
}
60+
//leetcode submit region begin(Prohibit modification and deletion)
61+
class Solution {
62+
public int[] twoSum(int[] nums, int target) {
63+
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
64+
for (int i = 0; i < nums.length; i++) {
65+
int complement = target - nums[i];
66+
if (map.containsKey(complement)) {
67+
return new int[]{map.get(complement), i};
68+
}
69+
map.put(nums[i], i);
70+
}
71+
throw new IllegalArgumentException("No two sum solution");
72+
}
73+
}
74+
//leetcode submit region end(Prohibit modification and deletion)
75+
76+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.shuzijun.leetcode.editor.cn;
2+
/**
3+
<p>整数数组 <code>nums</code> 按升序排列,数组中的值 <strong>互不相同</strong> 。</p>
4+
5+
<p>在传递给函数之前,<code>nums</code> 在预先未知的某个下标 <code>k</code>(<code>0 <= k < nums.length</code>)上进行了 <strong>旋转</strong>,使数组变为 <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>(下标 <strong>从 0 开始</strong> 计数)。例如, <code>[0,1,2,4,5,6,7]</code> 在下标 <code>3</code> 处经旋转后可能变为 <code>[4,5,6,7,0,1,2]</code> 。</p>
6+
7+
<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ,如果 <code>nums</code> 中存在这个目标值 <code>target</code> ,则返回它的下标,否则返回 <code>-1</code> 。</p>
8+
9+
<p> </p>
10+
11+
<p><strong>示例 1:</strong></p>
12+
13+
<pre>
14+
<strong>输入:</strong>nums = [<code>4,5,6,7,0,1,2]</code>, target = 0
15+
<strong>输出:</strong>4
16+
</pre>
17+
18+
<p><strong>示例 2:</strong></p>
19+
20+
<pre>
21+
<strong>输入:</strong>nums = [<code>4,5,6,7,0,1,2]</code>, target = 3
22+
<strong>输出:</strong>-1</pre>
23+
24+
<p><strong>示例 3:</strong></p>
25+
26+
<pre>
27+
<strong>输入:</strong>nums = [1], target = 0
28+
<strong>输出:</strong>-1
29+
</pre>
30+
31+
<p> </p>
32+
33+
<p><strong>提示:</strong></p>
34+
35+
<ul>
36+
<li><code>1 <= nums.length <= 5000</code></li>
37+
<li><code>-10^4 <= nums[i] <= 10^4</code></li>
38+
<li><code>nums</code> 中的每个值都 <strong>独一无二</strong></li>
39+
<li>题目数据保证 <code>nums</code> 在预先未知的某个下标上进行了旋转</li>
40+
<li><code>-10^4 <= target <= 10^4</code></li>
41+
</ul>
42+
43+
<p> </p>
44+
45+
<p><strong>进阶:</strong>你可以设计一个时间复杂度为 <code>O(log n)</code> 的解决方案吗?</p>
46+
<div><div>Related Topics</div><div><li>数组</li><li>二分查找</li></div></div><br><div><li>👍 1442</li><li>👎 0</li></div>
47+
*/
48+
49+
public class Q33SearchInRotatedSortedArray{
50+
public static void main(String[] args) {
51+
Solution solution = new Q33SearchInRotatedSortedArray().new Solution();
52+
}
53+
//leetcode submit region begin(Prohibit modification and deletion)
54+
class Solution {
55+
public int search(int[] nums, int target) {
56+
if (nums.length <= 0) return -1;
57+
if (nums.length == 1 && nums[0] == target) return 0;
58+
int s = 0;
59+
int e = nums.length - 1;
60+
while (s <= e) {
61+
int m = (s+e)/2;
62+
if (nums[m] == target) {
63+
return m;
64+
}
65+
if (nums[m] < nums[e]) {
66+
if (nums[m] < target && nums[e] >= target) {
67+
s = m + 1;
68+
} else {
69+
e = m - 1;
70+
}
71+
} else {
72+
if (nums[s] <= target && nums[m] > target) {
73+
e = m - 1;
74+
} else {
75+
s = m + 1;
76+
77+
}
78+
}
79+
}
80+
return -1;
81+
}
82+
}
83+
//leetcode submit region end(Prohibit modification and deletion)
84+
85+
}

0 commit comments

Comments
 (0)