C#,135字节
打高尔夫球
(int[] i)=>{for(int x=1,v,m=0,l=i.Length,y;x<l;x++){v=i[x];for(y=0;y<l&&v==i[x]?y<x:y<l;y++)if(i[y]==v){v=++m;y=-1;}i[x]=v;}return i;};
不打高尔夫球
( int[] i ) => {
for( int x = 1, v, m = 0, l = i.Length, y; x < l; x++ ) {
v = i[ x ];
for( y = 0; y < l && v == i[ x ] ? y < x : y < l ; y++ )
if( i[ y ] == v ) {
v = ++m;
y = -1;
}
i[ x ] = v;
}
return i;
};
非高尔夫可读
// Takes an array of Int32 objects ( 32-byte signed integers )
( int[] i ) => {
// Cycles through each element on the array
// x: Scan position, starts at the 2nd element
// v: Value being processed
// m: The next minimum value to replace
// l: Size of the array, to save some byte count
for( int x = 1, v, m = 0, l = i.Length, y; x < l; x++ ) {
// Hold the value
v = i[ x ];
// Re-scan the array for a duplicate value up the the current position ( 'x' ) IF
// ... the currently hold value hasn't been modified
// ... otherwise, re-scans the entire array to find a suitable value to replace
for( y = 0; y < l && v == i[ x ] ? y < x : y < l ; y++ )
// Check if 'v' shares the same value with other element
if( i[ y ] == v ) {
// Set 'v' to the minimum value possible
v = ++m;
// Reset the scan position to validate the new value
y = -1;
}
// Set the 'v' to the array
i[ x ] = v;
}
// Return the array
return i;
};
完整代码
using System;
using System.Collections.Generic;
namespace Namespace {
class Program {
static void Main( String[] args ) {
Func<Int32[], Int32[]> f = ( int[] i ) => {
for( int x = 1, v, m = 0, l = i.Length, y; x < l; x++ ) {
v = i[ x ];
for( y = 0; y < l && v == i[ x ] ? y < x : y < l ; y++ )
if( i[ y ] == v ) {
v = ++m;
y = -1;
}
i[ x ] = v;
}
return i;
};
List<Int32[]>
testCases = new List<Int32[]>() {
new Int32[] { },
new Int32[] { 5 },
new Int32[] { 1, 4, 2, 5, 3, 6 },
new Int32[] { 3, 3, 3, 3, 3, 3 },
new Int32[] { 6, 6, 4, 4, 2, 2 },
new Int32[] { 2147483647, 2, 2147483647, 2 },
};
foreach( Int32[] testCase in testCases ) {
Console.WriteLine( $" Input: {String.Join( ",", testCase )}\nOutput: {string.Join( ",", f( testCase ) )}\n" );
}
Console.ReadLine();
}
}
}
发布
笔记
6, 6, 1, 2, 3, 4, 5
→6, 7, 1, 2, 3, 4, 5