首页 > 其他分享 >如何去除有序数组中的重复元素

如何去除有序数组中的重复元素

时间:2024-08-21 13:38:27浏览次数:7  
标签:Node head slow 数组 fast 有序 去除 public linkedList

题目要求:输入一个有序的数组,你需要原地删除重复的元素,使得每个元素只能出现一次,返回去重后新数组的长度

题目分析:原地删除,即不可以创建新的辅助数组,那么需要在原数组上修改解决。

这种一般采用双指针技巧

即:如果是普通数组,使用两个指针slow和fast,fast负责网后遍历,如果发现数组元素不同,则把slow下标值对应的值替换为fast下标值对应的值,则在原数组上完成保存不重复值的操作,如果元素重复,则fast前进,而slow不动,则最终fast遍历到末尾时,slow下标值对应的所有元素已经是不重复的有序数组元素了

/**
 * 有序数组或者链表,通过两个指针方式去重数据,统计去重后数组个数
 */
public class ArraysTest {

    public static void main(String[] args) {
        int [] arrays = new int[7];
        arrays[0] = 0;
        arrays[1] = 1;
        arrays[2] = 1;
        arrays[3] = 2;
        arrays[4] = 3;
        arrays[5] = 3;
        arrays[6] = 4;
        System.out.println(removeDuplicates(arrays));
    }

    public static int removeDuplicates(int[] nums) {
        int slow = 0;
        int fast = 1;
        int length = nums.length;
        while (fast < length) {
            if(nums[slow] != nums[fast]){
                slow++;
                nums[slow] = nums[fast];
            }
            fast++;
        }
        return slow+1;
    }
}

  扩展问题,如果是有序链表呢,方式一样处理,代码如下

/**
 * 有序数组或者链表,通过两个指针方式去重数据,统计去重后数组个数
 */
public class LinkedArraysTest {


    public static void main(String[] args) {

        LinkedList linkedList = new LinkedList();
        linkedList.add(new Node(0));
        linkedList.add(new Node(1));
        linkedList.add(new Node(1));
        linkedList.add(new Node(2));
        linkedList.add(new Node(3));
        linkedList.add(new Node(4));
        linkedList.add(new Node(4));
        Node head = removeDulplicated(linkedList);
        int count = 0;
        while (head !=null){
            count++;
            head = head.next;
        }
        System.out.println(count);
    }

    public static Node removeDulplicated(LinkedList linkedList) {

        Node slow = linkedList.head;
        Node fast = linkedList.head.next;
        while (null != fast){
            if(slow.data != fast.data){
                slow.next = fast;
                slow = slow.next;
            }
            fast = fast.next;
        }
        slow.next =null;
        return linkedList.head;
    }

    public static class Node{
        int data;
        Node next;

        public Node(int data) {
            this.data = data;
        }

    }

    public static class LinkedList{
        Node head;

        public void add(Node node){
            if(null == head){
                head = node;
            }else{
                Node current = head;
                while (current.next != null){
                    current = current.next;
                }
                current.next = node;
            }
        }
    }

}

  

标签:Node,head,slow,数组,fast,有序,去除,public,linkedList
From: https://www.cnblogs.com/yangh2016/p/18371408

相关文章

  • C++类模板案例-数组类封装
    #include<iostream>usingnamespacestd;template<classT>classMyArray{public: MyArray(intcapacity) { this->m_Capacity=capacity; this->m_Size=0; this->pAddress=newT[this->m_Capacity]; } ~MyArray() { if(th......
  • 编写类A01,定义方法max,实现求某个double数组的最大值,并返回
    1publicclassHomework01{23//编写一个main方法4publicstaticvoidmain(String[]args){5A01a01=newA01();6double[]arr={1,1.4,-1.3,89.8,123.8,66};//;{};7Doubleres=a01.max(arr);8if......
  • 编写类A02,定义方法find,实现查找某字符串是否子啊字符数组中,并返回索引,如果找不到,返回-
    1publicclassHomework02{23//编写一个main方法4publicstaticvoidmain(String[]args){56String[]strs={"jack","tom","mary","milan"};7A02a02=newA02();8intin......
  • php多维数组排序 array_multisort
    参考文章:https://www.cnblogs.com/ivy-zheng/p/12557645.htmlarray_multisort — 对多个数组或多维数组进行排序array_multisort() 可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序。关联(string)键名保持不变,但数字键名会被重新索引返回值成功时返......
  • 数组
    数组​定义:数组是在内存中存储相同数据类型的连续的空间。概念:​数组元素:数组会在内存中开辟出一块连续固定大小的空间,每个空间相当于之前的一个变量​数组下标:数组下标是用于访问数组中特定元素的一个整数索引,从0开始​数组名代表的是连续空间的首地址,通......
  • c语言基础------数组指针
    在C语言中,数组指针是一种特殊的指针类型,它指向一个数组。具体来说,数组指针是指向数组首元素的指针,但它与普通指针有所不同,因为它包含了数组的大小信息。下面是关于数组指针的一些基本概念和用法:定义数组指针数组指针可以通过以下方式定义:int(*arrayPtr)[10];//arra......
  • 数据结构-队列 c语言使用链表和数组分别实现
    队列定义队列(queue)是一种遵循先入后到规则的线性数据结构,将队列头部称为“队首”,尾部称为“队尾”,把元素加入队尾称为“入队”,删除队首元素称为“出队”。队列实现基于链表的实现将链表的头节点和尾结点分别视为“队首”和“队尾”,规定队尾仅可添加节点,队首仅可删除节点。......
  • 【Leetcode 1370 】 数组序号转换—— 桶计数
    给你一个字符串 s ,请你根据下面的算法重新构造字符串:从 s 中选出 最小 的字符,将它 接在 结果字符串的后面。从 s 剩余字符中选出 最小 的字符,且该字符比上一个添加的字符大,将它 接在 结果字符串后面。重复步骤2,直到你没法从 s 中选择字符。从 s 中选出 ......
  • 【Leetcode 1365 】 有多少小于当前数字的数字 —— 数组模拟哈希表(就没写过这么详细
    给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目。换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j!=i 且 nums[j]<nums[i] 。以数组形式返回答案。示例1:输入:nums=[8,1,2,2,3]输出:[4,0,1,1,3]解......
  • day6 哈希表part01: 242.有效的字母异位词|349. 两个数组的交集|202. 快乐数|1. 两数
    242.有效的字母异位词 classSolution{publicbooleanisAnagram(Strings,Stringt){int[]record=newint[26];//a=97.soa-a=0,b-a=1.直接使用减法,不用记acii码值。//遍历第一个string++,遍历第二个string--.数组里的数字......