Python 迭代器双指针
我们知道在cpp 这种指针语言里面,双指针是这么写的:
for (auto i=v.begin(),j=v.begin();j<v,end();j++){
// do something ...
//update pointer i
while (cond){
i++;
}
}
对于 py 这样不带指针的,一般就只能这么写:
i=0
for j in range(len(lst)):
# do_something using lst[i] and lst[j] ...
# update index i
while cond:
i++
一般来说,迭代器可以比较好的覆盖指针操作,这里提供一种解法:
left=iter(s)
for j in s:
while cond:
i=next(left)
# do_comething using i and j ...
ref:https://leetcode.cn/problems/take-k-of-each-character-from-left-and-right/solutions/2932247/python-die-dai-qi-qu-dai-xia-biao-by-deu-zh17/