ARTS第七周

Algorithm

Leetcode 26. Remove Duplicates from Sorted Array

Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.

Example 1:
Given 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 returned length.

Example 2:
Given nums = [0,0,1,1,1,2,2,3,3,4],
Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
It doesn’t matter what values are set beyond the returned length.

这道题比较简单,题意是从一个有序数组中找出所有的不重复的数。要求空间复杂度是O(1)

用一个变量记录不重复的数的个数,这个变量用于找到下一个重复数的位置。
用一个变量记录前一个数的值,这个变量用于判断和当前数是否相同。

class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums == null) return 0;
        int len = nums.length;
        if(len <= 1) return len;
        int sum=1;
        int x=nums[0];
        for(int i=1;i<len;i++) {
            if(x != nums[i]){
                sum++;
                nums[sum-1] = nums[i];
            }
            x=nums[i];
        }
        return sum;
    }
}

Review

做了一道双端队列的模拟当作Review吧

/**
 * 要实现一个双端队列的数据结构
 *      1. 结点
 *          1. prev指向前驱结点
 *          2. next指向后继结点
 *          3. val存放元素值
 *      2. 头指针
 *      3. 尾指针
 *      4. 队列的容量
 *      5. 队列的元素个数
 */
public class MyCircularDeque {

    private static class Node {
        int val;
        Node prev;
        Node next;

        Node(int val, Node prev, Node next) {
            this.val = val;
            this.prev = prev;
            this.next = next;
        }
    }

    private Node head;
    private Node tail;
    private int capacity;
    private int count;


    /** Initialize your data structure here. Set the size of the deque to be k. */
    /**
     * 初始化一个指定大小k的双端队列
     * @param k 大小
     */
    public MyCircularDeque(int k) {
        this.capacity = k;
        this.head = new Node(-1, null, null);
        this.tail = new Node(-1, head, null);
        this.head.next = tail;
    }

    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    /**
     * 从队首插入结点,如果队列已经满了,则返回false
     * @param value 结点的值
     * @return 返回是否插入成功
     */
    public boolean insertFront(int value) {
        // 判断当前队列是否已经满了
        if (isFull()) return false;

        //新建结点,并更新节点间的关系
        Node node = new Node(value, head, head.next);
        head.next.prev = node;
        head.next = node;
        count++;
        return true;
    }

    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    /** 同boolean insertFront(int) */
    public boolean insertLast(int value) {
        if (isFull()) return false;

        Node node = new Node(value, tail.prev, tail);
        tail.prev.next = node;
        tail.prev = node;
        count++;
        return true;
    }

    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    /**
     * 从队首删除一个元素,如果队列为空,返回false
     * @return 是否删除成功
     */
    public boolean deleteFront() {
        // 当队列没有元素的时候
        if (isEmpty()) return false;

        Node node = head.next;
        node.next.prev = head;
        head.next = node.next;
        node.next = null;
        node.prev = null;
        count--;
        return true;
    }

    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    /** 同boolean deleteFront() */
    public boolean deleteLast() {
        if (isEmpty()) return false;

        Node node = tail.prev;
        node.prev.next = tail;
        tail.prev = node.prev;
        node.prev = null;
        node.next = null;
        count--;
        return true;
    }

    /** Get the front item from the deque. */
    /**
     * 获取队首元素
     * @return 队首元素值
     */
    public int getFront() {
        return head.next.val;
    }

    /** Get the last item from the deque. */
    /** 同int getFront() */
    public int getRear() {
        return tail.prev.val;
    }

    /** Checks whether the circular deque is empty or not. */
    /**
     * 判断队列是否为空
     * @return 为空 返回true
     */
    public boolean isEmpty() {
        return this.count == 0;
    }

    /** Checks whether the circular deque is full or not. */
    /** 同boolean isEmpty() */
    public boolean isFull() {
        return this.count == this.capacity;
    }
}

/**
 * MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3
 * circularDeque.insertLast(1);			// return true
 * circularDeque.insertLast(2);			// return true
 * circularDeque.insertFront(3);			// return true
 * circularDeque.insertFront(4);			// return false, the queue is full
 * circularDeque.getRear();  			// return 2
 * circularDeque.isFull();				// return true
 * circularDeque.deleteLast();			// return true
 * circularDeque.insertFront(4);			// return true
 * circularDeque.getFront();			// return 4
 *
 *
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * boolean param_1 = obj.insertFront(value);
 * boolean param_2 = obj.insertLast(value);
 * boolean param_3 = obj.deleteFront();
 * boolean param_4 = obj.deleteLast();
 * int param_5 = obj.getFront();
 * int param_6 = obj.getRear();
 * boolean param_7 = obj.isEmpty();
 * boolean param_8 = obj.isFull();
 */

Tip

git的基本概念学习

Share

Java基础的小细节:
Java对象实例化顺序的过程
父类对象能否强转为子类对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值