quake wang
October 10, 2015
530

# 用RUBY解LEETCODE算法题

Ruby Conf China 2015

October 10, 2015

## Transcript

6. ### 体验RUBY语言的生产力(I) Permutations G i v e n a c o

l l e c t i o n o f n u m b e r s , r e t u r n a l l p o s s i b l e p e r m u t a t i o n s . F o r e x a m p l e , [ 1 , 2 , 3 ] h a v e t h e f o l l o w i n g p e r m u t a t i o n s : [ 1 , 2 , 3 ] , [ 1 , 3 , 2 ] , [ 2 , 1 , 3 ] , [ 2 , 3 , 1 ] , [ 3 , 1 , 2 ] , a n d [ 3 , 2 , 1 ] . / / J a v a p u b l i c L i s t < L i s t < I n t e g e r > > p e r m u t e ( i n t [ ] n u m ) { L i n k e d L i s t < L i s t < I n t e g e r > > r e s = n e w L i n k e d L i s t < L i s t < I n t e g e r > > ( ) ; r e s . a d d ( n e w A r r a y L i s t < I n t e g e r > ( ) ) ; f o r ( i n t n : n u m ) { i n t s i z e = r e s . s i z e ( ) ; f o r ( ; s i z e > 0 ; s i z e - - ) { L i s t < I n t e g e r > r = r e s . p o l l F i r s t ( ) ; f o r ( i n t i = 0 ; i < = r . s i z e ( ) ; i + + ) { L i s t < I n t e g e r > t = n e w A r r a y L i s t < I n t e g e r > ( r ) ; t . a d d ( i , n ) ; r e s . a d d ( t ) ; } } } r e t u r n r e s ;
7. ### 体验RUBY语言的生产力(I) Permutations d e f p e r m u

t e ( n u m s ) n u m s . p e r m u t a t i o n . t o _ a e n d
8. ### 体验RUBY语言的生产力(II) Length of Last Word G i v e n

a s t r i n g s c o n s i s t s o f u p p e r / l o w e r - c a s e a l p h a b e t s a n d e m p t y s p a c e c h a r a c t e r s ' ' , r e t u r n t h e l e n g t h o f l a s t w o r d i n t h e s t r i n g . I f t h e l a s t w o r d d o e s n o t e x i s t , r e t u r n 0 . N o t e : A w o r d i s d e f i n e d a s a c h a r a c t e r s e q u e n c e c o n s i s t s o f n o n - s p a c e c h a r a c t e r s o n l y . F o r e x a m p l e , G i v e n s = " H e l l o W o r l d " , r e t u r n 5 . d e f l e n g t h _ o f _ l a s t _ w o r d ( s ) w o r d s = s . s p l i t ( ' ' ) w o r d s . l a s t ? w o r d s . l a s t . l e n g t h : 0 e n d
9. ### 体验RUBY语言的生产力(III) Add Digits G i v e n a n

o n - n e g a t i v e i n t e g e r n u m , r e p e a t e d l y a d d a l l i t s d i g i t s u n t i l t h e r e s u l t h a s o n l y o n e d i g i t . F o r e x a m p l e : G i v e n n u m = 3 8 , t h e p r o c e s s i s l i k e : 3 + 8 = 1 1 , 1 + 1 = 2 . S i n c e 2 h a s o n l y o n e d i g i t , r e t u r n i t . d e f a d d _ d i g i t s ( n u m ) r = n u m . t o _ s . c h a r s . m a p ( & : t o _ i ) . r e d u c e ( : + ) r < = 9 ? r : a d d _ d i g i t s ( r ) e n d
10. ### 学习RUBY的不常用方法(I) Add Binary G i v e n t w

o b i n a r y s t r i n g s , r e t u r n t h e i r s u m ( a l s o a b i n a r y s t r i n g ) . F o r e x a m p l e , a = " 1 1 " b = " 1 " R e t u r n " 1 0 0 " . # @ p a r a m { S t r i n g } a # @ p a r a m { S t r i n g } b # @ r e t u r n { S t r i n g } d e f a d d _ b i n a r y ( a , b ) ( a . t o _ i ( 2 ) + b . t o _ i ( 2 ) ) . t o _ s ( 2 ) e n d
11. ### 学习RUBY的不常用方法(II) Best Time to Buy and Sell Stock II S

a y y o u h a v e a n a r r a y f o r w h i c h t h e i t h e l e m e n t i s t h e p r i c e o f a g i v e n s t o c k o n d a y i . D e s i g n a n a l g o r i t h m t o f i n d t h e m a x i m u m p r o f i t . Y o u m a y c o m p l e t e a s m a n y t r a n s a c t i o n s a s y o u l i k e ( i e , b u y o n e a n d s e l l o n e s h a r e o f t h e s t o c k m u l t i p l e t i m e s ) . H o w e v e r , y o u m a y n o t e n g a g e i n m u l t i p l e t r a n s a c t i o n s a t t h e s a m e t i m e ( i e , y o u m u s t s e l l t h e s t o c k b e f o r e y o u b u y a g a i n ) . # @ p a r a m { I n t e g e r [ ] } p r i c e s # @ r e t u r n { I n t e g e r } d e f m a x _ p r o f i t ( p r i c e s ) p r i c e s . e a c h _ c o n s ( 2 ) . i n j e c t ( 0 ) { | s , a | [ s + a [ 1 ] - a [ 0 ] , s ] . m a x } e n d
12. ### 学习RUBY的不常用方法(III) Group Anagrams G i v e n a n

a r r a y o f s t r i n g s , g r o u p a n a g r a m s t o g e t h e r . F o r e x a m p l e , g i v e n : [ " e a t " , " t e a " , " t a n " , " a t e " , " n a t " , " b a t " ] , R e t u r n : [ [ " a t e " , " e a t " , " t e a " ] , [ " n a t " , " t a n " ] , [ " b a t " ] ] # @ p a r a m { S t r i n g [ ] } s t r s # @ r e t u r n { S t r i n g [ ] [ ] } d e f g r o u p _ a n a g r a m s ( s t r s ) s t r s . i n j e c t ( H a s h . n e w ( [ ] ) ) d o | h , s | h [ s . c h a r s . s o r t . j o i n ] + = [ s ] h e n d . m a p { | k , v | v . s o r t } e n d
13. ### 关于作弊(I) Regular Expression Matching I m p l e m

e n t r e g u l a r e x p r e s s i o n m a t c h i n g w i t h s u p p o r t f o r ' . ' a n d ' * ' .
14. ### 关于作弊(I) 使用Ruby内置对象 # @ p a r a m {

S t r i n g } s # @ p a r a m { S t r i n g } p # @ r e t u r n { B o o l e a n } d e f i s _ m a t c h ( s , p ) s = ~ / ^ # { p } \$ / ? t r u e : f a l s e e n d
15. ### 关于作弊(II) Count Primes C o u n t t h

