看了其他人的,我觉得我的从效率和资源来说都比较优的
Topic sourcedef trim(s): idx = 0 begin_idx = None end_idx = None strLen = len(s) while idx < strLen: if begin_idx is None and s[idx] != ' ': begin_idx = idx if end_idx is None and s[strLen-idx-1] != ' ': end_idx = strLen-idx idx = idx + 1 if begin_idx is None and end_idx is None: return '' return s[begin_idx:end_idx]
def trim(s): idx = 0 begin_idx = None end_idx = None strLen = len(s) while idx < strLen: if begin_idx is None and s[idx] != ' ': begin_idx = idx if end_idx is None and s[strLen-idx-1] != ' ': end_idx = strLen-idx idx = idx + 1 if begin_idx is None and end_idx is None: return '' return s[begin_idx:end_idx]
直接贴缩进乱掉了
def trim(s):
idx = 0
begin_idx = None
end_idx = None
strLen = len(s)
while idx < strLen:
if begin_idx is None and s[idx] != ' ':
begin_idx = idx
if end_idx is None and s[strLen-idx-1] != ' ':
end_idx = strLen-idx
idx = idx + 1
if begin_idx is None and end_idx is None:
return ''
return s[begin_idx:end_idx]
- 1
UTOO_NAIVE