‘下面的代码可以很好地处理某些输入,而不能处理其他输入。该代码是给定n=列表长度的循环链表的实现,以及要采取的删除节点的k=步骤。其意图是删除增量为k的节点,例如如果1,2,3和k =2,那么要删除的第一个节点是2,然后是1,因此最后一个节点将是3代码上有2个测试用例,第一个是n=12 k=4,结果应该是1,但我在最后一个消除步骤得到了2,由于某种原因,它弄乱了秒测试用例是n=12 k=3,结果应该是10,这是正常的,所以我不知道是否有人能识别购买,谢谢。
public static int whoIsElected(int n, int k) {
LinkedList<Integer> circle = new LinkedList<>();
for(int i=1;i<=n;i++)
{
circle.add(i);
}
int posToDel = k;
if(n==1)
{
return 1;
}
if(posToDel>n)
{
posToDel=posToDel%n;
}
while(circle.size()>1)
{
if(posToDel==0)// becasue the posToDel-1 when posToDel ==0 is the last index
{
circle.remove(n-1);
}
else
{
circle.remove(posToDel-1);
}
n--; // decrease the length of n by 1 when u delete one from the list
// after removing the posToDel in circle
// this if else set the new posToDel
if(posToDel==0)
{
posToDel=n-1;
}
else
{
posToDel=posToDel-1;
}
if(posToDel+k>n)
{
posToDel=(posToDel+k)%n;
}
else
{
posToDel=posToDel+k;
}
}
return circle.get(0);
}
/**
* bool doTestsPass()
* Runs various tests. Returns true if tests pass. Otherwise,
* returns false.
*/
public static boolean doTestsPass() {
// todo: implement more tests, please
// feel free to make testing more elegant
// test cases are structered as {n, k, expected answer}
int[][] testCases = {
{12, 4, 1},// give wrong answer
{12, 3, 10}// give right answer
};
for (int[] testCase : testCases) {
int answer = whoIsElected(testCase[0], testCase[1]);
if (answer != testCase[2]) {
System.out.println("test failed!");
System.out.printf("n:%d, k%d, answer got: %d, should be: %d\n", testCase[0], testCase[1], answer, testCase[2]);
return false;
}
}
System.out.println("All tested passed");
return true;
}
/**
* Execution entry point.
*/
public static void main(String args[]) {
doTestsPass();
}}
发布于 2020-01-23 11:32:50
我猜当只剩下3个元素时,问题就出现了[1,2,9]
因此,当手动尝试时,当要删除元素9时,posToDel应该为3,但当我们在代码中运行时,posToDel值为0。
您可以尝试在不同的时间点添加控制台日志和跟踪值。但它可能会很快变得混乱,你可能最终会花费比所需更多的时间。
然而,我可以建议你在循环逻辑中尝试以下算法:
在循环之前赋值posToDel= k-1,这样你就不必每次都在循环中执行posToDel-1 (只会让在循环中构建不同的逻辑变得更容易,特殊情况更少)
我已经尝试了下面的循环逻辑,它很适合你的测试用例:
while( circle.size() > 1 )
{
int temp = circle.remove( posToDel );
System.out.println( temp );
n--;
posToDel = posToDel - 1;
System.out.println( "after deletion of " + temp + ", pos is: " + posToDel );
if( posToDel + k >= n )
{
posToDel = ( posToDel + k ) % n;
}
else
{
posToDel = posToDel + k;
}
}我添加了2条sysout语句,以了解删除的顺序,这可能有助于调试
发布于 2020-01-24 03:07:16
感谢大家关注这篇文章,我想这是正确的代码
public class WhoIsElected {
public static int whoIsElected(int n, int k) {
/*******************************************start solution*******************************/
LinkedList<Integer> circle = new LinkedList<>();
for(int i=1;i<=n;i++)
{
circle.add(i);
}
int posToDel = k;
if(n==1)
{
return 1;
}
if(posToDel>n)
{
posToDel=posToDel%n;
}
while(circle.size()>1)
{
if(posToDel==0)// becasue the posToDel-1 when posToDel ==0 is the last index
{
circle.remove(n-1);
}
else
{
circle.remove(posToDel-1);
}
n--; // decrease the length of n by 1 when u delete one from the list
// after removing the posToDel in circle
if(posToDel>0)
{
posToDel=posToDel-1;
}
if(posToDel+k>n)
{
posToDel=(posToDel+k)%n;
}
else
{
posToDel=posToDel+k;
}
}
return circle.get(0);
/*******************************************end solution*******************************/
}
/**
* bool doTestsPass()
* Runs various tests. Returns true if tests pass. Otherwise,
* returns false.
*/
public static boolean doTestsPass() {
// todo: implement more tests, please
// feel free to make testing more elegant
// test cases are structered as {n, k, expected answer}
int[][] testCases = {
{1, 1, 1},
{2, 2, 1},
{2, 5, 2},// my add test to have k bigger than n
{100, 2, 73},
{4,2,1},
{5, 3, 4},
{6, 4, 5},
{12, 4, 1},
{12, 3, 10},
{1000,5,763}
};
for (int[] testCase : testCases) {
int answer = whoIsElected(testCase[0], testCase[1]);
if (answer != testCase[2]) {
System.out.println("test failed!");
System.out.printf("n:%d, k%d, answer got: %d, should be: %d\n", testCase[0], testCase[1], answer, testCase[2]);
return false;
}
}
System.out.println("All tested passed");
return true;
}
/**
* Execution entry point.
*/
public static void main(String args[]) {
doTestsPass();
}
class Node {
}}
https://stackoverflow.com/questions/59870823
复制相似问题