Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite0d5f1b

Browse files
author
applewjg
committed
update
1 parent2cae197 commite0d5f1b

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

‎RemoveDuplicatesfromSortedArray.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 12, 2014
4+
Problem: Remove Duplicates from Sorted Array
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/
7+
Notes:
8+
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
9+
Do not allocate extra space for another array, you must do this in place with constant memory.
10+
For example,
11+
Given input array A = [1,1,2],
12+
Your function should return length = 2, and A is now [1,2].
13+
14+
Solution: Update 7/16/2013: Let j start from 0 for better understanding.
15+
*/
16+
publicclassSolution {
17+
publicintremoveDuplicates(int[]A) {
18+
intN =A.length;
19+
intidx =0;
20+
for (inti =0;i <N; ++i) {
21+
if (i ==0 ||A[i] !=A[i -1]) {
22+
A[idx++] =A[i];
23+
}
24+
}
25+
returnidx;
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Author: Annie Kim, anniekim.pku@gmail.com
3+
Date: Dec 12, 2014 [Two pointers ('last' and 'lastlast').]
4+
Problem: Remove Duplicates from Sorted Array II
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
7+
Notes:
8+
Follow up for "Remove Duplicates":
9+
What if duplicates are allowed at most twice?
10+
For example,
11+
Given sorted array A = [1,1,1,2,2,3],
12+
Your function should return length = 5, and A is now [1,1,2,2,3].
13+
14+
Solution: ...
15+
*/
16+
publicclassSolution {
17+
publicintremoveDuplicates(int[]A) {
18+
intN =A.length;
19+
if (N <=2)returnN;
20+
intidx =2;
21+
for (inti =2;i <N; ++i) {
22+
if (A[i] !=A[idx-2])
23+
A[idx++] =A[i];
24+
}
25+
returnidx;
26+
}
27+
}

‎RemoveDuplicatesfromSortedList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public ListNode deleteDuplicates_1(ListNode head) {
3333
if(pre.val ==cur.val) {
3434
pre.next =cur.next;
3535
}else {
36-
pre =cur;
36+
pre =pre.next;
3737
}
3838
cur =cur.next;
3939
}

‎RemoveElement.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 12, 2014
4+
Problem: Remove Element
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/remove-element/
7+
Notes:
8+
Given an array and a value, remove all instances of that value in place and return the new length.
9+
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
10+
11+
Solution: Refactor: Update solution. Use two pointers.
12+
*/
13+
14+
publicclassSolution {
15+
publicintremoveElement(int[]A,intelem) {
16+
intN =A.length;
17+
intidx =0;
18+
for (inti =0;i <N; ++i) {
19+
if (A[i] !=elem) {
20+
A[idx++] =A[i];
21+
}
22+
}
23+
returnidx;
24+
}
25+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp