-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* var str = 'ABDCABC' | ||
* 输出AABBDCC | ||
* 思路:当输出结果是按照字母的ASCII值来排列的话,可以使用排序算法; | ||
* 当输出结果是按照先出现的来排列的话,使用对象,详见下列代码: | ||
*/ | ||
function print(s, num) { | ||
var ret = ''; | ||
for (var i = 0; i < num; i++) { | ||
ret += s; | ||
} | ||
return ret | ||
} | ||
function sortNotSuquent(str) { | ||
var s = str.split('') | ||
var obj = {}; | ||
for(var i = 0; i < s.length; i++) { | ||
if (obj[s[i]]) { | ||
obj[s[i]]++; | ||
} else { | ||
obj[s[i]] = 1; | ||
} | ||
} | ||
var ss = [...new Set(s)]; | ||
var ret = ''; | ||
Object.keys(obj).forEach(function(item) { | ||
ret += print(item, obj[item]) | ||
}) | ||
return ret; | ||
} | ||
sortNotSuquent("ABDCABC") |