e n u m b e r o f p r i m e n u m b e r s l e s s t h a n a n o n - n e g a t i v e n u m b e r .
16. ### 关于作弊(II) require # @ p a r a m {

I n t e g e r } n # @ r e t u r n { I n t e g e r } r e q u i r e ' p r i m e ' d e f c o u n t _ p r i m e s ( n ) P r i m e . e a c h ( n - 1 ) . c o u n t e n d
17. ### 关于作弊(III) N-Queens II F o l l o w u

p f o r N - Q u e e n s p r o b l e m . N o w , i n s t e a d o u t p u t t i n g b o a r d c o n f i g u r a t i o n s , r e t u r n t h e t o t a l n u m b e r o f d i s t i n c t s o l u t i o n s .
18. ### 关于作弊(III) Test Driven Development # @ p a r a

m { I n t e g e r } n # @ r e t u r n { I n t e g e r } d e f t o t a l _ n _ q u e e n s ( n ) [ 0 , 1 , 0 , 0 , 2 , 1 0 , 4 , 4 0 , 9 2 , 3 5 2 , 7 2 4 , 2 6 8 0 , 1 4 2 0 0 , 7 3 7 1 2 , 3 6 5 5 9 6 , 2 2 7 9 1 8 4 , 1 4 7 7 2 5 1 2 , 9 5 8 1 5 1 0 4 , 6 6 6 0 9 0 6 2 4 ] [ n ] e n d
19. ### 我的解法 V.S 别人的解法(I) Add Digits G i v e n

a n o n - n e g a t i v e i n t e g e r n u m , r e p e a t e d l y a d d a l l i t s d i g i t s u n t i l t h e r e s u l t h a s o n l y o n e d i g i t . F o r e x a m p l e : G i v e n n u m = 3 8 , t h e p r o c e s s i s l i k e : 3 + 8 = 1 1 , 1 + 1 = 2 . S i n c e 2 h a s o n l y o n e d i g i t , r e t u r n i t . F o l l o w u p : C o u l d y o u d o i t w i t h o u t a n y l o o p / r e c u r s i o n i n O ( 1 ) r u n t i m e ? # h t t p s : / / e n . w i k i p e d i a . o r g / w i k i / D i g i t a l _ r o o t d e f a d d _ d i g i t s ( n u m ) 1 + ( n u m - 1 ) % 9 e n d 类似直接用算数公式证明的题目比如：P e r f e c t S q u a r e s
20. ### 我的解法 V.S 别人的解法(II) Single Number G i v e n

a n a r r a y o f i n t e g e r s , e v e r y e l e m e n t a p p e a r s t w i c e e x c e p t f o r o n e . F i n d t h a t s i n g l e o n e . # @ p a r a m { I n t e g e r [ ] } n u m s # @ r e t u r n { I n t e g e r } d e f s i n g l e _ n u m b e r ( n u m s ) n u m s . s o r t . e a c h _ s l i c e ( 2 ) . f i n d { | s | s [ 0 ] ! = s [ 1 ] } [ 0 ] e n d d e f s i n g l e _ n u m b e r ( n u m s ) n u m s . i n j e c t ( : ^ ) e n d 用X O R 操作解S i n g l e N u m b e r 系列的通用解法
21. ### 我的解法 V.S 别人的解法(III) Majority Element G i v e n

a n a r r a y o f s i z e n , f i n d t h e m a j o r i t y e l e m e n t . T h e m a j o r i t y e l e m e n t i s t h e e l e m e n t t h a t a p p e a r s m o r e t h a n ⌊ n / 2 ⌋ t i m e s . Y o u m a y a s s u m e t h a t t h e a r r a y i s n o n - e m p t y a n d t h e m a j o r i t y e l e m e n t a l w a y s e x i s t i n t h e a r r a y . # @ p a r a m { I n t e g e r [ ] } n u m s # @ r e t u r n { I n t e g e r } d e f m a j o r i t y _ e l e m e n t ( n u m s ) n u m s . s o r t [ n u m s . s i z e / 2 ] e n d d e f m a j o r i t y _ e l e m e n t ( n u m s ) n u m s . i n j e c t ( [ 0 , 0 ] ) { | ( x , c ) , i | c = = 0 | | x = = i ? [ i , c + 1 ] : [ x , c - 1 ] } [ 0 ] e n d 算法出处以及单步演示