当前位置:   article > 正文

算法1 一维数组的动态和_具体的一维数组动态和

具体的一维数组动态和

在这里插入图片描述

class Solution {
    private int [] forSolution(int[] nums){
        int[] result = new int[nums.length];
        result[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            result[i] = result[i - 1] +  nums[i];
        }
        return result;
    }
     private int [] queueSolution(int[] nums){
        int[] result = new int[nums.length];
        ArrayQueue queue = ArrayQueue.createQueue(nums.length);
        try {
                queue.enQueue(nums[0]);
                for(int i=1; i < nums.length; i++) {
                    int enQueueNum = queue.peek()+ nums[i];
                    queue.enQueue(enQueueNum);
                }
                int index = 0;
                while(!queue.isEmpty()){
                    result[index] = queue.deQueue();
                    index++;
                }
        } catch(Exception e) {
            // do nothing
        }
        return result;
    }

    public int[] runningSum(int[] nums){
        // return forSolution(nums);
        return queueSolution(nums);
    }
}

class ArrayQueue {
    private int front;
    private int rear;
    private int capacity;
    private int [] array;

    private ArrayQueue(int size){
        capacity = size;
        front = -1;
        rear = -1;
        array = new int [size];
    }
    public static ArrayQueue createQueue(int size) {
        return new ArrayQueue(size);
    }
    public boolean isEmpty(){
        return (front == -1);
    }
    public boolean isFull() {
        return ((rear + 1) % capacity == front);
    }
    public int getQueueSize(){
        return ((capacity - front + rear + 1) % capacity);
    }
    public void enQueue(int data) throws Exception {
        if(isFull()) throw new Exception("Queue overflow");
        rear = (rear + 1) % capacity;
        array[rear] = data;
        if(front == -1) front = rear;
    }
    public int deQueue() throws Exception  {
        int data;
        if(isEmpty()) throw new Exception("Queue Empty");
        data = array[front];
        if(front == rear){
            front = rear - 1;
        }else {
            front = (front + 1) % capacity;
        }
        return data;
    }
    public int peek() throws Exception  {
        int data;
        if(isEmpty()) throw new Exception("Queue Empty");
        data = array[rear];
        return data;
    }
}

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/45195
推荐阅读
相关标签
  

闽ICP备14008679号