l*****a 发帖数: 14598 | 1 你们非用for loop把三种情况混在一起,感觉不好。
如下是不是会清晰易懂一点呢
if(num[0]!=start) print(start,num[0]-1);
int cur=0;
while(cur
if(num[cur]!=num[cur+1]-1) print(num[cur]+1,num[cur+1]-1);
cur++;
}
if(num[num.length-1]!=end) print(num[num.length-1]+1,end); |
|
p*********g 发帖数: 116 | 2 给你一个这个吧, 两个变量的做不出。
public static int maxOnes3(int[] A) {
if (A == null || A.length == 0)
return 0;
int max = 0;
int last = (A[0] == 0 ? 1 : 0);
int cur;
int num = A[0];
for (int i = 1; i < A.length; i++) {
num += A[i];
cur = last + (A[i] == 0 ? 1 : -1);
if (cur < 0)
cur = 0;
if (cur > max) {
max = cur;
}
last = cur;
}
... 阅读全帖 |
|
o********r 发帖数: 79 | 3 不要吵了。
我按beanbun的思路写了一遍,指教一下
typedef pair COORD;
void extend(COORD cur,
vector >& visited,
vector > matrix,
queue& current,
queue& next )
{
vector shift;
shift.push_back(make_pair(0,-1));
shift.push_back(make_pair(0,1));
shift.push_back(make_pair(1,0));
const int m = matrix.size();
const int n = matrix[0].size();
for(const auto s:shift)
... 阅读全帖 |
|
d**********o 发帖数: 279 | 4 没想出怎么用stack, 大牛讲解一下?
"""
Write a iterator to iterate a nested array.
For example, for given array: [1, 2, [3, [4, 5], [6, 7], 8], 9, 10]
call iterator.next() 10 times should return 1,2,3,4,5,6,7,8,9,10.
"""
def traverse(l):
if not l:
return
for i in l:
if isinstance(i, list):
traverse(i)
else:
print i
class NestIterator(object):
def __init__(self, l):
self.l = l
self.cur = -1
self.iterator = None
def next(self):
if self.iterator and s... 阅读全帖 |
|
b******b 发帖数: 713 | 5 psudo code:
List> result = new ArrayList<>();
List> inProgress = new ArrayList<>();
inProgress.add(new ArrayList<>(root));
while(!inProgress.isEmpty())
{
List cur = inProgress.remove(0);
if (cur.left == null && cur.right == null)
{
result.add(cur);
continue;
}
if (cur.left != null)
{
List newLeft = new List(cur);
newLeft.add(cur.left);
inProgress.add(newLeft);
}
//do the same for the right
}
ret... 阅读全帖 |
|
d********e 发帖数: 321 | 6 最近遇到next lowest的题,我写了下面这个inorder traversal (largest ->
smallest)的代码,但是对方坚持让我写 O(lg N),但是TreeNode 只有 left, right
pointer,没有parent pointer, 我觉得不可能啊,大牛有啥好办法吗?
public static Integer findNextLowest(TreeNode root, int target) {
Stack stack = new Stack<>();
TreeNode cur = root;
while (cur != null || stack.size() > 0) {
while (cur != null) {
stack.push(cur);
cur = cur.right;
}
TreeNode node = stack.pop(... 阅读全帖 |
|
d********e 发帖数: 321 | 7 【 以下文字转载自 JobHunting 讨论区 】
发信人: dilettante (半瓶醋), 信区: JobHunting
标 题: BST查找next lowest 可以达到 O(lg N)?
发信站: BBS 未名空间站 (Thu May 25 16:27:02 2017, 美东)
最近遇到next lowest的题,我写了下面这个inorder traversal (largest ->
smallest)的代码,但是对方坚持让我写 O(lg N),但是TreeNode 只有 left, right
pointer,没有parent pointer, 我觉得不可能啊,大牛有啥好办法吗?
public static Integer findNextLowest(TreeNode root, int target) {
Stack stack = new Stack<>();
TreeNode cur = root;
while (cur != null || stack.size() > 0) {
... 阅读全帖 |
|
l****c 发帖数: 782 | 8 vector > levelOrderBottom(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector tmp;
stack> result_stack;
tmp.push_back(root);
while(tmp.size()!=0) {
result_stack.push(tmp);
vector cur = tmp;
tmp.clear();
vector::iterator it;
for (it = cur.begin(); it < cur.end(); it++) {
if ((*it)->left!=NULL)
... 阅读全帖 |
|
j********x 发帖数: 2330 | 9 写了一个小时,没考虑特别的corner case,O(num of intervals of "except"),用了
上面提到的binary search:
#include
#include
#include
#include
#include
struct compareable_interval {
int start;
int end;
int real_start;
int real_end;
compareable_interval(int lhs, int rhs) : start(lhs), end(rhs), real_
start(lhs), real_end(rhs) {
}
compareable_interval(int l, int r, int rl, int rr) : start(l), end(r),
real_start(rl), real_end(rr) {
}
b... 阅读全帖 |
|
j********x 发帖数: 2330 | 10 写了一个小时,没考虑特别的corner case,O(num of intervals of "except"),用了
上面提到的binary search:
#include
#include
#include
#include
#include
struct compareable_interval {
int start;
int end;
int real_start;
int real_end;
compareable_interval(int lhs, int rhs) : start(lhs), end(rhs), real_
start(lhs), real_end(rhs) {
}
compareable_interval(int l, int r, int rl, int rr) : start(l), end(r),
real_start(rl), real_end(rr) {
}
b... 阅读全帖 |
|
c****9 发帖数: 164 | 11 刚刚写的,不知道算是DP还是recursive,基本上是brute force
bool checkpos(vector& cur, int pos)
{
for(int i=0;i
{
for(int j=0;j
{
if(cur[i][j] =='Q')
{
if(j==pos)
{
return false;
}
else if(i+j == pos + cur.size())
{
return false;
... 阅读全帖 |
|
l*******b 发帖数: 2586 | 12 大家看看有没有bug, 初步试了两个好像是对的
#include
using namespace std;
bool gt (int a, int b) { return a > b; }
bool lt(int a, int b) { return a < b; }
bool eq(int a, int b) { return a == b; }
bool ge(int a, int b) { return a >= b; }
bool le(int a, int b) { return a <= b; }
int search(int A[], int N, int key, bool (*f) (int, int)) {
int dir = f(A[0], A[N-1]) ? -1 : 1; // direction of the search
int cur = (dir == 1) ? 0 : N - 1;
while(!f(A[cur], key)) {
cur += dir;
if(cur < 0... 阅读全帖 |
|
a*****a 发帖数: 46 | 13 我觉得一遍就可以啊。
保存上一个小于9的节点last,每次相加,以和的个位数做新节点,如果有进位就增加
last.value,如果新节点小于9就把last移到新节点。
可以这样做的原因是,每一个节点只存一个数字,那么任何一位如果需要进位,个位数
最多为8,无论后面的数是什么都不会再进位。
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0), last = dummy, cur = dummy;
while (l1 != null && l2 != null) {
int sum = l1.value + l2.value;
cur.next = new ListNode(sum % 10);
cur = cur.next;
l1 = l1.next;
l2 = l2.next;
if (sum / 10 > 0) { // has carry
... 阅读全帖 |
|
r**h 发帖数: 1288 | 14 就是循环后序遍历呀
if(prev==NULL || prev->left==cur || prev->right==cur){
if(cur->left != NULL){
st.push(cur->left); <==============
}
else if(cur->right != NULL){
st.push(cur->right); <==============
}
}
大概像这样
箭头指着的地方换成一开始的写法就不行了 |
|
c********p 发帖数: 1969 | 15 我的跑不过的。。。
import java.util.*;
public class Solution {
public ArrayList> findLadders(String start, String end
, HashSet dict) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList> ret = new ArrayList>(
);
if(dict.isEmpty()){
return ret;
}
int len = 1;
int len2 = 0;
Queue q = new LinkedList();
... 阅读全帖 |
|
s*****4 发帖数: 25 | 16 I wrote the code based on LZ's description.
Do you guys think this is correct?
Any suggestion/improvement is appreciated!
class HitsCounter {
private:
vector hitcnt; // stores the hit for each second
long long total; // always keeps the current hit count
long long last; // the time of last hit
int size; // size of hitcnt array, for this question set to
300
public:
HitsCounter(int secondCnt) {
// in this case, secondCnt shoule be 300
hitcn... 阅读全帖 |
|
s*****4 发帖数: 25 | 17 I wrote the code based on LZ's description.
Do you guys think this is correct?
Any suggestion/improvement is appreciated!
class HitsCounter {
private:
vector hitcnt; // stores the hit for each second
long long total; // always keeps the current hit count
long long last; // the time of last hit
int size; // size of hitcnt array, for this question set to
300
public:
HitsCounter(int secondCnt) {
// in this case, secondCnt shoule be 300
hitcn... 阅读全帖 |
|
f**********t 发帖数: 1001 | 18 // 一个deque,但是只支持pushBack,pushFront,popBack, 没有popFront
// 给一个1-N的排列,问能否把1-N按照从小到大的顺序push到deque,pop的时机可以
任选
// ,使得pop出的顺序刚好是给定的排列
// 比如: 给定 23145, 那么对12345对应的操作序列是pushBack,pushBack,
popBack,
// pushBack,popBack,popBack,pushBack,popBack,pushBack,popBack
// 要求如果可能,输出任意一个可能的操作序列,如果没有可能的操作序列,输出
// impossible
bool OpSequense(vector vi) {
deque dq;
size_t cur = 0;
vector indexes(vi.size() + 1);
for (size_t i = 0; i < vi.size(); ++i) {
indexes[vi[i]] = i;
}
for (int i =... 阅读全帖 |
|
b*****a 发帖数: 1 | 19 来自主题: JobHunting版 - f家店面题 好像是 Jump Game II 稍加改动, 楼主的case可以过
int jump(int* nums, int numsSize)
{
if (!nums || !numsSize) return -1;
else
{
int last = 0;
int cur = 0;
int jumps = 0;
int speed = 1;
int i;
for (i = 0; i < numsSize; i++)
{
if (i > last)
{
last = cur;
jumps++;
speed++;
}
if (nums[i] && (nums[i + speed] || (i + speed > numsSize - 1)... 阅读全帖 |
|
P**********k 发帖数: 1629 | 20 void find_longest_substring(string str, int n, int& start_best, int& end_
best){
string pool (2, ' ');
int pool_size = 0;
int start, shift, end;
char cur;
int length_max=0, length_cur;
start=0;
pool[0]=str[start];
pool_size++;
for(int i=1; i
if(str[i]!=str[i-1]){
cur = str[i];
if(pool_size<2 && pool[0]!= cur){
pool[pool_size] = cur;
pool_size++;
shift = i;
... 阅读全帖 |
|
S***w 发帖数: 1014 | 21 String balance(String s) {
int start = 0, i = 0;
Stack stack = new Stack();
String cur = "", ret = "";
while (i < s.length()) {
if (s.charAt(i) == '(') {
stack.push(i);
i = i + 1;
}
else {
if (stack.isEmpty()) {
ret += cur;
i = i + 1;
start = i;
cur = "";
}
... 阅读全帖 |
|
f****e 发帖数: 923 | 22 网上的搜索的众说纷纭
https://goo.gl/ebfvvD
public class Solution {
public int ladderLength(String beginWord, String endWord, List
wordList) {
Queue q = new LinkedList<>();
HashSet list = new HashSet<>(wordList);
int level = 0;
q.add(beginWord);
HashSet set = new HashSet<>();
set.add(beginWord);
while(!q.isEmpty()){
int size = q.size();
level++;
for(int i = 0; i < size; i++){
... 阅读全帖 |
|
m******e 发帖数: 536 | 23 In 1987, a mutant kitten was born in Montana with hair like a poodle. Named
Miss DePesto, this kitten grew up and birthed curly kittens of her own. As
the curly cat family tree grew, Miss DePesto's descendants eventually became
recognized as a new breed: the Selkirk Rex.
Now, 25 years and about nine kitty generations later, researchers at the
University of Veterinary Medicine, Austria, have confirmed that these
felines are genetically distinct from previously known breeds, making
Selkirk Rex the... 阅读全帖 |
|
m******e 发帖数: 536 | 24 【 以下文字转载自 pets 讨论区 】
发信人: meditate (meditate), 信区: pets
标 题: Poodle Cats could be the new rage for feline fans
发信站: BBS 未名空间站 (Thu Nov 8 09:25:14 2012, 美东)
In 1987, a mutant kitten was born in Montana with hair like a poodle. Named
Miss DePesto, this kitten grew up and birthed curly kittens of her own. As
the curly cat family tree grew, Miss DePesto's descendants eventually became
recognized as a new breed: the Selkirk Rex.
Now, 25 years and about nine kitty generations later, researchers at t... 阅读全帖 |
|
s******t 发帖数: 2374 | 25 我想了好久还是不知道怎么用递归做。。。
笨死了。
上来求教一下,不想浪费时间了。
如果inorder,可以用static变量的话我似乎还能写一个出来,不过static变量应该是不被favorable的吧。。。
static int = k;
void search(Node cur){
if(cur == null || index < 0) return;
search(cur.left);
if(--index == 0) {System.out.println(cur.value); return;}
search(cur.right);
}
很土。。。。感觉是totally wrong.... |
|
s******t 发帖数: 2374 | 26 难道是这么写?
int search(Node cur, int index){
if(cur == null) return index;
if(index < 0) return -1;
index = search(cur.left, index);
if(--index == 0) {System.out.println(cur.value); return -1;}
index = search(cur.right, index);
return index;
} |
|
j*j 发帖数: 5564 | 27 给个简单点的version:
public Node insertInOrder(Node head, Node insert) {
if (head == null) {
if (insert != null)
insert.next = null;
return insert;
}
if (insert == null)
return head;
if (insert.data < head.data) {
insert.next = head;
return insert;
}
Node cur = head;
while (cur.next != null)
{
if (cur.next.data > insert.data)
{
Node tmp = cur.next;
cur.next = insert;
insert.ne |
|
h*********n 发帖数: 11319 | 28 i believe the output should depend on how the interviewer would like to trea
t ' ' (or other delimiters)
string reverseByWord(string in){
string out;
string::iterator s = in.end();
string::iterator e = in.end();
while(s!=in.begin()){
e = s;
s--;
while((*s != ' ')&&(s != in.begin())){
s--;//strops at space
}
string::iterator cur = s;
... 阅读全帖 |
|
b**********r 发帖数: 91 | 29 Run the ultimate example around 400 ms
here is my code
4, 13, 5, 4, 13, 6, 8, 12, 16, 9, 13, 13, 13, 16, 9, 2, 6, 5,
0, 4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167,
4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167, 13, 18, 22, 35, 41, 49, 61, 77, 86, 99, 112, 125, 141, 150,
152, 158, 163, 5, 9, 22, 28, 36, 48, 64, 73, 86, 99, 112, 128, 137, 139,
145, 150, 4, 17, 23, 31, 43, 59, 68, 81, 94, 107, 123, 132, 134, 140,
145, 13, 19, 27, 39... 阅读全帖 |
|
g**********y 发帖数: 14569 | 30 主要是Bravethinker的code, 改动很小:
1. 试candidate时,对相同值,用index最小的那个,这样往下滑得更快。
2. founded不用ArrayList, 改成数组,速度更快。
3. 测试candidate时,直接改map; 测试不通过时revert修改。不过这个改动好象无关
紧要,不怎么影响速度。
public class BraveThinker implements IMilestone {
private int dfsCount = 0;
public int[] findMilestones(int[] dists) {
long t0 = System.currentTimeMillis();
dfsCount = 0;
int L = dists.length;
int N = (int) ((Math.sqrt(1 + 8 * L) - 1) / 2);
int[] founded = new int[N+1];... 阅读全帖 |
|
b**********r 发帖数: 91 | 31 Run the ultimate example around 400 ms
here is my code
4, 13, 5, 4, 13, 6, 8, 12, 16, 9, 13, 13, 13, 16, 9, 2, 6, 5,
0, 4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167,
4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167, 13, 18, 22, 35, 41, 49, 61, 77, 86, 99, 112, 125, 141, 150,
152, 158, 163, 5, 9, 22, 28, 36, 48, 64, 73, 86, 99, 112, 128, 137, 139,
145, 150, 4, 17, 23, 31, 43, 59, 68, 81, 94, 107, 123, 132, 134, 140,
145, 13, 19, 27, 39... 阅读全帖 |
|
g**********y 发帖数: 14569 | 32 主要是Bravethinker的code, 改动很小:
1. 试candidate时,对相同值,用index最小的那个,这样往下滑得更快。
2. founded不用ArrayList, 改成数组,速度更快。
3. 测试candidate时,直接改map; 测试不通过时revert修改。不过这个改动好象无关
紧要,不怎么影响速度。
public class BraveThinker implements IMilestone {
private int dfsCount = 0;
public int[] findMilestones(int[] dists) {
long t0 = System.currentTimeMillis();
dfsCount = 0;
int L = dists.length;
int N = (int) ((Math.sqrt(1 + 8 * L) - 1) / 2);
int[] founded = new int[N+1];... 阅读全帖 |
|
D********g 发帖数: 650 | 33 This is a pretty tricky question, especially for the loop handling. Here is
my java code, tested with looped case and non-looped case.
public static class DLLNode {
DLLNode prev;
DLLNode next;
int value;
public DLLNode(int value) {
this.value = value;
prev = null;
next = null;
}
}
static DLLNode dummy = new DLLNode(-1);
static DLLNode loopDummy = new DLLNode(-2);
static void printLL(final DLLN... 阅读全帖 |
|
e****e 发帖数: 418 | 34 public Node reverse(Node head, int firstPos, int secondPos) {
if ( firstPos < 1 || firstPos >= secondPos || secondPos > getLength(
head ) )
throw new IllegalArgumentException();
Node firstSwitchNode = getPosNode( head, firstPos );
Node secondSwitchNode = getPosNode( head, secondPos );
Node newHead = null;
Node cur = firstSwitchNode;
while ( cur != secondSwitchNode.next ) {
Node tmp = cur;
cur = cu... 阅读全帖 |
|
b*****6 发帖数: 179 | 35 2 public String addBinary(String a, String b) {
3 int i = a.length() - 1;
4 int j = b.length() - 1;
5 int carry = 0;
6
7 ArrayList result = new ArrayList();
8
9 while (i >= 0 || j >= 0 || carry == 1)
10 {
11 int cur = carry;
12
13 if (i >= 0)
14 cur += a.charAt(i--) - '0';
15
16 if (j >= 0)
17 cur += ... 阅读全帖 |
|
Q*******e 发帖数: 939 | 36 抛砖引玉,
struct list_node *
merge2sorted2(struct list_node *a, struct list_node *b)
{
int first_node;
struct list_node *head = NULL;
struct list_node *cur = NULL, *node;
if (a == NULL) return b;
if (b == NULL) return a;
first_node = 1;
while (a || b ) {
if (a && b) {
if (a->data < b->data) {
node = a;
a = a->next;
} else {
node = b;
b = b->next;
}
} else if... 阅读全帖 |
|
c****m 发帖数: 11 | 37 同球test case
写了一个,思想是这样的:
按照start 排序,取出当前的element(cur),然后往后check每个element(next),
如果next的end是小于cur.end,说明next这个是被cur完全cover的;直到找到一个next
的end是大于cur.end,这样我们就可以更新cur了,直到遍历完。有点类似于蠕虫爬行
一样。
感觉虽然有两个loop,但是每个Event应该只比较了一次,遍历这块应该还是O(n)的吧
?当然sort的时候是O(nlgN).不知道这样对不对?一直在潜水,求指点,谢谢
int main()
{
Event e1(1, 9);
Event e2(2, 3);
Event e3(4, 5);
vector lVec;
lVec.push_back(e1);
lVec.push_back(e3);
lVec.push_back(e2);
std::sort(lVec.begin(), lVec.end());
int start = 0;
... 阅读全帖 |
|
n****r 发帖数: 120 | 38 上个版本的aux数组多余!写个用数组实现栈的版本,更快一些。
public class Solution {
public static class Bar {
int height, startIdx;
Bar(){}
Bar(int h, int s){ height = h; startIdx = s; }
}
public static int maxn = 1000;
public static Bar[] stack = new Bar[maxn];
public static int largestRectangle(int[] h){
int top = 0, max = 0;
stack[top++] = new Bar(-1, 0);
for (int i=0; i<=h.length; i++){
Bar cur = new Bar(0, i);
if (i < h.leng... 阅读全帖 |
|
l*****a 发帖数: 14598 | 39 public void eightQueens(int[] result,int cur)
{
if(cur==8) { OUTPUT; return; }
for(int i=0;i<8;i++) {
result[cur]=i;
if (isLegal(result,cur)) eightQueens(result,cur+1);
}
} |
|
f*******t 发帖数: 7549 | 40 public int ladderLength(String start, String end, HashSet dict) {
if (start == null || end == null || start.length() != end.length())
return 0;
int dist = 1;
HashSet used = new HashSet();
HashSet cur = new HashSet();
cur.add(start);
while(!cur.isEmpty()) {
HashSet newCur = new HashSet();
dist++;
for (String s : cur) {
char[] s... 阅读全帖 |
|
f*******t 发帖数: 7549 | 41 public int ladderLength(String start, String end, HashSet dict) {
if (start == null || end == null || start.length() != end.length())
return 0;
int dist = 1;
HashSet used = new HashSet();
HashSet cur = new HashSet();
cur.add(start);
while(!cur.isEmpty()) {
HashSet newCur = new HashSet();
dist++;
for (String s : cur) {
char[] s... 阅读全帖 |
|
c********t 发帖数: 5706 | 42 嗯,就是你这个算法,O(n*sqrt(n)),改了改ben5736的codes 不知过不过的了大case;
public static int cyclic(String s) {
int len = s.length();
if (len <= 1)
return 1;
ArrayList factors = get_factors(len);
for (int i = factors.size() - 1; i >= 0; i--) {
int fctr = factors.get(i);
if (valid_cyclic(s, fctr))
return fctr;
}
return len;
}
static boolean valid_cyclic(String s, int v) {
for (int i = 0, j = v; i < s.length(); i++, j++) {
if (j == s.... 阅读全帖 |
|
c********t 发帖数: 5706 | 43 嗯,就是你这个算法,O(n*sqrt(n)),改了改ben5736的codes 不知过不过的了大case;
public static int cyclic(String s) {
int len = s.length();
if (len <= 1)
return 1;
ArrayList factors = get_factors(len);
for (int i = factors.size() - 1; i >= 0; i--) {
int fctr = factors.get(i);
if (valid_cyclic(s, fctr))
return fctr;
}
return len;
}
static boolean valid_cyclic(String s, int v) {
for (int i = 0, j = v; i < s.length(); i++, j++) {
if (j == s.... 阅读全帖 |
|
x*****0 发帖数: 452 | 44 遇到一个奇怪的问题。对于同样的数据集,在自己的IDE上测试,没有任何问题,
leetcode的onlinejudge却报错。
例如:
leetcode online judge
input output expected
[0,0] [[0,0,0]] []
[1,2,-2,-1] [[-2,2,0],[-1,1,0]] []
[-1,0,1,2,-1,-4] [[-1,1,0]] [[-1,-1,2],[-1,0,1]]
在自己的IDE上,能够给出expected的答案。
下面是我的代码:
class Solution {
public:
vector > threeSum(vector &num) {
// Start typing your C/C++ solution below
// DO NO... 阅读全帖 |
|
r*********n 发帖数: 4553 | 45 要考虑overflow,为什么不从低位加到高位呢?
假如当前位的值是cur,判断一下之前位总和val是不是满足
val <= numeric_limits::max() - cur
如果满足,则val += cur and recurse,否则throw overflow_error。这里有一个问题
是连续很多零,直接导致遇到非零位计算cur的时候,cur直接overflow,但这个问题很
好解决,只要track连续零的个数即可。 |
|
S******6 发帖数: 55 | 46 pass了,不过不是replace
class Solution {
public:
ListNode *sortList(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector res;
if(head == NULL || head->next == NULL) return head;
ListNode *cur = head;
while(cur)
{
res.push_back(cur->val);
cur = cur->next;
}
sort(res.begin(), res.end());
ListNode ... 阅读全帖 |
|
g*****g 发帖数: 212 | 47 来自主题: JobHunting版 - 新鲜G面经 我是针对我之前贴的算法(在下面)说的
如果有null 会失败,
其实考虑这个case,除了保存一个cur之外,保存一个bool的hasnext就好了
----
第二题,似乎就是用一个private变量 cur 保存一个当前value就好了。
每次call next的时候,替换当前 cur,直到 null
call peek 返回 cur
hasnext 返回 cur != null
--- |
|
f**********t 发帖数: 1001 | 48 // Given a doubly linked list with a data item, a previous and a next ptr
along with another pointer "child" that may point to the head of another
doubly linked list of same type(it will lead to a general tree type of
structure) or it may be null. Flatten the tree into a linked list... minimum
space and time complexity(order n). The doubly linked lists's head and tail
are given.
struct List;
struct ListNode {
char val;
ListNode *pre, *next;
List *child;
};
struct List {
ListNode *head, *... 阅读全帖 |
|
l*****a 发帖数: 14598 | 49 你可以连续调用
list.remove(cur)
相当于把list中cur,cur+1,cur+2..删除
然后list.insert(cur,**)
就是插在当前位置 |
|