数据结构-数组之数组分割

思想

在用关键词遍历分割对象时,只存储位置和长度信息。遍历结束后,根据遍历时取到的信息进行截取。

 

源代码:

    public static byte[][] split(byte[] in, byte[] keys) {

        if ( in == null ) return null;

        if ( keys == null || keys.length <= 0) return new byte[][] {in};

        if (in.length < keys.length) return new byte[][] {in};

        ArrayList<SplitedInfo> splited = new ArrayList<SplitedInfo>();

        int pre_pos = 0;

        int pos = 0;

        int length = 0;

        int len_in = in.length;

        int len_keys = keys.length;

        int max = (len_in - len_keys) + 1;

 

        while(pos < max) {

            // System.out.println("pos=" + pos + ",max=" + max);

            if (compare(in, pos, keys) == true) {

                // pos

 

                // length

                length = pos - pre_pos;

                // System.out.println("★pos:" + pre_pos + ",length=" + length);

                splited.add(new SplitedInfo(pre_pos, length));

                pos += len_keys - 1;

                pre_pos = pos + 1;

            }

            pos ++;

        }

        length = len_in - pre_pos;

        if (length > 0) {

            splited.add(new SplitedInfo(pre_pos, length));

        }

        // System.out.println("★pos:" + pre_pos + ",length=" + length);

        byte[][] result = new byte[splited.size()][];

        int i = 0;

        for (SplitedInfo info : splited) {

            if (info.getLength() == 0) {

                result[i] = null;

            } else {

                result[i] = new byte[info.getLength()];

                System.arraycopy(in, info.getPos(), result[i], 0, info.getLength());

            }

            i ++;

        }

        return result;

    }

    private static boolean compare(byte[] in, int pos, byte[] keys) {

        int i = 0;

        while(i < keys.length) {

            if (keys[i] != in[pos + i]) {

                return false;

            }

            i ++;

        }

        return true;

    }

}

 

class SplitedInfo {

    private int pos;

    private int length;

    public SplitedInfo() {

        this.pos = -1;

        this.length = -1;

    }

    public SplitedInfo(int pos, int length) {

        this.pos = pos;

        this.length = length;

    }

    /**

     * @return pos

     */

    public int getPos() {

        return pos;

    }

    /**

     * @param pos  pos

     */

    public void setPos(int pos) {

        this.pos = pos;

    }

    /**

     * @return length

     */

    public int getLength() {

        return length;

    }

    /**

     * @param length  length

     */

    public void setLength(int length) {

        this.length = length;

    }

    /**

     * @see java.lang.Object#toString()

     */

    @Override

    public String toString() {

        StringBuffer buf = new StringBuffer();

        buf.append("pos=");

        buf.append(this.pos);

        buf.append(",length=");

        buf.append(this.length);

        return buf.toString();

    }

}

 

测试程序:

import javay.util.UArys;

 

public class TestSplit {

 

    public static void main(String[] args) {

        byte[] a = new byte[10];

        a[0] = 1;

        a[1] = 1;

        a[2] = 2; // 0, 2

        a[3] = 1;

        a[4] = 3;

        a[5] = 2; // 3, 2

        a[6] = 4;

        a[7] = 2; // 6, 1

        a[8] = 2; // 8, 0

        a[9] = 5; // 9. 1

 

        byte[] b = new byte[2];

        b[0] = 2;

        b[1] = 2;

 

 

        byte[][] c = UArys.split(a, b);

        System.out.println("test");

    }

 

}

 

 

(结束)

 

转载于:https://my.oschina.net/dubenju/blog/731075

### 数组分割数据结构实现方法 数组分割是一种常见的数据处理技术,主要用于将一个大的数组划分为多个小的子数组。这种操作在许多场景中都非常有用,例如并行计算、分块存储和数据分片等。以下是关于数组分割的一些常见数据结构和实现方法。 #### 1. 使用堆(或集合)维护最小值 在某些情况下,数组分割需要满足特定条件,例如左部分的最大值小于等于右部分的最小值。在这种情况下,可以使用堆或集合来动态维护剩余部分的最小值[^1]。 ```cpp class Solution { public: int partitionDisjoint(vector<int>& nums) { int n = nums.size(); multiset<int> st; for (auto c : nums) st.insert(c); int mx = -1; for (int i = 0; i < n; i++) { mx = max(nums[i], mx); st.erase(st.find(nums[i])); if (mx <= *st.begin()) return i + 1; } return -1; } }; ``` 上述代码通过`multiset`实现了动态删除元素的功能,并利用堆顶元素判断分割点。 #### 2. 使用固定大小分割数组 对于简单的数组分割任务,可以按照固定的大小将数组划分为若干个子数组。这种方法通常用于分块存储或批量处理数据[^4]。 ```javascript function chunk(arr, size) { var rsArr = []; for (var i = 0; i < arr.length; i += size) { var tempArr = []; for (var j = 0; j < size && i + j < arr.length; j++) { tempArr.push(arr[i + j]); } rsArr.push(tempArr); } return rsArr; } chunk(["a", "b", "c", "d"], 2); ``` 该实现通过双重循环完成了按指定大小分割数组的任务。 #### 3. 动态调整分割大小 如果分割大小不是固定的,而是根据某种规则动态调整,则可以引入更复杂的逻辑。例如,在某些情况下,可以根据数组元素的特性(如权重或优先级)来决定每个子数组的大小。 #### 4. 使用栈实现多数组分割 在一些特殊的应用场景中,可以使用栈来实现多数组分割。例如,将一个数组划分为三个独立的栈,每个栈占据数组的一部分[^5]。 ```java int stackSize = 100; int[] stackPointer = {-1, -1, -1}; int[] buffer = new int[stackSize * 3]; /* 返回栈“stackNum”栈顶元素在数组中的索引 */ int topOfStack(int stackNum) { return stackSize * stackNum + stackPointer[stackNum]; } ``` 上述代码展示了如何通过数组实现三个固定大小的栈,每个栈的元素存储在数组的不同区域。 #### 5. 分割算法的时间复杂度分析 不同的分割方法具有不同的时间复杂度。例如,基于固定大小的分割算法的时间复杂度为O(n),而基于堆的动态分割算法的时间复杂度可能更高,具体取决于堆的操作次数和复杂度[^1]。 ### 总结 数组分割可以通过多种方式实现,包括使用堆、集合、固定大小分割以及栈等数据结构。选择合适的实现方法取决于具体的业务需求和性能要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值