当前位置:网站首页>Joseph Ring problem
Joseph Ring problem
2022-07-21 00:14:00 【N. LAWLIET】
problem
Given a chain header node head, And a positive number m
Start from scratch , Count to every time m Kill the current node
Then the next node of the killed node starts from 1 Start counting again ,
Go round and round until there is only one node left , Return the last node .
Example
A little
thought
Recurrence of violence , This type of topic belongs to periodic function , When you encounter this kind of problem, you can use y = x %i How to do it
Code
public static Node josephusKill1(Node head, int m) {
if(head == null || head.next == head||m<1) {
return head;
}
Node cur = head;
int size = 0;
while(cur != null) {
size++;
cur = cur.next;
}
int live = getLive(size,m);
while(--live>=0) {
head = head.next;
}
head.next = head;
return head;
}
// As long as there is a razor feeling, the function starts from y = x % i Start ( Regular slash )
public static int getLive(int size, int m) {
if(size == 1) {
return 1;
}
return ((getLive(size-1, m) + m-1) % size) + 1;
}
public static class Node{
Node next;
int val;
public Node(int val ) {
this.val = val;
}
}
边栏推荐
- Windows11 install MySQL 5.7 X nanny graphic tutorial
- If paging by frame fails - solution
- el-cascader 级联选择器动态加载数据及回显数据方法(最全概括)<grootbaby>
- Software performance test plan - performance test preparation
- Portuguese financial Vocabulary Translation
- 外表简单内里复杂的功能测试,如何进行?
- 关于业务安全平台架构设计,顶象给“我”讲透了
- 144. 二叉树的前序遍历
- MySQL 5.7 is about to stop and only maintain. It's time to learn a wave of MySQL 8
- The idea version of postman has been released, and its functions are really powerful
猜你喜欢
没有了可用Task slot,Flink新增任务会怎样?
关于我写书这件事
It's just a TCC distributed transaction. Is it so difficult?
Helm introduction
Maintainability of data intensive application of reading notes
Unable to install cloudera manager agent
Software performance test plan - performance test preparation
Siemens low code customer case | overcome communication barriers and solve the bottleneck of application development efficiency
Yolov5 trains its own VOC dataset
FAQ丨构建业务安全平台架构,你想要的答案都在这里!
随机推荐
FAQ - build a business security platform architecture. Here are all the answers you want!
Helm — Chart介绍
Helm 介绍
Yolov5 trains its own VOC dataset
Perfect integration into cloud native codeless platform IVX editor practice
测试/开发程序员小张相亲记......
It's just a TCC distributed transaction. Is it so difficult?
Golang — RESTful框架 go-restful
Without available task slots, what will happen to Flink's new tasks?
IDEA版Postman面世了,功能真心强大
Centos8 install MySQL
魔王冷饭||#103 魔王看经济;烂尾楼、租房附加条款、周易和水库优质男
外表简单内里复杂的功能测试,如何进行?
GIS技术在医疗行业的应用:利用切片地图发布技术解决dmetrix数字病理切片在线浏览
Awk statistical average max min
Implementation of imx8mp kdump function
字節一面掛了,面試官問DDD,我卻不知道
动态线段树leetcode.731
TiKV & TiFlash 加速复杂业务查询
COLA 4.0 - DDD项目实践