front的第二种更新表达式

front的第二种更新表达式

波波老师更新front的方法:front = front == 0 ? data.length - 1 : front - 1;

我更新front的方法:front = (front + data.length - 1) % data.length;

请问我这种方法可以么?我自己验证是对的

正在回答

登陆购买课程后可参与讨论,去登陆

4回答

可以的。本质和这个问答里对 size 的更新是一个意思:)

https://class.imooc.com/course/qadetail/251834 


继续加油!:)

假蛙工程师 2021-10-29 10:11:53

如果front - 1 >0 0, front % data.length == front ,front可以指向正确的位置。

如果front == 0, front - 1 = -1 , -1 % data.length = -1, -1不是一个合法的索引。因此需要把front转换到正确的位置。


只有求-1的同余数就可以了-1 的同余数为 -1 + data.length

front + data.length -1的意思就是找出 -1的同余数。


如果使用的是python语言,就不存在这个问题因为python的%是取模运算,-1取模data.length相当于data.length - 1可以得到正确的结果,java %是取余运算-1取余data.length 结果为-1 


参考:https://blog.csdn.net/gao_zhennan/article/details/72312696


假蛙工程师 2021-10-29 10:11:13

如果front - 1 >0 0, front % data.length == front ,front可以指向正确的位置。

如果front == 0, front - 1 = -1 , -1 % data.length = -1, -1不是一个合法的索引。因此需要把front转换到正确的位置。


只有求-1的同余数就可以了-1 的同余数为 -1 + data.length

front + data.length -1的意思就是找出 -1的同余数。


如果使用的是python语言,就不存在这个问题因为python的%是取模运算,-1取模data.length相当于data.length - 1可以得到正确的结果,java %是取余运算-1取余data.length 结果为-1 


参考:https://blog.csdn.net/gao_zhennan/article/details/72312696

假蛙工程师 2021-10-28 20:46:17

