每一回合,你可以从两端之一拿起一枚石子(位置最大或最小),并将其放入两端之间的任一空闲位置。形式上,假设这三枚石子当前分别位于位置 x, y, z 且 x < y < z。那么就可以从位置 x 或者是位置 z 拿起一枚石子,并将该石子移动到某一整数位置 k 处,其中 x < k < z 且 k != y。
classSolution { publicint[] numMovesStones(int a, int b, int c) { intx= Math.min(Math.min(a, b), c); intz= Math.max(Math.max(a, b), c); inty= a + b + c - x - z;
int[] res = newint[2]; res[0] = 2; if (z - y == 1 && y - x == 1) { res[0] = 0; } elseif (z - y <= 2 || y - x <= 2) { res[0] = 1; } res[1] = z - x - 2; return res; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9
classSolution: defnumMovesStones(self, a: int, b: int, c: int) -> List[int]: x, y, z = sorted([a, b, c]) res = [2, z - x - 2] if ((z - y) == 1and (y - x) == 1): res[0] = 0 elif ((z - y) <= 2or (y - x) <= 2): res[0] = 1 return res
funcnumMovesStones(a int, b int, c int) []int { x:= min(min(a, b), c) z:= max(max(a, b), c) y:= a + b + c - x - z res := []int{2, z - x - 2} if ((z - y) == 1 && (y - x) == 1) { res[0] = 0; } elseif ((z - y) <= 2 || (y - x) <= 2) { res[0] = 1; } return res }
funcmin(a, b int)int { if a < b { return a } return b }
funcmax(a, b int)int { if a > b { return a } return b }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12
var numMovesStones = function(a, b, c) { let x = Math.min(Math.min(a, b), c); let z = Math.max(Math.max(a, b), c); let y = a + b + c - x - z; let res = [2, z - x - 2]; if (z - y == 1 && y - x == 1) { res[0] = 0; } elseif (z - y <= 2 || y - x <= 2) { res[0] = 1; } return res; };
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassSolution { publicint[] NumMovesStones(int a, int b, int c) { int x = Math.Min(Math.Min(a, b), c); int z = Math.Max(Math.Max(a, b), c); int y = a + b + c - x - z;
int[] res = newint[2]; res[0] = 2; if (z - y == 1 && y - x == 1) { res[0] = 0; } elseif (z - y <= 2 || y - x <= 2) { res[0] = 1; } res[1] = z - x - 2; return res; } }
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
int* numMovesStones(int a, int b, int c, int* returnSize) { int x = MIN(a, b); int z = MAX(a, b); x = MIN(x, c); z = MAX(z, c); int y = a + b + c - x - z;