由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一个stack怎么sort
相关主题
求推荐学习recursive 算法的资料求问把二叉树的recursive遍历改为stack实现的思路
找2个sorted array中的第K小的元素,有O(lgn)方法吗?求教一个combination的问题,求好方法
请问一个简单的面试题linked list排序的算法除了bubble
LCA复杂度是多少请教recursive backtracking问题的时间复杂度的分析
算法空间复杂度的小白问题[ 每日一课] Sort List
请教个问题"简单的"linklist的问题
反省了一下面试题目都答对但是还是没offer的原因。。。有人同看Populating Next Right Pointers in Each Node II的recursive写法么?
Interview question::判断一个linked list是不是palindrome
相关话题的讨论汇总
话题: sort话题: stack话题: insert话题: temp话题: st
进入JobHunting版参与讨论
1 (共1页)
P*******b
发帖数: 1001
1
怎么recursion?
r****o
发帖数: 1950
2
为啥一定要recursion呢?这题时间复杂度和空间复杂度有什么要求?

【在 P*******b 的大作中提到】
: 怎么recursion?
P*******b
发帖数: 1001
3
就是要求不要extra的stack

【在 r****o 的大作中提到】
: 为啥一定要recursion呢?这题时间复杂度和空间复杂度有什么要求?
j**l
发帖数: 2911
4
想必是用到系统的stack?
f*********5
发帖数: 576
5
这样可以不?
void sort(stack &st)
{
if (st.IsEmpty())return;
int temp=st.top();
st.pop();
sort(st);
Insert(st,temp); //insert temp to the right position in st
//here we can use a list to temporarily store
//the nodes whose value less than temp
}

【在 P*******b 的大作中提到】
: 怎么recursion?
K******g
发帖数: 1870
6
感觉你这个仍然是在insert 排序,O(N^2)。那recursive有什么必要呢
直接挨个pop,insert不就好了?

【在 f*********5 的大作中提到】
: 这样可以不?
: void sort(stack &st)
: {
: if (st.IsEmpty())return;
: int temp=st.top();
: st.pop();
: sort(st);
: Insert(st,temp); //insert temp to the right position in st
: //here we can use a list to temporarily store
: //the nodes whose value less than temp

y*c
发帖数: 904
7

这个是对的,然后在写一个recursive的insert, which maintains order. The
structure is similar to this sort.

【在 f*********5 的大作中提到】
: 这样可以不?
: void sort(stack &st)
: {
: if (st.IsEmpty())return;
: int temp=st.top();
: st.pop();
: sort(st);
: Insert(st,temp); //insert temp to the right position in st
: //here we can use a list to temporarily store
: //the nodes whose value less than temp

f*********5
发帖数: 576
8
wait
st已经是排序的了
insert不需要递归了,pop出小于temp的,然后插入temp,插入那些小于的,就OK

【在 y*c 的大作中提到】
:
: 这个是对的,然后在写一个recursive的insert, which maintains order. The
: structure is similar to this sort.

y*c
发帖数: 904
9

That's right. 不过一般需要system stack (recursion), 不是另外搞一个stack。

【在 f*********5 的大作中提到】
: wait
: st已经是排序的了
: insert不需要递归了,pop出小于temp的,然后插入temp,插入那些小于的,就OK

1 (共1页)
进入JobHunting版参与讨论
相关主题
判断一个linked list是不是palindrome算法空间复杂度的小白问题
G电面请教个问题
Print a binary tree in level order but starting from leaf node up to root反省了一下面试题目都答对但是还是没offer的原因。。。
也问两个算法题Interview question::
求推荐学习recursive 算法的资料求问把二叉树的recursive遍历改为stack实现的思路
找2个sorted array中的第K小的元素,有O(lgn)方法吗?求教一个combination的问题,求好方法
请问一个简单的面试题linked list排序的算法除了bubble
LCA复杂度是多少请教recursive backtracking问题的时间复杂度的分析
相关话题的讨论汇总
话题: sort话题: stack话题: insert话题: temp话题: st