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;
}
}
相關文章
- LeetCode-Encode and Decode StringsLeetCode
- perl encode,decode
- python encode和decode的妙用Python
- EXT中decode()和encode()方法(轉載)
- python編碼問題之”encode”&”decode”Python
- python中encode和decode函式說明Python函式
- python str與byte轉換 encode decodePython
- 字串的encode與decode解決亂碼問題字串
- Python——UnicodeEncodeError: 'ascii' codec can't encode/decode charactersPythonUnicodeErrorASCII
- 簡單加密/解密方法包裝, 含encode(),decode(),md5() (轉)加密解密
- PHPjson_encode/json_decode與serialize/unserializ效能測試PHPJSON
- PHP中json_decode()和json_encode()的使用方法PHPJSON
- url編碼和解碼分析URLEncoder.encode和URLDecoder.decode
- php中利用json_encode和json_decode傳遞包括特殊字元的資料PHPJSON字元
- JSON到物件的轉換(反序列化)方法,EXT.decode()和EXT.encode()方法(轉帖)JSON物件
- 7.76 DECODE
- golang url decodeGolang
- decode函式函式
- decode的使用
- hackmyvm--Decode
- Analyzing Strings with sscanf
- iOS 使用 NSCharacterSet encode URLiOS
- 代替DECODE的CASE
- Add Strings 字串相加字串
- Leetcode Multiply StringsLeetCode
- Chapter 1 Arrays and Strings - 1.7APT
- Linux/AIX strings 命令LinuxAI
- Go Standard library - stringsGo
- golang中的strings.EqualFoldGolang
- Golang 常用的 strings 函式Golang函式
- Leetcode-Multiply StringsLeetCode
- Multiply Strings leetcode javaLeetCodeJava
- CF482C Game with StringsGAM
- php json_encode 細節PHPJSON
- json_decode詳解JSON
- Leetcode-Decode WaysLeetCode
- Decode Ways leetcode javaLeetCodeJava
- oracle中的decode(函式)Oracle函式