1. 删除有序数组中的重复项(简单)
地址: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
2022/02/26-7/24
做题反思:
class Solution {
public int removeDuplicates(int[] nums) {
int fast = 0, slow = 0;
while (fast < nums.length) {
if (nums[fast] != nums[slow]) {
slow++;
nums[slow] = nums[fast];
}
fast++;
}
return slow + 1;
}
}
2. 删除排序链表中的重复元素(简单)
地址: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
2022/02/26-7/24
做题反思:
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return null;
}
ListNode fast = head, slow = head;
while (fast != null) {
if (fast.val != slow.val) {
slow.next = fast;
slow = slow.next;
}
fast = fast.next;
}
slow.next = null;
return head;
}
}
3. 移除元素(简单)
地址: https://leetcode-cn.com/problems/remove-element/
2022/02/26-7/24
做题反思:
class Solution {
public int removeElement(int[] nums, int val) {
int fast = 0, slow = 0;
while (fast < nums.length) {
if (nums[fast] != val) {
nums[slow] = nums[fast];
slow++;
}
fast++;
}
return slow;
}
}
4. 移动零(简单)
地址: https://leetcode-cn.com/problems/move-zeroes/
2022/02/26-7/24
做题反思:
class Solution {
public void moveZeroes(int[] nums) {
int slow = 0, fast = 0;
while (fast < nums.length) {
if (nums[fast] != 0) {
int temp = nums[slow];
nums[slow] = nums[fast];
nums[fast] = temp;
slow++;
}
fast++;
}
}
}