Skip to content

Commit fd3d1a6

Browse files
author
changhongyuan
committed
27题
1 parent b6bbc45 commit fd3d1a6

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

.idea/leetcode/editor.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
2+
//
3+
// 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
4+
//
5+
// 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
6+
//
7+
//
8+
//
9+
// 说明:
10+
//
11+
// 为什么返回数值是整数,但输出的答案是数组呢?
12+
//
13+
// 请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
14+
//
15+
// 你可以想象内部操作如下:
16+
//
17+
//
18+
//// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
19+
//int len = removeElement(nums, val);
20+
//
21+
//// 在函数里修改输入数组对于调用者是可见的。
22+
//// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
23+
//for (int i = 0; i < len; i++) {
24+
// print(nums[i]);
25+
//}
26+
//
27+
//
28+
//
29+
//
30+
// 示例 1:
31+
//
32+
//
33+
//输入:nums = [3,2,2,3], val = 3
34+
//输出:2, nums = [2,2]
35+
//解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而
36+
//nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。
37+
//
38+
//
39+
// 示例 2:
40+
//
41+
//
42+
//输入:nums = [0,1,2,2,3,0,4,2], val = 2
43+
//输出:5, nums = [0,1,4,0,3]
44+
//解释:函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面
45+
//的元素。
46+
//
47+
//
48+
//
49+
//
50+
// 提示:
51+
//
52+
//
53+
// 0 <= nums.length <= 100
54+
// 0 <= nums[i] <= 50
55+
// 0 <= val <= 100
56+
//
57+
// Related Topics 数组 双指针 👍 1362 👎 0
58+
59+
package com.changhongyuan.leetcode.editor.cn;
60+
class RemoveElement{
61+
//leetcode submit region begin(Prohibit modification and deletion)
62+
class Solution {
63+
public int removeElement(int[] nums, int val) {
64+
int result = 0;
65+
if (null != nums && 0 < nums.length) {
66+
result = nums.length;
67+
for (int i = 0; i < nums.length; i++) {
68+
if (nums[i] == val) {
69+
nums[i] = -1;
70+
result--;
71+
}
72+
}
73+
for (int i = 0; i < nums.length; i++) {
74+
// System.out.println(i);
75+
// for (int num : nums) {
76+
// System.out.print(num + " ");
77+
// }
78+
// System.out.println();
79+
if (nums[i] != -1) continue;
80+
for (int j = nums.length - 1; j > i; j--) {
81+
if (nums[j] != -1) {
82+
int temp = nums[j];
83+
nums[j] = nums[j - 1];
84+
nums[j - 1] = temp;
85+
}
86+
}
87+
}
88+
}
89+
return result;
90+
}
91+
}
92+
//leetcode submit region end(Prohibit modification and deletion)
93+
94+
public static void main(String[] args) {
95+
Solution solution = new RemoveElement().new Solution();
96+
int[] nums = new int[]{0,1,2,2,3,0,4,2};
97+
System.out.println(solution.removeElement(nums, 2));
98+
for (int num : nums) {
99+
System.out.print(num + " ");
100+
}
101+
}
102+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<p>给你一个数组 <code>nums</code><em> </em>和一个值 <code>val</code>,你需要 <strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地</a></strong> 移除所有数值等于 <code>val</code><em> </em>的元素,并返回移除后数组的新长度。</p>
2+
3+
<p>不要使用额外的数组空间,你必须仅使用 <code>O(1)</code> 额外空间并 <strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地 </a>修改输入数组</strong>。</p>
4+
5+
<p>元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。</p>
6+
7+
<p> </p>
8+
9+
<p><strong>说明:</strong></p>
10+
11+
<p>为什么返回数值是整数,但输出的答案是数组呢?</p>
12+
13+
<p>请注意,输入数组是以<strong>「引用」</strong>方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p>
14+
15+
<p>你可以想象内部操作如下:</p>
16+
17+
<pre>
18+
// <strong>nums</strong> 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
19+
int len = removeElement(nums, val);
20+
21+
// 在函数里修改输入数组对于调用者是可见的。
22+
// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。
23+
for (int i = 0; i < len; i++) {
24+
    print(nums[i]);
25+
}
26+
</pre>
27+
28+
<p> </p>
29+
30+
<p><strong>示例 1:</strong></p>
31+
32+
<pre>
33+
<strong>输入:</strong>nums = [3,2,2,3], val = 3
34+
<strong>输出:</strong>2, nums = [2,2]
35+
<strong>解释:</strong>函数应该返回新的长度 <strong>2</strong>, 并且 nums<em> </em>中的前两个元素均为 <strong>2</strong>。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。
36+
</pre>
37+
38+
<p><strong>示例 2:</strong></p>
39+
40+
<pre>
41+
<strong>输入:</strong>nums = [0,1,2,2,3,0,4,2], val = 2
42+
<strong>输出:</strong>5, nums = [0,1,4,0,3]
43+
<strong>解释:</strong>函数应该返回新的长度 <strong><code>5</code></strong>, 并且 nums 中的前五个元素为 <strong><code>0</code></strong>, <strong><code>1</code></strong>, <strong><code>3</code></strong>, <strong><code>0</code></strong>, <strong>4</strong>。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。
44+
</pre>
45+
46+
<p> </p>
47+
48+
<p><strong>提示:</strong></p>
49+
50+
<ul>
51+
<li><code>0 <= nums.length <= 100</code></li>
52+
<li><code>0 <= nums[i] <= 50</code></li>
53+
<li><code>0 <= val <= 100</code></li>
54+
</ul>
55+
<div><div>Related Topics</div><div><li>数组</li><li>双指针</li></div></div><br><div><li>👍 1362</li><li>👎 0</li></div>

0 commit comments

Comments
 (0)