FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add files via upload · feixiangcode/algorithm@a59f4b0 · GitHub

Commit a59f4b0

Browse files
authored
Add files via upload
1 parent bd3b0d5 commit a59f4b0

4 files changed

Lines changed: 125 additions & 93 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//执行用时 : 1 ms, 在Find Minimum in Rotated Sorted Array的Java提交中击败了92.41% 的用户
2+
//内存消耗 : 37.5 MB, 在Find Minimum in Rotated Sorted Array的Java提交中击败了7.22%的用户
3+
4+
class Solution {
5+
public int findMin(int[] nums) {
6+
int i = 1;
7+
int min = nums[0];
8+
for(;i<nums.length;i++){
9+
if(min>nums[i]) min=nums[i];
10+
}
11+
return min;
12+
}
13+
}
Lines changed: 38 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,57 @@
1-
2.0
2-
//先设置链表头
3-
//任一为空,直接连接另一条链表
4-
//执行用时 : 2 ms, 在Merge Two Sorted Lists的Java提交中击败了99.44% 的用户
5-
//内存消耗 : 35.3 MB, 在Merge Two Sorted Lists的Java提交中击败了90.56% 的用户
1+
1申请额外空间
2+
3+
4+
//执行用时 : 3 ms, 在Sort Array By Parity的Java提交中击败了99.59% 的用户
5+
//内存消耗 : 46.5 MB, 在Sort Array By Parity的Java提交中击败了68.41% 的用户
6+
67
class Solution {
7-
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
8-
ListNode head = null;
9-
ListNode p = null;
8+
public int[] sortArrayByParity(int[] A) {
109

11-
//if(l1 == null && l2 == null) return l1; 包含在下一句中
12-
if(l1 == null) return l2;
13-
if(l2 == null) return l1;
10+
int j = A.length;
11+
if (j<=1) return A;
12+
int[] B = new int[j];
1413

15-
if(l1.val <= l2.val){
16-
head = l1;
17-
l1 = l1.next;
18-
p = head;
19-
}
20-
else{
21-
head = l2;
22-
l2 = l2.next;
23-
p = head;
24-
}
14+
int x=0;
15+
int y=j-1;
2516

26-
while (l1 != null || l2 != null){
27-
ListNode t;
28-
if (l2 == null) {
29-
p.next = l1;
30-
break;
31-
}
32-
else if(l1 == null){
33-
p.next = l2;
34-
break;
17+
for(int i=0; i<j; i++){ //注意是i<j,而不是i<=j
18+
19+
if(A[i]%2 != 0){
20+
B[y] = A[i];
21+
y--;
3522
}
3623
else{
37-
if(l1.val <= l2.val){
38-
t = l1;
39-
l1 = l1.next;
40-
p.next = t;
41-
p = p.next;
42-
}
43-
else{
44-
t = l2;
45-
l2 = l2.next;
46-
p.next = t;
47-
p = p.next;
48-
}
49-
}
24+
B[x] = A[i];
25+
x++;
26+
}
5027
}
51-
return head;
28+
return B;
5229
}
5330
}
5431

5532

56-
1.0
57-
//先设置链表头
58-
//任一为空,后续不高效
59-
//执行用时 : 2 ms, 在Merge Two Sorted Lists的Java提交中击败了99.44% 的用户
60-
//内存消耗 : 34.8 MB, 在Merge Two Sorted Lists的Java提交中击败了95.63% 的用户
33+
34+
2不利用额外空间前后指针交换
6135
class Solution {
62-
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
63-
ListNode head = null;
64-
ListNode p = null;
65-
66-
//if(l1 == null && l2 == null) return l1; 包含在下一句中
67-
if(l1 == null) return l2;
68-
if(l2 == null) return l1;
36+
public int[] sortArrayByParity(int[] A) {
6937

70-
if(l1.val <= l2.val){
71-
head = l1;
72-
l1 = l1.next;
73-
p = head;
74-
}
75-
else{
76-
head = l2;
77-
l2 = l2.next;
78-
p = head;
79-
}
38+
if(A == null || A.length == 1) return A;
39+
int j = A.length - 1;
40+
int i = 0;
8041

81-
while (l1 != null || l2 != null){ //注意是 ||,不是&&
82-
ListNode t;
83-
if (l2 == null) {
84-
t = l1;
85-
l1 = l1.next;
86-
p.next = t;
87-
p = p.next;
88-
}
89-
else if(l1 == null){
90-
t = l2;
91-
l2 = l2.next;
92-
p.next = t;
93-
p = p.next;
42+
while(i<=j){ //注意不是i!=j,比如数组长度为2,会导致越界
43+
if(A[i]%2!=0 && A[j]%2==0){
44+
int temp = A[i];
45+
A[i] = A[j];
46+
A[j] = temp;
47+
i++;
48+
j--;
9449
}
9550
else{
96-
if(l1.val <= l2.val){
97-
t = l1;
98-
l1 = l1.next;
99-
p.next = t;
100-
p = p.next;
101-
}
102-
else{
103-
t = l2;
104-
l2 = l2.next;
105-
p.next = t;
106-
p = p.next;
107-
}
51+
if(A[i]%2==0) i++;
52+
if(A[j]%2!=0) j--;
10853
}
10954
}
110-
return head;
55+
return A;
11156
}
112-
}
57+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1遍历
2+
https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/comments/
3+
class Solution {
4+
public boolean search(int[] nums, int target) {
5+
for(int i = 0; i<nums.length; i++){
6+
if(target == nums[i]) return true;
7+
}
8+
return false;
9+
}
10+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
1建立新数组
2+
3+
//执行用时 : 6 ms, 在Sort Array By Parity II的Java提交中击败了83.69% 的用户
4+
//内存消耗 : 48.9 MB, 在Sort Array By Parity II的Java提交中击败了67.91% 的用户
5+
6+
class Solution {
7+
public int[] sortArrayByParityII(int[] A) {
8+
if(A == null || A.length == 1) return A;
9+
int k = A.length-1;
10+
int[] B = new int[A.length];
11+
12+
int i = 0;
13+
int j = 0;
14+
int x = 1;
15+
while(i<=k){
16+
if(A[i]%2 == 0){
17+
B[j] = A[i];
18+
j=j+2;
19+
}
20+
else{
21+
B[x] = A[i];
22+
x = x+2;
23+
}
24+
i++;
25+
}
26+
return B;
27+
}
28+
}
29+
30+
31+
32+
2
33+
//执行用时 : 7 ms, 在Sort Array By Parity II的Java提交中击败了77.23% 的用户
34+
//内存消耗 : 49.7 MB, 在Sort Array By Parity II的Java提交中击败了51.80% 的用户
35+
36+
//执行用时 : 5 ms, 在Sort Array By Parity II的Java提交中击败了93.39% 的用户
37+
//内存消耗 : 41.5 MB, 在Sort Array By Parity II的Java提交中击败了92.67% 的用户
38+
39+
//执行用时 : 7 ms, 在Sort Array By Parity II的Java提交中击败了77.23% 的用户
40+
//内存消耗 : 52.3 MB, 在Sort Array By Parity II的Java提交中击败了9.49% 的用户
41+
class Solution {
42+
public int[] sortArrayByParityII(int[] A) {
43+
if(A == null || A.length == 1) return A;
44+
int k = A.length-1;
45+
46+
int i = 0;
47+
int j = 1;
48+
49+
while(i<=k && j<=k){
50+
if(A[i]%2 != 0 && A[j]%2 == 0 ){
51+
int temp = A[i];
52+
A[i] = A[j];
53+
A[j]=temp;
54+
i = i+2;
55+
j = j+2;
56+
}
57+
else{
58+
if(A[i]%2==0) i = i+2;
59+
if(A[j]%2!=0) j = j+2;
60+
}
61+
}
62+
return A;
63+
}
64+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL