Encode and Decode Strings
https://www.lintcode.com/zh-cn/problem/encode-and-decode-strings/
import java.util.ArrayList;
import java.util.List;
public class Solution {
/*
* @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
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strs.size(); i++) {
String s = strs.get(i);
if (i != 0) {
sb.append(" ");
}
char[] chars = s.toCharArray();
for (int j = 0; j < chars.length; j++) {
char aChar = chars[j];
sb.append((char) (aChar + 1));
}
}
return sb.toString();
}
/*
* @param str: A string
* @return: dcodes a single string to a list of strings
*/
public List<String> decode(String str) {
// write your code here
String[] split = str.split(" ");
List<String> res = new ArrayList<>();
for (int i = 0; i < split.length; i++) {
String s = split[i];
char[] chars = s.toCharArray();
for (int j = 0; j < chars.length; j++) {
char aChar = chars[j];
chars[j] = (char) (aChar - 1);
}
res.add(String.valueOf(chars));
}
return res;
}
}
相關文章
- python str與byte轉換 encode decodePython
- python中encode和decode函式說明Python函式
- Python——UnicodeEncodeError: 'ascii' codec can't encode/decode charactersPythonUnicodeErrorASCII
- url編碼和解碼分析URLEncoder.encode和URLDecoder.decode
- 7.76 DECODE
- golang url decodeGolang
- hackmyvm--Decode
- no-strings-attached
- iOS 使用 NSCharacterSet encode URLiOS
- leetcode-91-Decode WaysLeetCode
- Add Strings 字串相加字串
- strings 和 strconv 包
- Go Standard library - stringsGo
- php json_encode 細節PHPJSON
- [LeetCode] 205. Isomorphic StringsLeetCode
- json_encode() 不編碼中文JSON
- Composer 提示 zlib_decode (): data errorError
- LeetCode 394. Decode String All In OneLeetCode
- Oracle中Decode()函式的使用Oracle函式
- 聊聊Java 9的Compact StringsJava
- [ABC343G] Compress Strings
- golang中的strings.EqualFoldGolang
- CF482C Game with StringsGAM
- Golang 常用的 strings 函式Golang函式
- Golang strings.Builder 原理解析GolangUI
- go 字串之 strings 包介紹Go字串
- [ARC058F] Iroha Loves Strings
- LeetCode Greatest Common Divisor of Strings All In OneLeetCode
- Go 語言中 strings 包常用方法Go
- 介紹 DotNet 庫 - Viyi.Strings
- PHP JSON_decode 返回為 null 問題PHPJSONNull
- ffmpeg demo decode_video.c例子流程圖IDE流程圖
- [LeetCode] 893. Groups of Special-Equivalent StringsLeetCodeUI
- GO語言————4.7 strings和strconv 包Go
- 每日一個 Golang Packages 06/05 stringsGolangPackage
- CF985F Isomorphic Strings (雜湊)
- Python報錯:UnicodeDecodeError: 'gbk' codec can't decode byte ...PythonUnicodeError
- Could not decode a text frame as UTF-8 的解決