由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 约瑟夫问题 用循环链表算法 时间 复杂度多少
相关主题
java 链表里面dummy node 一问?谢谢leetcode上这个链表节点的定义是什么意思?ListNode(int x) : val(x), next(NULL) {}
大牛们帮忙,Rverse Nodes in k-Group弱问题,连反转链表都看不懂了
leetcode 关于Partition Listf 的面经
请大牛review一下这个Insertion Sort List的解法链表插入排序都写了一个小时,对人生失去信心了。
[ 每日一课] Sort ListPURE 储存 OA
lc 上面4 sum的时间复杂度要求多少?两个链表怎么查找相交点?
请教下3sum为撒超时问个面试题目
[leetcode] merge k lists 求助On-Site求祝福+电面一两题-Update-拒了-题
相关话题的讨论汇总
话题: listnode话题: int话题: head话题: solution话题: val
进入JobHunting版参与讨论
1 (共1页)
f****e
发帖数: 923
1
class ListNode{
int val;
ListNode next;
ListNode(int val){
this.val = val;
}
}
class Solution{
public int live(int n, int k){
ListNode head = new ListNode(1);
ListNode node = head;
for(int i = 2; i <= n; i++){
node.next = new ListNode(i);
node = node.next;
}
node.next = head;
System.out.println("before remove");
printList(head);
System.out.println("removing the nodes");
while(head.next != head){
int count = 1;
while(count < k - 1){
head = head.next;
count++;
}
System.out.print(head.next.val + "->");
head.next = head.next.next;
head = head.next;
}
System.out.println();
System.out.println("after remove");
return head.val;
}
public void printList(ListNode head){
if (head != null){
ListNode temp = head;
do{
System.out.print(temp.val + " ");
temp = temp.next;
} while (temp != head);
}
System.out.println();
}
public static void main(String[] args){
Solution s = new Solution();
int n = 5;
int k = 2;
System.out.println(s.live(n, k));
int n1 = 7;
int k1 = 3;
System.out.println(s.live(n1, k1));
int n2 = 14;
int k2 = 2;
System.out.println(s.live(n2, k2));
}
}
r*****s
发帖数: 1815
2
循环最差就是n^2,会超时
必须要用动态规划啊
y**********u
发帖数: 2839
W***o
发帖数: 6519
4
红狗哥有没有poj的全套答案啊?
这题有意思

【在 y**********u 的大作中提到】
: http://poj.org/problem?id=1012
: 加油

y**********u
发帖数: 2839
5
都是靠google……,等我到了1000就把我的git repo share给各位基友

【在 W***o 的大作中提到】
: 红狗哥有没有poj的全套答案啊?
: 这题有意思

z*********n
发帖数: 1451
6
约瑟夫问题有数学地推式的。
o*******r
发帖数: 73
7
记得我以前回复过。
public class Solution {
public int lastStand(int n, int m) {
int ret = 0;
for (int i = 2; i <= n; i++) {
ret = (ret + m) % i;
}
return (ret + 1);
}
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
On-Site求祝福+电面一两题-Update-拒了-题[ 每日一课] Sort List
求问两题思路lc 上面4 sum的时间复杂度要求多少?
【我自己写的LinkedList为什么总有错?】请教下3sum为撒超时
请教一道单链表问题[leetcode] merge k lists 求助
java 链表里面dummy node 一问?谢谢leetcode上这个链表节点的定义是什么意思?ListNode(int x) : val(x), next(NULL) {}
大牛们帮忙,Rverse Nodes in k-Group弱问题,连反转链表都看不懂了
leetcode 关于Partition Listf 的面经
请大牛review一下这个Insertion Sort List的解法链表插入排序都写了一个小时,对人生失去信心了。
相关话题的讨论汇总
话题: listnode话题: int话题: head话题: solution话题: val