|
亲!马上注册或者登录会查看更多内容!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
pocket gem 二轮电面:
6 `4 E& x; w/ S' H/ E* y2 {* q& t. o& ?; q' D
sort color , color 给的 object 问了一些小的细节, * q- l2 J5 f/ S% [. n* x; p J
class PGObject{" w$ W0 ]7 Y! T; ~
char c;
' H* M0 b: E! k" s( D1 V0 T6 @ public PGObject(char input){! g. b% E/ g& O* }5 x* L3 p0 a
this.c = input;6 w1 n) R2 B% t) n5 j$ s4 U3 d
}/ I! w- j) H# Q8 t( h) r
public PGObject(){
, f) `! W: Z& ~+ a6 |" s( {6 i& a$ d- K
}
- y; M" H, V0 C}
2 N5 ]3 x; V" D+ ^9 Z, c' ^9 p7 F* v$ b5 N# s
) c$ J) N7 `" K! i: l
class Solution {
?: L" D! N( G8 M3 F7 [7 h" D" B) Z% K/ U2 F* P, Q
public static void main(String[] args) {6 A: L, V' ^; Y
PGObject p1 = new PGObject('g');
4 ?' d# @; G9 j PGObject p2 = new PGObject('r');
s8 ]" S5 q# z8 `5 ^. b! S: _# j PGObject p3 = new PGObject('g');
6 W, j1 D T V PGObject p4 = new PGObject('b');
) b N! k- n& y, _ PGObject p5 = new PGObject('g');
* O. Z6 E, P; r2 m$ u. L! f7 ] PGObject p6 = new PGObject('b');
9 l- ~/ U! {9 X0 Y7 T9 u/ F L! _2 i) Z8 H4 Y; c
PGObject[] res = new PGObject[]{p1,p2,p3,p4,p5,p6};4 k0 M9 m# S4 I; Z0 ~
( |. v/ ]! i8 Q. R
sortColor(res);' w) H( x' U4 H8 b. u
6 A: G) D0 e/ q+ m) r) @ for(int i = 0; i< res.length ; i++){+ c1 z! k0 G+ `9 a4 o+ W
System.out.println("");# i; X* {8 v8 s- b% b
System.out.println(res【i】.c);
9 S; j& W8 S; [: N( C u }
* |9 p$ f" E- S9 t' {& Z( F# `: b0 ]+ {4 i& R e. G0 f
}
- e2 o# }3 W+ [( U% _' o, Y7 |
9 |' o# Q" K' U, y2 @7 `/ y9 a. x# ^/ c3 I
public static void sortColor(PGObject[] colors ){
0 U' z/ C6 [: y5 B1 E- o, C' d" o int begin = 0;
% I& k# V9 `& b9 T! p7 Q int end = colors.length - 1;" Z( T0 p. A$ f' |! S; }
int current = 0;- x- D! Q) t0 v: e" M t
while(current <= end){
$ l! m& {9 P4 u- R5 u- c if(colors[current].c == 'r'){. J7 p# o) a' L4 M; C
swap(colors,begin,current);1 V% h9 \% U. `1 `
current++;
* N) e1 Q- @4 @* ~0 G begin++;6 E2 s/ K( c" V) @$ {% Z& o
}else if(colors[current].c == 'g'){, ~- `6 ?8 Z- M0 q$ v
current++;
% ?, J& X2 [' g7 v8 N: k# ~ }else if(colors[current].c == 'b'){1 K3 Z q6 W" P
swap(colors, current, end);0 I+ l5 q- U' X/ R' O4 e, l, w
end--;& e/ a* t7 p+ [ y( e1 {* M
}
/ o: _ e3 D; A K3 C& F7 s }
x# U( ~. G6 B2 t8 v }
3 G" G" M! x& c2 d* z9 c; Y1 `* d; Y+ B& m' v& k9 d* R
private static void swap(PGObject[] a,int i, int j){
2 A7 ?% h# b1 Z0 t6 V: }3 k PGObject temp = new PGObject();2 J8 H+ S$ D& A( ]
temp = a【i】;5 g% z9 l# b5 ^3 `* B/ `+ |5 R
a【i】 = a[j];- A% [+ f- W( I+ G
a[j] = temp;
, ?2 w0 E9 e I0 e3 N7 I$ p1 Z7 a$ b$ C/ }' U
% f/ S; I( K; o) l% I6 t& ~% J
}. ?6 p9 b% @+ n1 S2 f
) Z" \6 c( P. |1 _* m}
6 E( A0 S1 B" [; X+ p' K0 w, z/ i
1 i# N2 y( |4 q
1 ?; D* |1 \( }6 i- p第二题 是 Given a node in the BST, find the next largest node in the BST,
5 l( f& R+ _ g& V7 a0 h7 @代码4 [) ~! i5 L5 q
class Node{0 n9 j% A# d0 V- c1 p' | V' l1 G
Node left;
( f8 k* C0 J/ C$ m Node right;
5 I0 I+ T j# f0 z+ z Node parent;
( U) u8 D* ~& ^$ E. D8 `0 |: }}. P5 _: W( {& J: s" a
0 @! j: M5 E3 C0 {# o' Oclass Solution{+ a' k0 F) M7 I1 ]% b
public static void main(String[] args){
& b3 r' y8 s- ~
; C8 E8 S: S( ]% u }
: S2 A+ a- F- Y/ Y1 Y8 _! {3 a' {; Y- t public Node nextLargestNode(Node t){
+ ]$ J4 R0 n! r6 b( S Node temp = t;& q7 j" ^ [: D
if(t.right != null){4 U4 R% L7 B6 D: ?: I
return getLeftMost(t.right);* ~& Z) O0 O9 e) i
}& o; V+ \1 X( N9 K7 P$ C2 C
while(t!= null && t.parent != null){
, v- i0 |9 }3 A1 o* H% H" ]% ? Node parent = t.parent;$ k0 ? }7 x$ Z5 H; m8 M' h% S u" {
if(parent.left == t){) l& j9 B* s2 e9 t t# m% c
return parent;
; y1 O9 C4 N3 i }9 F; O8 ^% h+ v, Y( w5 B3 \+ I
t=t.parent;- c3 e m9 O# R* h
}: {1 x$ H& T; u1 U* n4 X& Y8 {
5 B7 Y' a( T9 X8 n, ?+ ^ return temp;4 m# T# }7 Q+ D, Q$ ~
}
! V3 z! G+ y5 |; C7 S private static Node getLeftMost(Node root){
$ H4 T' Z5 U. D2 c( B Node p = root;
( c7 n7 V$ y" t7 V" B while(p.left != null){/ c. f+ S7 ~) O% H% x! Y6 o
p = p.left;3 s2 q! M* c+ m" V( e' S
}
6 S1 Q& c6 `; V0 G( i+ N return p;
0 w8 ?* e% g) F" Q }" Z5 ^" v5 E3 o/ G0 \& |, w
}
$ l" ^! ]; P0 d+ K) Z& t& n% w/ T
/ M5 ?% a n/ u; q% ^( F. h) r5 c8 r& I0 E. R* w! X
|
评分
-
查看全部评分
|