初始化
This commit is contained in:
16
src/cn/celess/leetcode/topinterview150/test26/Main.java
Normal file
16
src/cn/celess/leetcode/topinterview150/test26/Main.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package cn.celess.leetcode.topinterview150.test26;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
int[] nums = {0,0,1,1,1,2,2,3,3,4}; // 输入数组
|
||||
int[] expectedNums = {0,1,2,3,4}; // 长度正确的期望答案
|
||||
|
||||
int k = new Solution().removeDuplicates(nums); // 调用
|
||||
|
||||
assert k == expectedNums.length;
|
||||
for (int i = 0; i < k; i++) {
|
||||
System.out.println(nums[i]);
|
||||
assert nums[i] == expectedNums[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
35
src/cn/celess/leetcode/topinterview150/test26/Solution.java
Normal file
35
src/cn/celess/leetcode/topinterview150/test26/Solution.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package cn.celess.leetcode.topinterview150.test26;
|
||||
|
||||
class Solution {
|
||||
public int removeDuplicates(int[] nums) {
|
||||
if(nums.length == 1){
|
||||
return 1;
|
||||
}
|
||||
|
||||
int left = 0;
|
||||
int right = 1;
|
||||
|
||||
int[] arr = new int[nums.length];
|
||||
|
||||
arr[0] = nums[0];
|
||||
int len = 1;
|
||||
while(right < nums.length){
|
||||
|
||||
if(nums[left] == nums[right]){
|
||||
left++;
|
||||
right++;
|
||||
continue;
|
||||
}
|
||||
arr[len++] = nums[right];
|
||||
left++;
|
||||
right++;
|
||||
}
|
||||
|
||||
for(int j = 0; j < len; j++){
|
||||
nums[j] = arr[j];
|
||||
}
|
||||
|
||||
|
||||
return len;
|
||||
}
|
||||
}
|
||||
38
src/cn/celess/leetcode/topinterview150/test27/Main.java
Normal file
38
src/cn/celess/leetcode/topinterview150/test27/Main.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package cn.celess.leetcode.topinterview150.test27;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
int[] nums = new int[]{0, 1, 2, 2, 3, 0, 4, 2}; // 输入数组
|
||||
int val = 2; // 要移除的值
|
||||
int[] expectedNums = {0, 1, 3, 0, 4}; // 长度正确的预期答案。
|
||||
// 它以不等于 val 的值排序。
|
||||
|
||||
int k = new Solution().removeElement(nums, val); // 调用你的实现
|
||||
|
||||
assert k == expectedNums.length;
|
||||
int actualLength = k;
|
||||
Arrays.sort(nums, 0, k); // 排序 nums 的前 k 个元素
|
||||
for (int i = 0; i < actualLength; i++) {
|
||||
System.out.println(nums[i]);
|
||||
assert nums[i] == expectedNums[i];
|
||||
}
|
||||
|
||||
//
|
||||
// int[] nums = new int[]{1}; // 输入数组
|
||||
// int val = 1; // 要移除的值
|
||||
// int[] expectedNums = {}; // 长度正确的预期答案。
|
||||
// // 它以不等于 val 的值排序。
|
||||
//
|
||||
// int k = new Solution().removeElement(nums, val); // 调用你的实现
|
||||
//
|
||||
// assert k == expectedNums.length;
|
||||
// int actualLength = k;
|
||||
// Arrays.sort(nums, 0, k); // 排序 nums 的前 k 个元素
|
||||
// for (int i = 0; i < actualLength; i++) {
|
||||
// System.out.println(nums[i]);
|
||||
// assert nums[i] == expectedNums[i];
|
||||
// }
|
||||
}
|
||||
}
|
||||
26
src/cn/celess/leetcode/topinterview150/test27/Solution.java
Normal file
26
src/cn/celess/leetcode/topinterview150/test27/Solution.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package cn.celess.leetcode.topinterview150.test27;
|
||||
|
||||
class Solution {
|
||||
public int removeElement(int[] nums, int val) {
|
||||
int head = 0;
|
||||
int tail = nums.length - 1;
|
||||
|
||||
while (tail >= 0 && head <= tail) {
|
||||
|
||||
if (nums[tail] == val) {
|
||||
tail--;
|
||||
continue;
|
||||
}
|
||||
if (nums[head] == val) {
|
||||
int tmp = nums[head];
|
||||
nums[head] = nums[tail];
|
||||
nums[tail] = tmp;
|
||||
head++;
|
||||
tail--;
|
||||
continue;
|
||||
}
|
||||
head++;
|
||||
}
|
||||
return head ;
|
||||
}
|
||||
}
|
||||
30
src/cn/celess/leetcode/topinterview150/test80/Solution.java
Normal file
30
src/cn/celess/leetcode/topinterview150/test80/Solution.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package cn.celess.leetcode.topinterview150.test80;
|
||||
|
||||
public class Solution {
|
||||
|
||||
public int removeDuplicates(int[] nums) {
|
||||
int slow = 2;
|
||||
int fast = 2;
|
||||
|
||||
while (fast < nums.length) {
|
||||
if (nums[fast] != nums[slow - 2]) {
|
||||
nums[slow++] = nums[fast];
|
||||
}
|
||||
fast++;
|
||||
}
|
||||
return slow;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Main {
|
||||
public static void main(String[] args) {
|
||||
int[] nums = {0, 0};
|
||||
int len = new Solution().removeDuplicates(nums);
|
||||
for (int i = 0; i < len; i++) {
|
||||
System.out.println(nums[i]);
|
||||
}
|
||||
System.out.println("=====");
|
||||
System.out.println(len);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user