解题方案:
var originalDigits = function(s) {
let match = (char) => (s.match(new RegExp(char,'g')) || []).length ;
let n0 = match('z');
let n2 = match('w');
let n8 = match('g');
let n6 = match('x');
let n3 = match('t') - n2 - n8;
let n4 = match('r') - n0 - n3;
let n7 = match('s') - n6;
let n1 = match('o') - n4 - n2 - n0;
let n5 = match('v') - n7;
let n9 = match('i') - n8 - n6 - n5;
let res = [n0,n1,n2,n3,n4,n5,n6,n7,n8,n9]
return res.reduce( (str,times,num) => str+num.toString().repeat(times),'' )
};
解题思路:找规律,寻找各个数字中的不同点,比如只有zero中有字母‘z’,只有two中有‘w’等等,然后得到所有数字的个数;
1、reduce() 方法:计算数组元素相加后的总和;
2、a.repeat(b) 方法:重复a字符串b次;