通过码流串,格式为key,len,val。提供key,查找val的算法。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

public class HuaweiTest {

    public static void main(String[] args) {
//        System.out.println("Hello World!");
        String tag = "31";
        String codeStreamStr = "31 01 32";
        String codeStringVal = getCodeStringVal(tag, codeStreamStr);
        System.out.println(codeStringVal);
    }


    public static String getCodeStringVal(String tag, String codeStreamStr) {
        String trim = tag.trim();
        if (trim == null) {
            return null;
        }
        if (trim.isEmpty()) {
            return null;
        }
        if ((codeStreamStr.length() * 4) > 50000) {
            return null;
        }
        String trim1 = codeStreamStr.trim();
        if (trim1 == null) {
            return null;
        }
        if (trim1.isEmpty()) {
            return null;
        }
        String[] split = trim1.split("\\s");
        if (!(split.length % 3 == 0)) {
            return null;
        }
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            if (s.length() > 2) {
                return null;
            }
            String trim2 = s.trim();
            for (int j = 0; j < trim2.length(); j++) {
                char c = trim2.charAt(j);
                if (Character.isDigit(c)) {
                    continue;
                } else {
                    if (Character.isLowerCase(c)) {
                        return null;
                    }
                    if (!(c >= 'A' && c <= 'F')) {
                        return null;
                    }
                }
            }
        }
        String[] split2 = codeStreamStr.split("\\s");
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        for (int i = 0; i < split2.length; i++) {
            String s = split2[i];
            String trim2 = s.trim();
            if (stringIntegerHashMap.containsKey(trim2)) {
                stringIntegerHashMap.put(trim2, stringIntegerHashMap.get(trim2) + 1);
            } else {
                stringIntegerHashMap.put(trim2, 1);
            }
        }
        System.out.println(stringIntegerHashMap);
        ArrayList<CodeStream> codeStreamArrayList = new ArrayList<>();
        String[] split1 = codeStreamStr.split("\\s");
        int count = 0;
        int codeTagIndex = 0;
        for (int i = 0; i < split1.length; i++) {
            if ((count + 1) % 3 == 0) {
                continue;
            } else {
                CodeStream codeStream = new CodeStream();
                codeStream.setId(UUID.randomUUID().toString());
                String s = split1[codeTagIndex];
                if (extracted(s, stringIntegerHashMap)) return null;
                codeStream.setTag(s);
                String s1 = getString(split1, codeTagIndex);
                if (s1 == null) return null;
                codeStream.setLength(Integer.valueOf(s1));
                String s2 = split1[codeTagIndex + 2];
                codeStream.setValue(s2);
                codeStreamArrayList.add(codeStream);
                count += 3;
                codeTagIndex = count;
            }
        }
        System.out.println(codeStreamArrayList);
        ArrayList<String> stringArrayList = new ArrayList<>();
        codeStreamArrayList.forEach(e -> {
            String tag1 = e.getTag();
            if (tag.equals(tag1)) {
                String value = e.getValue();
                stringArrayList.add(value);
            }
        });
        System.out.println(stringArrayList);
        StringBuilder stringBuilder = new StringBuilder();
        stringArrayList.forEach(e -> {
            stringBuilder.append(e);
            stringBuilder.append("\\s");
        });
        return stringBuilder.toString();
    }

    private static String getString(String[] split1, int codeTagIndex) {
        String s1 = split1[codeTagIndex + 1];
        byte[] bytes1 = s1.getBytes();
//        if (bytes1.length>2){
//            return null;
//        }
        return s1;
    }

    private static boolean extracted(String s, HashMap<String, Integer> stringIntegerHashMap) {
        byte[] bytes = s.getBytes();
        int length = bytes.length;
//        if (length>1){
//            return true;
//        }
        if (stringIntegerHashMap.containsKey(s)) {
            if (stringIntegerHashMap.get(s) >= 2) {
                return true;
            }
        }
        return false;
    }
}

class CodeStream {
    private String id;
    private String tag;
    private Integer length;
    private String value;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "CodeStream{" +
                "id='" + id + '\'' +
                ", tag='" + tag + '\'' +
                ", length=" + length +
                ", value='" + value + '\'' +
                '}';
    }
}

折半查找(Binary Search)和二叉排序树(Binary Search Tree)是两种常用的查找算法。 1. 折半查找算法实现: 折半查找是一种在有序数组中查找指定元素的算法。它的基本思想是将数组分成两部分,然后判断目标元素与中间元素的大小关系,进而确定目标元素在哪一部分中,然后再在该部分中进行查找,如此循环直到找到目标元素或者确定目标元素不存在。 以下是折半查找算法实现示例: ```python def binary_search(arr, target): low = 0 high = len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return -1 # 目标元素不存在 arr = [5, 14, 18, 21, 23, 29, 31, 35] target = 29 result = binary_search(arr, target) print("小于29的最大关键字值是:", arr[result-1]) # 输出:23 ``` 2. 二叉排序树算法实现: 二叉排序树是一种特殊的二叉树,它的每个节点的左子树的所有节点的值都小于该节点的值,右子树的所有节点的值都大于该节点的值。通过这种特性,可以实现高效的插入和删除操作。 以下是二叉排序树的算法实现示例: ```python class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def insert(root, val): if root is None: return TreeNode(val) if val < root.val: root.left = insert(root.left, val) else: root.right = insert(root.right, val) return root def delete(root, val): if root is None: return root if val < root.val: root.left = delete(root.left, val) elif val > root.val: root.right = delete(root.right, val) else: if root.left is None: return root.right elif root.right is None: return root.left else: min_node = find_min(root.right) root.val = min_node.val root.right = delete(root.right, min_node.val) return root def find_min(root): while root.left: root = root.left return root def inorder_traversal(root): if root: inorder_traversal(root.left) print(root.val, end=" ") inorder_traversal(root.right) # 构造二叉排序树 keys = [45, 24, 53, 12, 37, 93, 13] root = None for key in keys: root = insert(root, key) # 删除关键字53和24 root = delete(root, 53) root = delete(root, 24) # 中序遍历二叉排序树 inorder_traversal(root) # 输出:12 13 37 45 93 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值