Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7bd9f56

Browse files
committed
minor optimize runlength
1 parenta4235e0 commit7bd9f56

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

‎src/compression/runlength/runlength.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,37 @@
1414
* This takes O(n).
1515
*/
1616
functionconvertToAscii(str){
17-
varresult='';
17+
varresult=[];
1818
varcurrentChar='';
1919
vari=0;
2020
for(;i<str.length;i+=1){
2121
currentChar=str[i].charCodeAt(0).toString(2);
22-
if(currentChar.length<8){
23-
while(8-currentChar.length){
24-
currentChar='0'+currentChar;
25-
}
26-
}
27-
result+=currentChar;
22+
currentChar=newArray(9-currentChar.length).join('0')+currentChar;
23+
result.push(currentChar);
2824
}
29-
returnresult;
25+
returnresult.join('');
3026
}
3127

3228
/**
3329
* Encodes the binary string to run-length encoding.
3430
* Takes O(n^2).
3531
*/
3632
functionrunLength(vector){
37-
varresult='';
33+
varresult=[];
3834
varzeros=0;
3935
varzerosTemp='';
40-
varwordLength=0;
4136
vari=0;
4237
for(;i<vector.length;i+=1){
4338
if(vector[i]==='0'){
4439
zeros+=1;
4540
}else{
4641
zerosTemp=zeros.toString(2);
47-
wordLength=zerosTemp.length-1;
48-
while(wordLength){
49-
result=result+'1';
50-
wordLength-=1;
51-
}
52-
result+='0'+zerosTemp;
42+
result.push(newArray(zerosTemp.length).join('1'));
43+
result.push('0'+zerosTemp);
5344
zeros=0;
5445
}
5546
}
56-
returnresult;
47+
returnresult.join('');
5748
}
5849

5950
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp