1
northisland OP def temp(line,flag,pos,lvl=0):
if line[pos].isdigit(): flag[pos]=lvl+1 if pos+1<len(line): return temp(line,flag,pos+1,lvl+1) else: return pos+1,lvl else: flag[pos]=0 lvl=0 return pos+1,lvl def extract_digits(line): """ extract longest digit sub-str from line :param line: :return: """ flag=[0 for x in range(len(line))] print flag pos=0 while pos<len(flag): pos,lvl=temp(line,flag,pos) print flag length=max(flag) if length>0: pe=flag.index(length) return line[pe-length+1:pe+1] else: return '' if __name__=='__main__': print extract_digits('tt2012g2t11111g') |
2
northisland OP |
3
northisland OP <script src="https://gist.github.com/anonymous/5885857ecfeef423be7a.js"></script>
好吧,原来 V2EX 的代码时这么贴的=_= 下午写代码遇到这个功能, 于是手痒做了一个递归算法版本的这个函数 大概写了 20 分钟写好,个人感觉递归函数很抽象, 请各位同志点评 |
4
xiaolajiao 2016-02-26 22:21:01 +08:00 3
import re
def maximum(string): split_list = re.split('[a-zA-z]', string) return max(split_list) |
5
manfay 2016-02-27 00:22:51 +08:00 via iPad 1
ruby
/\d+/.match("tt2012g2")[0] |
6
manfay 2016-02-27 00:31:51 +08:00 via iPad
错了😂
|
7
function007 2016-02-27 00:52:58 +08:00 1
$str = 'tt2012dg2';
preg_match_all('/\d+/', $str,$result); rsort($result[0]); echo $result[0][0]; |
8
imcotton 2016-02-27 01:20:05 +08:00 1
'tt2012dg2'.match(/\d+/g).reduce((a, b) => a.length > b.length ? a : b)
-ES2015 |
9
ETiV 2016-02-27 01:21:20 +08:00 1
写法 1, 不考虑长度并列:
INPUT_STRING.match(/(\d+)/g).sort(function(m,n){return n.length - m.length})[0]; 2, 考虑并列长度: var l=0; INPUT_STRING.match(/(\d+)/g).map(function(n){l=n.length>l?n.length:l;return n;}).filter(function(n){return l == n.length}); |
10
vibbow 2016-02-27 01:27:01 +08:00 2
<?php
$input = 'tt2012g2'; preg_match_all('/(\d+)/', $input, $matches); echo max($matches[0]); |
11
scarlex 2016-02-27 01:37:59 +08:00
python
"tt2012g2"[2:6] |
12
allan888 2016-02-27 09:16:36 +08:00 1
其实不太明白楼主写的啥意思,感觉不是 O(n)复杂度的样子,贴一个 O(n)的
https://gist.github.com/jieaozhu/3387e2561bfb0280635c |
13
yuriko 2016-02-27 09:23:37 +08:00
唉我感觉是不是理解错了……
直接遍历一遍数数字个数不就好了? |
14
northisland OP |
15
allan888 2016-02-27 09:41:40 +08:00 1
@northisland 递归的话你多找找 backtracking 和深度优先的题目来做啊。。。
|
16
yuriko 2016-02-27 09:43:53 +08:00 1
|
17
myid 2016-02-28 20:16:56 +08:00 1
Haskell 代码。 O(n) 。递归。
import Data.Char (isDigit) longestInts :: String -> [Int] longestInts xs = map (digitsToInt) $ longestLength $ extractDigits xs extractDigits :: String -> [String] extractDigits [] = [] extractDigits xs | null $ dropWhile (not.isDigit) xs = [] | otherwise = (takeWhile (isDigit) $ dropWhile (not.isDigit) xs) : (extractDigits $ dropWhile (isDigit) $ dropWhile (not.isDigit) xs) |
18
myid 2016-02-28 20:54:59 +08:00
|
19
xiaolajiao 2016-02-28 22:04:05 +08:00 1
之前版本有错误,这样就好了
@myid import re def maximum(string): split_list = re.split('\D', string) int_list = [int(s) for s in split_list if len(s) > 0] return max(int_list) |
21
scarlex 2016-02-29 22:20:05 +08:00 1
@myid
Haskell getNumbers [] = [[]] getNumbers xs = [ys] ++ getNumbers zs' ____where (ys, ys') = span (\x -> x `elem` ['0'..'9']) xs __________(zs, zs') = break (\x -> x `elem` ['0'..'9']) ys' getLongest = (foldr f "") . getNumbers ____where f x y = if (length x > length y) then x else y 不用正则,只用预设函数写出来的版本~ |