* code_3_plus.js
/*
【题目】
从一副不含大小王的牌组中随机抽出7张牌,判断7张牌中是否存在顺子牌型,即是否存在5张连续的牌。
当存在顺子牌型时,返回最大的一组顺子,即点数之和相加最大的5张连续牌,不存在时则直接返回null。
每张扑克牌由3位数的字符串组成,【1位花色+2位点数】即百位表示花色,个位和十位共同表示点数
花色部分:1-4分别表示,黑桃、红桃、草花、方块
点数部分:A为1,2-10为数字本身,J为11,Q为12,K为13。A可以视为14。
例:
101 表示黑桃A
201 表示红桃A
112 表示黑桃Q
【需求】
需要用JavaScript完成函数getShunzi,使其实现以下示例效果
【示例】
示例 1:
输入: ['101','102','103','104','105', '106', '107']
输出: ['103','104','105','106','107']
示例 2:
输入: ['202','203','101','104','105','308','107']
输出: ['101','202','203','104','105']
示例 3:
输入: ['101','102','103','104','105', '201', '107']
输出: ['101','102','103','104','105'] 或 ['201','102','103','104','105'] 均可
示例 4:
输入: ['202','206','101','104','105', '201', '308']
输出: null
*/
module.exports = getShunzi;
function Poker(s) {
const ZERO = "0".charCodeAt(0);
this.suit = s.charCodeAt(0) - ZERO;
this.number = 10 * (s.charCodeAt(1)-ZERO) + s.charCodeAt(2)-ZERO;
}
Poker.prototype.__toString = function() {
var s = "" + this.suit;
if (this.number < 10) {
s += "0";
} else if (this.number === 14) {
s += "01"; // 01,14都是A
return s;
}
s += this.number;
return s;
}
Poker.groupBy = function(/* Poker[] */pokers) {
var a = [null, [], [], [], []];
pokers.forEach(function(p) {
a[p.suit].push(p);
});
return a;
}
Poker.sortByNumber = function(pokers) {
return pokers.sort(function(/* Poker */a, /* Poker */b) {
if (0 === a.number - b.number) {
return a.suit - b.suit;
}
return a.number - b.number;
});
}
Poker.unique = function(pokers) {
var m = [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0];
var a = [];
pokers.forEach(function(p) {
if (m[p.number] === 0) {
a.push(p);
m[p.number] += 1;
}
});
return a;
}
/**
* @param {string[]} pokers
* @return {string[]}
*/
function getShunzi(pokers = []) {
// 在此编写逻辑
var a = pokers.map(function(s) {
return new Poker(s);
});
// "A" 01,14
var len = a.length;
for (var i = 0; i <len; i++) {
if (a[i].number === 1) {
a.push(new Poker(a[i].suit +"14"))
}
}
a = Poker.unique( Poker.sortByNumber(a) );
// console.debug(a)
var left, right;
var d = 0;
const N = 5;
for (right = a.length-1, left = right - 1;
0 < left && 0 < right && right+1-left < N; )
{
d = a[left+1].number - a[left].number;
if (d !== 1) {
right -= 1;
left = right -1;
} else {
left -= 1;
}
// console.debug("left="+left + ", right=" + right + ", d="+d);
}
if (right+1 - left < N) {
return null;
}
d = a[left+1].number - a[left].number;
if (d !== 1) {
return null;
}
return a.slice(left, right+1).map(function(poker) {
return poker.__toString();
});
}
* main.js
var getShunzi = require ("./code_3_plus");标签:pokers,return,牌型,张牌,number,var,101,顺子,left From: https://blog.51cto.com/u_11658127/6037069
// var pokers = ['101','102','103','104','105', '106', '107']
// var pokers = ['107','202','102','203','105','101','104','204','308'];
// var pokers = ['202','203','101','104','105','308','107'];
// var pokers = ['101','102','103','104','105', '201', '107'];
// var pokers = ['202','206','101','104','105', '201', '308'];
// var pokers = ['201','208','209','110','111','112','313','101']
var pokers = ['101','207','411','113','102','412','303'];
var a = getShunzi(pokers);
console.log(a);