首页 > 其他分享 >271. Encode and Decode Strings [Medium]

271. Encode and Decode Strings [Medium]

时间:2023-01-09 19:35:07浏览次数:38  
标签:code String list Decode 271 str Encode strings string

271. Encode and Decode Strings

居然要premium才能做,果断换LintCode来写这题
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.
Please implement encode and decode

Example

Input: ["lint","code","love","you"]
Output: ["lint","code","love","you"]
Explanation:
One possible encode method is: "lint:;code:;love:;you"

思路

只要知道每一个String长度,然后记录他的长度,那解码的时候就可以依据他的长度,拿到该String,关键在于只存长度还是有点问题,因为如果String中也有数字,那就不知道在哪边截断,取出正确的字符串长度,所以可以自定义一个字符。
规则应该是String.length + Special Char + String

题解

  • 无脑快速AC
/*
     * @param strs: a list of strings
     * @return: encodes a list of strings to a single string.
     */
    public String encode(List<String> strs) {
        // write your code here
        String res = "";
        for (String val : strs) {
            res += (val.length() + "," + val);
        }
        return res;
    }

    /*
     * @param str: A string
     * @return: dcodes a single string to a list of strings
     */
    public List<String> decode(String str) {
        // write your code here
        ArrayList<String> result = new ArrayList<>();
        while (!str.equals("")) {
	    // 拿到截断字符串的下标,
            int splitIndex = str.indexOf(",");
	    // 通过该下标,直接截断字符串取出当前待解码字符串长度
            Integer strLength = Integer.valueOf(str.substring(0, splitIndex));
	    // 设置待解码字符串在当前编码字符串中的起始位置
            int start = splitIndex + 1;
	    // 算出结束位
            int end = start + strLength;
            String curStr = str.substring(start, end);
            result.add(curStr);
	    //  直接将当前已插入结果集的子串截去,substring不太熟练,+1-1搞晕
            str = str.substring(end);
        }
        return result;
    }

标签:code,String,list,Decode,271,str,Encode,strings,string
From: https://www.cnblogs.com/tanhaoo/p/17038323.html

相关文章

  • [HarekazeCTF2019]encode_and_encode
    [HarekazeCTF2019]encode_and_encode考点:json_decode的unicode编码绕过进入题目后,很容易就可以看到源码query.php<?phperror_reporting(0);if(isset($_GET['source'......
  • weblogic XMLDecoder反序列化漏洞(CVE-2017-10271)
    漏洞描述WeblogicWLS组件中存在CVE-2017-10271远程代码执行漏洞,可以构造请求对运行weblogic中间件的主机进行攻击。受影响weblogic版本10.3.6.0.0,12.1.3.0.0,12.2.1.1.......
  • C# to PHP base64 encode/decode
    ​​http://stackoverflow.com/questions/257462/c-sharp-to-php-base64-encode-decode​​We shouldprobablyURLEncodeyourBase64stringontheC#sidebeforeyou......
  • 变分自编码器 - VAE: Variational Auto-Encoder
    总之,VAE本身是一个生成模型,我们假设观测的某个变量\(\mathbf{x}\)(比如数字0~9的各种图像)受到隐变量\(\mathbf{z}\)的影响,那么在得到分布后,只需要采样得到一个\(\mat......
  • 【李宏毅机器学习】自编码器auto-encoder
    note:VAE的本质结构:重构的过程是希望没噪声的,而KLloss则希望有高斯噪声的,两者是对立的。所以,VAE跟GAN一样,内部其实是包含了一个对抗的过程,只不过它们两者是混合起来,共......
  • PY0271验证码的创建
    生成的随机背景色的效果  fromPILimportImage,ImageDraw,ImageFont,ImageFilterimportrandom#设置随机的背景颜色。defrndColor():r=random.randint(......
  • 软件icons图标大全(新增至2719枚大苏尔风格图标)下载
    想要获取海量图标,macw小编为大家分享一套mac电脑icons图标包,此套图标大全中包含两千多个.icns格式图片,这是一套不可多得的设计素材包。软件icons图标大全(新增至2719枚大......
  • Python读取文件时出现UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in po
    Python在读取文件时withopen('article.txt')asf:#打开新的文本text_new=f.read()#读取文本数据出现错误:UnicodeDecodeError:'gbk'codeccan'tdecodeby......
  • JJencode和emoji加密
    JavaScript混淆是一种常用的手段,用于在不改变代码功能的情况下使代码难以被人阅读和理解。本文将介绍两种常见的JavaScript混淆方法——JJencode和emoji加密,并给出解......
  • Python encode()方法和decode()方法
    Pythonencode()方法encode()方法为字符串类型(str)提供的方法,用于将str类型转换成bytes类型,这个过程也称为“编码”。encode()方法的语法格式如下:str.encode([enco......