测试下回复

  • 测试下回复

    2021-10-28 20:46:31
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    /**
     * 双端队列
     * @author gzn
     * @version 1.0
     */
     
    public class Deque<E>{
     
        private E[] data;
        private int front, tail;
        private int size;
     
        public Deque(int capacity){
            data = (E[])new Object[capacity];
            front = tail = 0;
            size = 0;
        }
     
        public int getCapacity(){
            return data.length;
        }
        public int getSize(){
            return size;
        }
        public boolean isEmpty(){
            return size == 0;
        };
        private void resize(int newCapacity){
            E[] newData = (E[])new Object[newCapacity];
            for(int i = 0; i < size; i ++){
                newData[i] = data[(i + front) % data.length];
            }
            data = newData;
            front = 0;
            tail = size - 1;
        }
     
        public void addFront(E e){
            if(size == getCapacity()){
                resize(getCapacity() * 2);
            }
     
            if (data[front] != null){
             //   front = front - 1 >= 0 ? front - 1 : front - 1 + data.length;
                front = front == 0 ? data.length - 1 : front - 1;
            }
            data[front] = e;
            size ++;
        }
        public void addLast(E e){
            if(size == getCapacity()){
                resize(getCapacity() * 2);
            }
     
            if (data[tail] != null){
               tail = (tail + 1) % getCapacity();
            }
            data[tail] = e;
            size ++;
        }
     
        public E  removeFront(){
            if(isEmpty()){
                throw new RuntimeException("RemoveFront failed. Cannot remove from a empty deque.");
            }
            E ret = data[front];
            data[front] = null;
            size --;
            if(!isEmpty()){
                front = (front + 1) % data.length;
            }
     
            if(size == getCapacity() / 4 && getCapacity() / 2 != 0){
                resize(getCapacity() / 2);
            }
            return ret;
        }
        public E removeLast(){
            if(isEmpty()){
                throw new RuntimeException("RemoveLast failed. Cannot remove from a empty deque.");
            }
     
            E ret = data[tail];
            data[tail] = null;
            size --;
            if(!isEmpty()){
                // tail = tail - 1 >= 0 ? tail - 1 : tail - 1 + data.length;
                tail = tail == 0 ? data.length - 1 : tail - 1;
            }
            if(size == getCapacity() / 4 && getCapacity() / 2 != 0){
                resize(getCapacity() / 2);
            }
            return ret;
        };
     
     
        public E getFront(){
            if(isEmpty()){
                throw new RuntimeException("Deque si empty.");
            }
            return data[front];
        }
     
        public E getLast(){
            if(isEmpty()){
                throw new RuntimeException("Deque si empty.");
            }
            return data[tail];
        }
        @Override
        public String toString(){
            StringBuilder res = new StringBuilder();
            res.append(String.format("Dedeque: size = %d, capacity = %d\n", size, data.length));
            res.append("front [");
            for(int i = 0; i < size ; i ++){
                res.append(data[(i + front) % data.length]);
                if(i != size - 1){
                    res.append(" ,");
                }
            }
            res.append("] tail");
            return res.toString();
        }
        public static void main(String[] args) {
     
            Deque<Integer> deque = new Deque<>(10);
     
            for (int i = 0; i < 10; i ++){
                if(i % 2 == 0){
                    deque.addLast(i);
                    System.out.println(deque);
                    System.out.println("当前添加元素:" + deque.getLast());
                }else{
                    deque.addFront(i);
                    System.out.println(deque);
                    System.out.println("当前添加元素: " + deque.getFront());
                }
     
                if(i % 3 == 2){
                    if(i % 2 == 0){
                        deque.removeLast();
                        System.out.println(deque);
                    }else{
                        deque.removeFront();
                        System.out.println(deque);
                    }
                }
            }
     
        }
    }


    2021-10-29 09:47:39
  • Deque<>{
    
        [] (capacity){
            = ([])Object[capacity]= = = }
    
        (){
            .}
        (){
            }
        (){
            == }(newCapacity){
            [] newData = ([])Object[newCapacity](i = i < i ++){
                newData[i] = [(i + ) % .]}
            = newData= = - }
    
        (e){
            (== getCapacity()){
                resize(getCapacity() * )}
    
            ([] != ){
             = == ? .- : - }
            [] = e++}
        (e){
            (== getCapacity()){
                resize(getCapacity() * )}
    
            ([] != ){
               = (+ ) % getCapacity()}
            [] = e++}
    
        (){
            (isEmpty()){
                RuntimeException()}
            ret = [][] = --(!isEmpty()){
                = (+ ) % .}
    
            (== getCapacity() / && getCapacity() / != ){
                resize(getCapacity() / )}
            ret}
        (){
            (isEmpty()){
                RuntimeException()}
    
            ret = [][] = --(!isEmpty()){
                = == ? .- : - }
            (== getCapacity() / && getCapacity() / != ){
                resize(getCapacity() / )}
            ret}(){
            (isEmpty()){
                RuntimeException()}
            []}
    
        (){
            (isEmpty()){
                RuntimeException()}
            []}
        String (){
            StringBuilder res = StringBuilder()res.append(String.(.))res.append()(i = i < i ++){
                res.append([(i + ) % .])(i != - ){
                    res.append()}
            }
            res.append()res.toString()}
        (String[] args) {
    
            Deque<Integer> deque = Deque<>()(i = i < i ++){
                (i % == ){
                    deque.addLast(i)System..println(deque)System..println(+ deque.getLast())}{
                    deque.addFront(i)System..println(deque)System..println(+ deque.getFront())}
    
                (i % == ){
                    (i % == ){
                        deque.removeLast()System..println(deque)}{
                        deque.removeFront()System..println(deque)}
                }
            }
    
        }
    }


    2021-10-29 09:48:39
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
算法与数据结构
  • 参与学习       2600    人
  • 解答问题       1093    个

慕课网算法名师Liuyubobobo,5年集大成之作 从0到工作5年,算法与数据结构系统解决方案

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码