• Leetcode 题目整理-6 Swap Nodes in Pairs & Remove Duplicates from Sorted Array


    24. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head.

    For example,
    Given 1->2->3->4, you should return the list as 2->1->4->3.

    Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

    注 :给定一个链表,交换每两个节点,返回首指针。要在常数空间内完成算法不能修改链表值,只能改变节点之间的链接。

    解:竟然可以输入奇数个,无语。

    if (head==0)
        {return head;}
        ListNode *backer, *fronter, *temp;
        //游标定位
        backer = head;
        if (head->next == NULL){ return head; }//针对只输入一个的情况
        fronter = head->next;
        temp = fronter->next;//可以有,也可以是NULL
        //进行交换
        head = fronter;
        fronter->next = backer;
        if (temp != NULL && temp->next!=NULL)
        {
            backer->next = temp->next;
        }
        else if (temp != NULL && temp->next == NULL)
        {
            backer->next = temp;
            return head;
        }
        else { backer->next = NULL; return head; }
        while (temp != NULL) 
        {
            //移动游标
            backer = temp;
    
            fronter = temp->next;
            temp = fronter->next;
            //进行交换
            fronter->next = backer;
            if (temp != NULL && temp->next != NULL)
            {
                backer->next = temp->next;
            }
            else if (temp != NULL && temp->next == NULL)
            {
                backer->next = temp;
                return head;
            }
            else { backer->next = NULL; return head; }
        }
        return head;

    26. Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

    Do not allocate extra space for another array, you must do this in place with constant memory.

    For example,
    Given input array nums = [1,1,2],

    Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

    注:对于一个排好序的数组,移除其中重复的元素,使得每个元素只出现一次,然后返回新的长度。不能使用额外的空间。这个数组一定是越变越小的,所以只要恰当的使用指针移动数组中的数据应该可以不占用额外的空间。

    解:这里新学会了v.erase(iter)的使用

    if (nums.size() == 0)
            return 0;
        vector<int>::iterator nums_i = nums.begin(), nums_j = nums.begin() + 1;
        while (nums_i != nums.end() && nums_j != nums.end())
        {
            if (*nums_i == *nums_j)
            {
                nums_j=nums.erase(nums_j);//删除后者j并返回j后一个元素的指针,如果没有就返回end()指针
            }
            else{
                nums_i++; nums_j++;
            }
        } 
        return nums.size();
  • 相关阅读:
    linux driver ------ 交叉工具链(cross toolchain)
    Qt ------ 截图、获取鼠标指定的RGB值
    Qt ------ QWidget 自定义子类使用信号与槽(Q_OBJECT)后 stylesheet 失效
    Qt error ------ incomplete type 'QApplication' used in nested name specifier
    Qt ------ Q_UNUSED
    SpringCloud 组件Eureka参数配置项详解
    过滤器(Filter)与拦截器(Interceptor)的区别
    事务隔离级别
    事务四大特性
    get与post的区别
  • 原文地址:https://www.cnblogs.com/simayuhe/p/6185907.html
Copyright © 2020-2023  润新知