设置首尾两个指针,设置输出条件,首指针后移+1,尾指针前移-1。(常见于排序数组)
输入:hello,输出:olleh
- class Solution(object):
- def reverseString(self, s):
- i,j=0,len(s)-1
- while i < j:#判断条件很重要
- s[i],s[j]=s[j],s[i]
- i+=1
- j-=1
- return s
输入:[1,3,2,4] ,输出:4 (=1+3,[1,2],[3,4])
- class Solution(object):
- def arrayPairSum(self, nums):
- nums.sort()
- n=len(nums)
- aa=0
- i=0
- while i
1: - aa+=nums[i]
- i+=2
- return aa
输入:[2,7,11,15],输出:9
class Solution(object):