avatar
重口味手语书# Joke - 肚皮舞运动
a*e
1
这些题看着容易,老是写不对或者代码很冗长,有大牛们指点一下如何提高么?
呜呜,不知道哪年哪月才能刷完一遍,有同学互勉一下么?
比如
Given a linked list and a value x, partition it such that all nodes less
than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the
two partitions.
For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
我的
Runtime Error

Last executed input:
{1}, 0
ListNode *partition(ListNode *head, int x) {
if(head==NULL)
return head;
ListNode *dummy = new ListNode(0);
dummy->next = head;

ListNode *cur = head->next;
ListNode *curHead = head;
ListNode *prev = head;
bool flag = false;
while(curHead!=NULL||cur!=NULL||prev!=NULL){
if ((cur->valval>x)){
cur = cur?cur->next:NULL;
curHead = curHead->next;
prev = prev->next;
}
else if (flag==false&&cur->val>=x){
flag=true;
prev = prev->next;
cur = cur?cur->next:NULL;
}
else if (flag==true&&cur->valprev->next = cur->next;
cur->next = curHead->next;
curHead->next = cur;
curHead = cur;
}
else{
cur = cur?cur->next:NULL;
curHead = curHead->next;
prev = prev->next;
}
}
return dummy->next;
}
看到别的答案,
ListNode* partition(ListNode* head, int x) {
ListNode left_dummy(-1); // 􀹨􀢲􀗄
ListNode right_dummy(-1); // 􀹨􀢲􀗄
auto left_cur = &left_dummy;
auto right_cur = &right_dummy;
for (ListNode *cur = head; cur; cur = cur->next) {
if (cur->val < x) {
left_cur->next = cur;
left_cur = cur;
} else {
right_cur->next = cur;
right_cur = cur;
}
}
left_cur->next = right_dummy.next;
right_cur->next = nullptr;
return left_dummy.next;
avatar
N*D
2
有点害怕辐射。。。
avatar
x*o
3
h
avatar
r*k
4
扫描一遍链表
整两个新链表
每个点跟x比,比x大放一个,小于放另一个
最后把这两个链表接起来就行了
记得要把拿出来的node的next置成null
否则容易有环

the

【在 a***e 的大作中提到】
: 这些题看着容易,老是写不对或者代码很冗长,有大牛们指点一下如何提高么?
: 呜呜,不知道哪年哪月才能刷完一遍,有同学互勉一下么?
: 比如
: Given a linked list and a value x, partition it such that all nodes less
: than x come before nodes greater than or equal to x.
: You should preserve the original relative order of the nodes in each of the
: two partitions.
: For example,
: Given 1->4->3->2->5->2 and x = 3,
: return 1->2->2->4->3->5.

avatar
L*H
5
买个啥计数器测测,嘿嘿
avatar
M*a
6
已经刷完了。可是很多又忘了。
avatar
N*D
7
明天就去测量。。。
avatar
r*k
8
同感

【在 M**a 的大作中提到】
: 已经刷完了。可是很多又忘了。
avatar
t*8
9
辐射过的有德味,值钱
avatar
h*e
10
这就达到张三丰教张无忌耍剑的境界了。

【在 M**a 的大作中提到】
: 已经刷完了。可是很多又忘了。
avatar
t*f
11
By a new roll of ISO3200 BW film, leave ti3 and the film roll
together for a month or so. (Put some small pieces of lead between
them, better yet). Without shooting the film, develop and check
if there are any sign of exposure.

【在 N**D 的大作中提到】
: 有点害怕辐射。。。
avatar
g*5
12
非常艰难的刷了20几道题,早忘了,更别提后面的没做过的了
avatar
N*D
13
done. no radiation detected.:-)

【在 N**D 的大作中提到】
: 明天就去测量。。。
avatar
a*n
14
链表题目就是考细心、考corner case、考指针来回穿插衔接的。静下心做就好。
avatar
s*s
15
盖盖子曝光试试 亮点太多的话。。。
avatar
y*n
16
数据结构都还好,最搞的就是Array的题,变化太多,有时候很难想到好的答案。。
avatar
x5
17
买含铅的油漆刷一下

【在 N**D 的大作中提到】
: 有点害怕辐射。。。
avatar
a*e
18
多谢各位,但现在试着在纸上跑程序,但有些就是搞不清哪里错了,比如这个
Copy List with Random Pointer
􀫭􀶍
A linked list is given such that each node contains an additional random
pointer which could point to
any node in the list or null.
Return a deep copy of the list.
􀙳􀼅
我看了idea,自己写了如下程序,但就是
Submission Result: Runtime Error
Last executed input:
{-1,#}
看了半天,试着一步一步走进去,还是不知道哪里错了,打算在VS里面debug一下看。
觉得和能通过的答案没有多少区别啊。。。。。。。。
My wrong answer.
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (head==NULL) return NULL;
RandomListNode *cur = head;
while(cur!=NULL){
RandomListNode *copied = new RandomListNode(cur->label);
copied->next = cur->next;
cur->next = copied;
cur = copied->next;
}
cur=head;

while(cur!=NULL){
if(cur->random!=NULL){
cur->next->random = cur->random->next;
}
cur = cur->next->next;
}
RandomListNode *newHead = head->next;
cur = newHead;
RandomListNode *oc = head;

while(cur){
oc->next = oc->next->next;
oc = oc->next;
if (cur->next){
cur->next = cur->next->next;
cur = cur->next;
}
}
return newHead;
}
};
Others' correct answer
for (RandomListNode* cur = head; cur != nullptr; ) {
RandomListNode* node = new RandomListNode(cur->label);
node->next = cur->next;
cur->next = node;
cur = node->next;
}
for (RandomListNode* cur = head; cur != nullptr; ) {
if (cur->random != NULL)
cur->next->random = cur->random->next;
cur = cur->next->next;
}
// 􀙳􀒷􀨆􀛱􀖆􀧽􀑖
RandomListNode dummy(-1);
for (RandomListNode* cur = head, *new_cur = &dummy;
cur != nullptr; ) {
new_cur->next = cur->next;
new_cur = new_cur->next;
cur->next = cur->next->next;
cur = cur->next;
}
return dummy.next;
avatar
N*D
19
smart...

