Skip to content

Commit 7be6014

Browse files
authored
039 to re-submit week1 homework with correct naming standard
1 parent 8272982 commit 7be6014

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.paula.algorithmsAndDataStructure.leetCode_83;
2+
3+
public class LeetCode_083_039 {
4+
public ListNode deleteDuplicates(ListNode head) {
5+
if(head == null) return null;
6+
7+
ListNode p = head;
8+
ListNode q = null;
9+
while(p.next != null) {
10+
if(p.val == p.next.val) {
11+
if(q == null || (q != null && q.val != p.val)) {
12+
q = p;
13+
}
14+
}else {
15+
if(q != null && q.val == p.val) {
16+
q.next = p.next;
17+
}
18+
}
19+
p = p.next;
20+
}
21+
22+
if(q != null && p.val == q.val && p.next == null) {
23+
q.next = null;
24+
}
25+
return head;
26+
}
27+
28+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.paula.algorithmsAndDataStructure;
2+
3+
public class LeetCode_922_039 {
4+
public int[] sortArrayByParityII(int[] A) {
5+
int len = A.length;
6+
int i=0; //偶数下标
7+
int j=i+1; //奇数下标
8+
Boolean evenIndexOddValue = false;
9+
Boolean oddIndexEvenValue = false;
10+
11+
while (i < len-1 && j < len) {
12+
evenIndexOddValue = isOdd(A[i]);
13+
oddIndexEvenValue= isEven(A[j]);
14+
15+
if(!evenIndexOddValue) i +=2;
16+
if(!oddIndexEvenValue) j +=2;
17+
if(evenIndexOddValue && oddIndexEvenValue) {
18+
int tempVal = A[i];
19+
A[i] = A[j];
20+
A[j] = tempVal;
21+
22+
i += 2;
23+
j += 2;
24+
25+
}
26+
}
27+
28+
29+
return A;
30+
}
31+
32+
public boolean isEven(int value) {
33+
34+
return value%2 == 0 ? true : false;
35+
}
36+
37+
public boolean isOdd(int value) {
38+
39+
return value%2 == 1 ? true : false;
40+
}
41+
42+
}

0 commit comments

Comments
 (0)