publicclassCodec {// Encodes a list of strings to a single string.publicStringencode(List<String> strs) {StringBuilder sb =newStringBuilder();for(String str : strs){for(int i =0; i <str.length(); i++){if(str.charAt(i) =='#'){sb.append('\\'); }sb.append(str.charAt(i)); }sb.append('#'); }if(sb.length() >1) sb.setLength(sb.length() -1);returnsb.toString(); }// Decodes a single string to a list of strings.publicList<String> decode(String s) {List<String> list =newArrayList<String>();StringBuilder sb =newStringBuilder();for(int i =0; i <s.length(); i++){if(s.charAt(i) !='#'){sb.append(s.charAt(i)); } else {// Found '\#' , remove '\' and add '#'if(i -1>=0&&s.charAt(i -1) =='\\'){sb.setLength(sb.length() -1);sb.append('#'); } else {list.add(sb.toString());sb.setLength(0); } } }if(sb.length() >0){list.add(sb.toString()); }return list; }}// Your Codec object will be instantiated and called as such:// Codec codec = new Codec();// codec.decode(codec.encode(strs));
思想上讲和 OS 的 file system 是非常像的,在实际 data 最前面的 header 里会存 metadata,告诉你下一段数据的内存地址或者offset。
publicclassCodec {// Encodes a list of strings to a single string.publicStringencode(List<String> strs) {StringBuilder sb =newStringBuilder();for(String str : strs){sb.append(str.length());sb.append(':');sb.append(str); }returnsb.toString(); }// Decodes a single string to a list of strings.publicList<String> decode(String s) {List<String> list =newArrayList<String>();int i =0;while(i <s.length()){int delimiter =s.indexOf(':', i);int size =Integer.valueOf(s.substring(i, delimiter));list.add(s.substring(delimiter +1, delimiter +1+ size)); i = delimiter +1+ size; }return list; }}
publicclassCodec {// Encodes a list of strings to a single string.publicStringencode(List<String> strs) {StringBuilder sb =newStringBuilder();for(String str : strs){for(int i =0; i <str.length(); i++){char chr =str.charAt(i);if(chr =='/'){sb.append("//"); } elseif(chr =='#'){sb.append("/#"); } else {sb.append(chr); } }sb.append("#"); }returnsb.toString(); }// Decodes a single string to a list of strings.publicList<String> decode(String s) {List<String> res =newArrayList<>();StringBuilder str =newStringBuilder();for (int i =0; i <s.length(); i++) {if (s.charAt(i) =='/') {str.append(s.charAt(++i)); } elseif (s.charAt(i) =='#') { res.add(str.toString()); str.setLength(0); } else {str.append(s.charAt(i)); } }return res; }}// Your Codec object will be instantiated and called as such:// Codec codec = new Codec();// codec.decode(codec.encode(strs));
publicclassValidWordAbbr {HashMap<String,String> map;publicValidWordAbbr(String[] dictionary) { map =newHashMap<String,String>();for(String str:dictionary){String key =getKey(str);// If there is more than one string belong to the same key// then the key will be invalid, we set the value to ""if(map.containsKey(key)){if(!map.get(key).equals(str)){map.put(key,""); } } else {map.put(key, str); } } }publicbooleanisUnique(String word) {return!map.containsKey(getKey(word))||map.get(getKey(word)).equals(word); }StringgetKey(String str){if(str.length() <=2) return str;returnstr.charAt(0)+Integer.toString(str.length()-2)+str.charAt(str.length()-1); }}
publicclassSolution {publicList<String> generateAbbreviations(String word) {List<String> list =newArrayList<>();dfs(list,new StringBuilder(),word.toCharArray(),0,0);return list; }privatevoiddfs(List<String> list,StringBuilder sb,char[] word,int index,int curNum){int len =sb.length();if(index ==word.length){if(curNum !=0) sb.append(curNum);list.add(sb.toString()); } else {// Don't add, merge to current numberdfs(list, sb, word, index +1, curNum +1);// Add current charif(curNum !=0) sb.append(curNum);dfs(list,sb.append(word[index]), word, index +1,0); }sb.setLength(len); }}