【在 s**********s 的大作中提到】
: 盖盖子曝光试试 亮点太多的话。。。
avatar
a*e
20
多谢各位,但现在试着在纸上跑程序,但有些就是搞不清哪里错了,比如这个
Copy List with Random Pointer
􀫭􀶍
A linked list is given such that each node contains an additional random
pointer which could point to
any node in the list or null.
Return a deep copy of the list.
􀙳􀼅
我看了idea,自己写了如下程序,但就是
Submission Result: Runtime Error
Last executed input:
{-1,#}
看了半天,试着一步一步走进去,还是不知道哪里错了,打算在VS里面debug一下看。
觉得和能通过的答案没有多少区别啊。。。。。。。。
My wrong answer.
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (head==NULL) return NULL;
RandomListNode *cur = head;
while(cur!=NULL){
RandomListNode *copied = new RandomListNode(cur->label);
copied->next = cur->next;
cur->next = copied;
cur = copied->next;
}
cur=head;

while(cur!=NULL){
if(cur->random!=NULL){
cur->next->random = cur->random->next;
}
cur = cur->next->next;
}
RandomListNode *newHead = head->next;
cur = newHead;
RandomListNode *oc = head;

while(cur){
oc->next = oc->next->next;
oc = oc->next;
if (cur->next){
cur->next = cur->next->next;
cur = cur->next;
}
}
return newHead;
}
};
Others' correct answer
for (RandomListNode* cur = head; cur != nullptr; ) {
RandomListNode* node = new RandomListNode(cur->label);
node->next = cur->next;
cur->next = node;
cur = node->next;
}
for (RandomListNode* cur = head; cur != nullptr; ) {
if (cur->random != NULL)
cur->next->random = cur->random->next;
cur = cur->next->next;
}
// 􀙳􀒷􀨆􀛱􀖆􀧽􀑖
RandomListNode dummy(-1);
for (RandomListNode* cur = head, *new_cur = &dummy;
cur != nullptr; ) {
new_cur->next = cur->next;
new_cur = new_cur->next;
cur->next = cur->next->next;
cur = cur->next;
}
return dummy.next;
avatar
R*a
21
吃点儿铅最好了

【在 x5 的大作中提到】
: 买含铅的油漆刷一下
avatar
a*e
22
Ft, 用VS不到半分钟就发现问题。。。。。。。。。。。。
// while(cur){
while(cur&&oc){ //加这个check就行了
oc->next = oc->next->next;
oc = oc->next;
if (cur->next){
cur->next = cur->next->next;
cur = cur->next;
}
}
但这段程序就不用那么多check
// 􀙳􀒷􀨆􀛱􀖆􀧽􀑖
RandomListNode dummy(-1);
for (RandomListNode* cur = head, *new_cur = &dummy;
cur != nullptr; ) {
new_cur->next = cur->next;
new_cur = new_cur->next;
cur->next = cur->next->next;
cur = cur->next;
}
avatar
l*a
23
注意别吃铅笔

【在 R***a 的大作中提到】
: 吃点儿铅最好了
avatar
i*t
24
画图,dummy头指针很handy,大多数用recursion和双指针
avatar
x5
25
会拉出来的,那得连续吃,麻烦

【在 R***a 的大作中提到】
: 吃点儿铅最好了
avatar
p*u
26
翻来覆去刷
avatar
N*D
27
good idea but takes time.

【在 t*******f 的大作中提到】
: By a new roll of ISO3200 BW film, leave ti3 and the film roll
: together for a month or so. (Put some small pieces of lead between
: them, better yet). Without shooting the film, develop and check
: if there are any sign of exposure.

avatar
m*3
28
runtime error一般都是访问了不该访问的memory,比如数组越界,指针访问错误, 你
自己把程序拿出来在自己机器上跑跑看

【在 a***e 的大作中提到】
: 多谢各位,但现在试着在纸上跑程序,但有些就是搞不清哪里错了,比如这个
: Copy List with Random Pointer
: 􀫭􀶍
: A linked list is given such that each node contains an additional random
: pointer which could point to
: any node in the list or null.
: Return a deep copy of the list.
: 􀙳􀼅
: 我看了idea,自己写了如下程序,但就是
: Submission Result: Runtime Error

avatar
N*D
29
不好买。kaka

【在 x5 的大作中提到】
: 买含铅的油漆刷一下
avatar
l*i
30
推荐Sedgewick的algorithm in C,看相应章节,理解他的写法就好了。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。