华为OD --- TLV解码

华为OD --- TLV解码

题目

独立实现

理解

个人认为这题最大的难点就是理解题目

以测试用例举个🌰

js 复制代码
31
32 01 00 AE 90 02 00 01 02 30 03 00 AB 32 31 31 02 00 32 33 33 01 00 CC

题目需要找到tag 31对应的value值. 示例中第一个tag值为32,由于,所以tag31对应的length为01,00,又因为,小端序简单来说就是和我们理解的字节序完全相反,比如一个数字是2311,那么按小端序排列就是1132.(小端序参考资料),那么tag31对应的length长度应该是01 -> 10 00 -> 00,最后合并成0001.那么对应的tag长度就是1, tag的value值完全取决于tag的长度,因为tag31的长度是1,那么value值就往后取一位就行为AE.

思路

按照题目理解 我们可以把输入流所有的tag以及对应的value值全部找出来,最后直接输出就行.题主使用的是map数据结构来保存.如题目测试用例,最后的数据结构就是

AC源码

js 复制代码
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // 输入字符串个数
    const allStrLength = []
    // 保存关键字
    const tartTag = await readline();
    let TLV_code_stream = (await readline()).split(' ');

    const tagMap = new Map()
    let currentTagIndex = ''
    let currentTagValue = []


    do {
        // 取出第一个作为map的值
        currentTagIndex = TLV_code_stream.shift();
        // tag长度为两个字节
        let currentLength = TLV_code_stream.splice(0, 2).map(item => item.split('').reverse().join('')).join('').split('').reverse().join('')
        tagLength = parseInt(currentLength, 16)
        currentTagValue = TLV_code_stream.splice(0, tagLength);
        tagMap.set(currentTagIndex, {
            length: tagLength,
            value: currentTagValue
        })

    } while (TLV_code_stream.length > 0)


    // console.log(tagMap)
    console.log(tagMap.get(tartTag).value.join(' '))


})();
相关推荐
塔中妖5 天前
【华为OD】分割数组的最大差值
数据结构·算法·华为od
塔中妖5 天前
【华为OD】数字游戏
算法·游戏·华为od
熊文豪6 天前
【华为OD】找出通过车辆最多颜色
算法·华为od
塔中妖6 天前
【华为OD】环中最长子串2
算法·华为od
熊文豪7 天前
【华为OD】区块链文件转储系统
算法·华为od·区块链
塔中妖7 天前
【华为OD】Linux发行版的数量
linux·算法·华为od
熊文豪7 天前
【华为OD】阿里巴巴找黄金宝箱
算法·华为od
塔中妖7 天前
【华为OD】5G网络建设
网络·5g·华为od
塔中妖7 天前
【华为OD】查找接口成功率最优时间段
算法·链表·华为od
塔中妖7 天前
【华为OD】最大子矩阵和
算法·华为od·矩阵