Commit 4a7b4724 authored by Scott Sun's avatar Scott Sun

scott

parent eb680f48
function stringZip(iStr) { function stringZip(iStr) {
var arr = []; var res = "";
var temp = {}; var temp = 1;
for (var i = 0, len = iStr.length; i < len; i++) { for (var i = 0, len = iStr.length; i < len; i++) {
var element = iStr[i];
if (i === 0) { if (i === 0) {
temp[element] = 1 res = len===1? "1" + iStr[i] : ""
} else if (element === iStr[i-1]) { } else if (iStr[i] === iStr[i-1]) {
temp[element]++ temp++
} else { } else {
arr.push(temp) res += temp + iStr[i-1]
temp = {} temp = 1
temp[element] = 1
} }
if(i >= len - 1){ if(i >= len - 1 && len > 1){
arr.push(temp) res += temp + iStr[i-1]
} }
} }
var res = "";
arr.forEach(function(v){
for (var key in v) {
res += v[key] + key
}
})
return res return res
} }
...@@ -36,7 +28,9 @@ function stringUnZip(iStr) { ...@@ -36,7 +28,9 @@ function stringUnZip(iStr) {
return str return str
} }
// console.log(stringZip("aaabccccaaaaccddecccee")); console.log(stringZip("aaabccccaaaaccddecccee"));
console.log(stringZip("a"));
console.log(stringUnZip("3a1b6c2d3e")); console.log(stringUnZip("3a1b6c2d3e"));
console.log(stringUnZip("1a"));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment