leetcode-18-remove

283. Move Zeroes

解题思路:

从nums[0]开始,如果是零就和它后面的第一个非零数交换,不是零就下一位。不贴代码了,比较简单。



27. Remove Element

解题思路:

这道题对结果的顺序无要求,所以显然可以想到交换。我的思路是设置两个指针i和j,j先从后往前扫一下,找到第一个不是val的位置。

i从前向后,找到第一个是val的位置,nums[i]和nums[j]交换。最后,nums[j]后面都是val,所以返回j+1即是想要的长度。需要注意

的是,交换完之后,j向前走,前面仍然可能是val,所以需要加一个循环,找到前方第一个不是val的位置作为j的新位置。

int removeElement(vector<int>& nums, int val) {
        if (nums.size() == 0)
            return 0;
        if (nums.size() == 1) {
            if (nums[0] == val)
                return 0;
            else
                return 1;
        }
        int i,j;
        j = nums.size() - 1;
        while (nums[j] == val) {
            j --;
            if (j < 0)
                return 0;
        }
        for (i = 0; i <= j; i++) {
            if (nums[i] == val) {
                nums[i] = nums[j];
                nums[j] = val;
                j --;
                // nums[j] may be val. then move forward.
                while (nums[j] == val)
                    j--;
                continue;
            }

        }
        return j+1;
    }


26. Remove Duplicates from Sorted Array

解题思路:

考虑到nums是排好序的,所以先找到第一个与nums[i]不同的位置j,nums[i+1]=nums[j],然后i继续向前扫。如果nums[i]和nums[j]不相等,

那就继续向前扫咯。以1112222233为例。当然,如果有1111这种情况,在找j的时候,j会等于nums.size()这时候,终止就可以了。

int removeDuplicates(vector<int>& nums) {
        if (nums.size() < 2)
            return nums.size();
        int i = 0;
        int j = i + 1;
        while(i < nums.size()-1) {
            if (nums[j] == nums[i]) {
                while (nums[j] == nums[i]) {
                    j++;
                    if (j == nums.size())
                        return i+1;
                }
                nums[i+1] = nums[j];
                i++;
                continue;
            }
            else {
                i++;
                j++;
            }
        }
        return i+1;
    }  


203. Remove Linked List Elements

解题思路:

这道题WA好几次。。没考虑到连续几个数都是val的情况。我的思路是先处理头部是val的情况,然后处理val在second

的位置的情况。不过写的时候一定要考虑指针为空的情况,特别是用到->next赋值的时候。

ListNode* removeElements(ListNode* head, int val) {
        if (head == NULL)
            return head;
        // if val is at head
        while (head != NULL && head->val == val) {
            head = head->next;
        }
        if (head == NULL)
            return head;
        ListNode* first = head;
        ListNode* second = head->next;
        while (second != NULL) {
            if (second->val == val) {
                // judge if second is NULL. serial val
                while(second != NULL && second->val == val) {
                    second = second->next;
                }
                if (second == NULL) {
                    first->next = NULL;
                }
                else {
                    first->next = second;
                    first = second;
                    if (first != NULL)
                        second = first->next;
                    else
                        second = NULL;
                }
                continue;
            }
            // not val
            first = second;
            second = second->next;
        }
        return head;
    }
时间: 2024-08-25 23:33:40

leetcode-18-remove的相关文章

LeetCode OJ - Remove Nth Node From End of List

题目: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.Note:Give

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

LeetCode:Remove Nth Node From End of List

1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 3.题目内容 英文:Given a linked list, remove the nth node from the end of list and return its head. 中文:给出一个链表,删去链表中的倒数第n项,返回链表首节点 例如:

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

[LeetCode 题解]: Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 题意:给定一个已排序的链表,删除其中重复的元素,保证每个元素有且仅有一个. 使用两个指针,分别指向前一个元素,和当前元素,

LeetCode:Remove Element

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. Solution: class Solution { public: int removeElement(int

[LeetCode] 027. Remove Element (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 027. Remove Element (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-element/ 代码(github):https://github.com/illuz/leetcode 题意: 删除一个数组里值为 elem 的所有数. 分析: 用

[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 026. Remove Duplicates from Sorted Array (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 代码(github):https://github.com/ill

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] &lt;c++&gt;

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {

[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby 描述 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1