编号为 1 到 n 的 n 个人围成一圈。从编号为 1 的人开始报数,报到 m 的人离开。
下一个人继续从 1 开始报数。
n-1 轮结束以后,只剩下一个人,问最后留下的这个人编号是多少?
数据范围:
进阶:空间复杂度
,时间复杂度
5,2
3
开始5个人 1,2,3,4,5 ,从1开始报数,1->1,2->2编号为2的人离开 1,3,4,5,从3开始报数,3->1,4->2编号为4的人离开 1,3,5,从5开始报数,5->1,1->2编号为1的人离开 3,5,从3开始报数,3->1,5->2编号为5的人离开 最后留下人的编号是3
1,1
1
struct ListNode* BuyNode(int x)
{
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
if(node == NULL)
{
exit(1);
}
node->val = x;
node->next = NULL;
return node;
}
//创建循环链表
struct ListNode* createCircle(int n)
{
struct ListNode* phead, *ptail;
phead = ptail = BuyNode(1);
//创建链表
for(int i = 2; i <= n; i++)
{
ptail->next = BuyNode(i);
ptail = ptail->next;
}
//尾节点连接头节点
ptail->next = phead;
return ptail;
}
int ysf(int n, int m ) {
// write code here
//创建循环链表
struct ListNode* prev = createCircle(n);
struct ListNode* pcur = prev->next;
int count = 1; //count需要从一开始数
//遍历链表
while(prev->next != prev)
{
if(count == m)
{
prev->next = pcur->next;
free(pcur);
pcur = prev->next;
count=1;
}
else {
prev = pcur;
pcur = pcur->next;
count++;
}
}
//释放申请的节点空间
int ret = pcur->val;
free(pcur);
pcur = NULL;
return ret;
} typedef struct ListNode ListNode;
// 创建节点
ListNode* ListBuy(int n)
{
ListNode* mode = (ListNode*)malloc(sizeof(ListNode));
if(mode == NULL)
{
exit(1);
}
mode->val = n;
mode->next = NULL;
return mode;
}
// 创建环形链表
ListNode* CreatCircle(int n)
{
// 创建头节点
ListNode* phead = ListBuy(1);
// 创建尾节点
ListNode* ptail = phead;
for(int i =2; i<=n; i++){
ptail->next=ListBuy(i);
ptail=ptail->next;
}
// 首尾相连
ptail->next=phead;
return ptail;
}
int ysf(int n, int m ) {
// write code here
// 创建环形链表
ListNode* prev = CreatCircle(n);
ListNode* pcur = prev->next;
// 计数
int count = 1;
while(prev->next != prev)
{
if(count == m)
{
// 删除节点
prev->next=pcur->next;
free(pcur);
pcur=prev->next;
count=1;
}
else {
prev=pcur;
pcur=pcur->next;
count++;
}
}
return pcur->val;
} typedef struct ListNode LN;
struct ListNode* creatNode(int n)
{
LN* newNode=(LN*)malloc(sizeof(LN));
newNode->val=n;
newNode->next=NULL;
return newNode;
}
LN* creatCircle(int n)
{
LN*phead=creatNode(1);//创建第一个节点
LN*pcur=phead;
int i=2;
LN*newNode;
while(i<=n)
{
newNode=creatNode(i++);
pcur->next=newNode;
pcur=pcur->next;
//newNode->next=NULL;
}
pcur->next=phead;
return phead;
}
int ysf(int n, int m ) {
// write code here
int count=0;//报数的序号
int peopleCount=n;
LN*head=creatCircle(n);//创建环形链表
LN*pcur=head;
LN*prev=head;
while(prev->next->val!=1)//找到尾节点
{
prev=prev->next;
}
while(1)
{
count++;
if(count==m)//报到m删除节点
{
if(peopleCount==2)
{
return prev->val;
}
prev->next=pcur->next;
count=0;
peopleCount--;
pcur=pcur->next;
}
else
{
pcur=pcur->next;
prev=prev->next;
}
}
} #include <stdlib.h>
typedef struct ListNode ListNode ;
//创建节点
ListNode* CreatNode(int x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
newnode->val = x;
newnode->next = NULL;
return newnode;
}
//创建链表
ListNode* CreatList(int n)
{
//先创建头节点
ListNode* head = CreatNode(1);
ListNode* ptail = head;
//循环创建剩余节点,连接至已有节点后
for (int i=2; i<=n; i++) {
ListNode* newnode = CreatNode(i);
ptail->next = newnode;
//尾节点后移
ptail = ptail->next;
}
//将链表连接成环
ptail->next = head;
//return head;
return ptail; //为防止prev->next为空
}
int ysf(int n, int m ) {
//创建循环链表
ListNode* prev = CreatList(n);
ListNode* pcur = prev->next;
//ListNode* prev = NULL;
int count = 1;
//若节点个数大于1,继续执行删除
while(pcur->next != pcur)
{
//逢m就删除
if(count == m)
{
prev->next = pcur->next;
free(pcur);
pcur = prev->next;
//重新再喊
count = 1;
}
else
{
//不是m,继续喊
prev = pcur;
pcur = pcur->next;
count++;
}
}
return pcur->val;
} /**
*
* @param n int整型
* @param m int整型
* @return int整型
*/
int ysf(int n, int m ) {
// write code here
struct ListNode *head = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *p = head;
head->val = 1;
for (int i = 2; i <= n; i++) {
struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));
p->next = node;
p = node;
p->val = i;
}
p->next = head;
for (int i = 1; i < n; i++) {
for (int j = 1; j < m - 1; j++) {
head = head->next;
}
struct ListNode *q = head->next;
head->next = q->next;
free(q);
head = head->next;
}
return head->val;
}