题目很水,不过看到了很简洁的写法学习一下.
题目描述
水果机器有三列,出现的结果分别可能是十种情况. 得分标准如下:
1. There are always exactly three reels
2. Each reel has 10 different items.
3. The three reel inputs may be different.
4. The spin array represents the index of where the reels finish.
5. The three spin inputs may be different
6. Three of the same is worth more than two of the same
7. Two of the same plus one "Wild" is double the score.
8. No matching items returns 0.

Sample
reel1 = ["Wild","Star","Bell","Shell","Seven","Cherry","Bar","King","Queen","Jack"];
reel2 = ["Wild","Star","Bell","Shell","Seven","Cherry","Bar","King","Queen","Jack"];
reel3 = ["Wild","Star","Bell","Shell","Seven","Cherry","Bar","King","Queen","Jack"];
spin = [5,5,5];
result = fruit([reel1,reel2,reel3],spin);
代码实现
function fruit(reels, spins) {
let list = ['Jack', 'Queen', 'King', 'Bar', 'Cherry', 'Seven', 'Shell', 'Bell', 'Star', 'Wild'];
let [c,b,a] = reels
.map((reel,i)=>list.indexOf(reel[spins[i]])+1)
.sort((a,b)=>a-b);
if(a===b && a===c) return a*10;
if(a===b) return c===10 ? a * 2 : a;
return b===c?b:0;
}
感觉有点皮
本文介绍了一种简洁的水果机得分算法实现。该算法通过处理三个相同的数组(每组包含10种不同的项),根据旋转后的结果计算得分。特别地,相同项越多得分越高,若有“Wild”项则按特定规则加倍得分。
1300

被折叠的 条评论
为什么被折叠?



