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

Commit59b154d

Browse files
remove element in progress
1 parentefb79ed commit59b154d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

‎EASY/src/easy/RemoveElement.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
packageeasy;
2+
3+
importutils.CommonUtils;
4+
5+
/**27. Remove Element
6+
7+
Total Accepted: 135216
8+
Total Submissions: 384018
9+
Difficulty: Easy
10+
11+
Given an array and a value, remove all instances of that value in place and return the new length.
12+
13+
Do not allocate extra space for another array, you must do this in place with constant memory.
14+
15+
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
16+
17+
Example:
18+
Given input array nums = [3,2,2,3], val = 3
19+
20+
Your function should return length = 2, with the first two elements of nums being 2.*/
21+
publicclassRemoveElement {
22+
publicintremoveElement(int[]nums,intval) {
23+
for(inti =0;i <nums.length;i++){
24+
intstart =i;
25+
while(i <nums.length &&nums[i] ==val){
26+
i++;
27+
}
28+
if(i ==nums.length)i--;
29+
nums[start] =nums[i];
30+
}
31+
CommonUtils.printArray(nums);
32+
return0;
33+
}
34+
35+
publicstaticvoidmain(String...strings){
36+
RemoveElementtest =newRemoveElement();
37+
int[]nums =newint[]{3,2,2,3};
38+
intval =3;
39+
test.removeElement(nums,val);
40+
}
41+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp