leetcode:423. 从英文中重建数字
解题方案: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 = m
·
解题方案:
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次;
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)