使用双指针
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if len(nums) < 2: return len(nums)
# 定义双指针
a, b = 0, 1
while b < len(nums):
# 双指针的值不相等时,将b指针的值赋值给a+1位置上
if nums[a] != nums[b]:
a += 1
nums[a] = nums[b]
else:
b += 1
return a + 1
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
i = 1
while i < len(nums):
# 用后值对比前值,相等则删除后值
if nums[i] == nums[i - 1]:
nums.pop(i)
else:
i += 1
return len(nums)
可以尝试使用正则匹配:
grep -E 'qingcdn.com|trpcdn.net|bsgslb.cn|bsclink.cn|bsgslb.com|bsccdn.com|baishan-cloud.net|dolfindns.net|solocdn.cn|baishancdnx.cn|baishancdnx.com|azchcdnb.com|ctxcdn.cn|cdn.miguvideo.com|qn.qnydns.com|zllxyun.cn|bc.yuancdn.com|vendor1.iqiyi.router7.com' t.txt
可以把原件发出来么,好调试些
递归解法
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
# 如果l1或l2有一个为空,则直接返回不为空的那个
if l1 and l2:
# 如果l1比l2大,将l1,l2值互换,使得 l1 指向比较小的那个节点对象
if l1.val > l2.val: l1, l2 = l2, l1
# 修改 l1 的 next 属性为递归函数返回值
l1.next = self.mergeTwoLists(l1.next, l2)
return l1 or l2
将有效的括号替换为空,返回最终字符是否为空
class Solution:
def isValid(self, s: str) -> bool:
for i in range(int(len(s)/2)):
s = s.replace('()', '').replace('[]', '').replace('{}', '')
return s == ''
还可以利用os.path.commonprefix
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
return os.path.commonprefix(strs)
比较最大最小的公共前缀就是整个数组的公共前缀
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs: return ""
s1 = min(strs)
s2 = max(strs)
for i,x in enumerate(s1):
if x != s2[i]:
return s2[:i]
return s1
只有在遇到特殊情况时,两个字符中左边的字符小于右边的字符,且等于右边的字符代表的数减左边字符代表的数。所以将字符串倒叙循环判断。
class Solution:
def romanToInt(self, s: str) -> int:
mapping = {
"I":1,
"V":5,
"X":10,
"L":50,
"C":100,
"D":500,
"M":1000
}
result = 0
prev_num = 0
for c in reversed(s):
a = mapping.get(c)
if a < prev_num:
result -= a
else:
result += a
prev_num = a
return result
利用字符串翻转
class Solution:
def isPalindrome(self, x: int) -> bool:
s = str(x)
return s == s[::-1]
利用字符串的切片翻转,然后判断最后一个字符是否为正负,接着超出范围返回 0
class Solution:
def reverse(self, x: int) -> int:
string = str(x)
r = string[::-1]
if r[-1:] == '-':
r = '-' + r[:-1]
i_str = int(r)
if i_str>(-2**31) and i_str<(2**31-1):
return i_str
else:
return 0
使用 python3, 拿 target 循环减去 list 中的数据,然后在判断结果是否存在余下的列表中。存在返回下标
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
result = []
for v in nums:
b = target - v
i = nums.index(v) + 1
if b in nums[i:]:
result = [i - 1, nums[i:].index(b) + i]
else:
continue
return result
理解的意思
解决方案
demo
---
- hosts: localhost
gather_facts: False
vars:
- url_info: [
"http://192.168.132.37:7443/repository/pkg/pay/test/yt-fee-service/20191224131820/yt-fee-service-bin-1.0.zip",
"http://192.168.132.37:7443/repository/pkg/pay/test/yt-fee-service/20191224131818/yt-fee-service-bin-1.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191224165931/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191224142428/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191224184236/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/pay/test/yt-fee-service/20191224130912/yt-fee-service-bin-1.0.zip",
"http://192.168.132.37:7443/repository/pkg/pay/test/yt-fee-service/20191224131815/yt-fee-service-bin-1.0.zip",
"http://192.168.132.37:7443/repository/pkg/pay/uat/yt-fee-service/20191224131824/yt-fee-service-bin-1.0.zip",
"http://192.168.132.37:7443/repository/pkg/pay/test/yt-fee-restapi/20191224130634/yt-fee-restapi.war",
"http://192.168.132.37:7443/repository/pkg/upp/test/upp-all/20191224141001/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191224192016/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191224193330/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191224192907/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225105513/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225091727/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225103534/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225104508/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/test/upp-all/20191225103528/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191225113612/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/pay/uat/ytd-pf-web/20191225134636/ytd-pf-web.war",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225143018/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225144501/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225150010/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225161135/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225164907/upp-all-bin-1.0.0.zip"
]
tasks:
- debug: var=url_info
- shell: echo -e {{ url_info | join("\\n")}} | grep med3|grep uat |grep upp-all |sort -rh |head -n1
register: result
- debug: var=result.stdout
- debug: msg={{ url_info | get_last("med3", "uat", "upp-all")}}
filter
# cat filter_plugins/get.py
def sortInt(elem):
t = elem.split('/')
return int(t[8])
def search(item, s):
result = []
for value in item:
if s in value:
result.append(value)
return result
def get_last(item, *args):
for a in args:
item = search(item, a)
item.sort(key=sortInt, reverse=True)
return item[0]
class FilterModule(object):
"""Filters for working with output from hostvars check_result"""
def filters(self):
return {
'get_last': get_last
}
执行结果
ansible-playbook tf.yaml
PLAY [localhost] *******************************************************************************************************
TASK [debug] ***********************************************************************************************************
ok: [localhost] => {
"url_info": [
"http://192.168.132.37:7443/repository/pkg/pay/test/yt-fee-service/20191224131820/yt-fee-service-bin-1.0.zip",
"http://192.168.132.37:7443/repository/pkg/pay/test/yt-fee-service/20191224131818/yt-fee-service-bin-1.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191224165931/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191224142428/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191224184236/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/pay/test/yt-fee-service/20191224130912/yt-fee-service-bin-1.0.zip",
"http://192.168.132.37:7443/repository/pkg/pay/test/yt-fee-service/20191224131815/yt-fee-service-bin-1.0.zip",
"http://192.168.132.37:7443/repository/pkg/pay/uat/yt-fee-service/20191224131824/yt-fee-service-bin-1.0.zip",
"http://192.168.132.37:7443/repository/pkg/pay/test/yt-fee-restapi/20191224130634/yt-fee-restapi.war",
"http://192.168.132.37:7443/repository/pkg/upp/test/upp-all/20191224141001/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191224192016/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191224193330/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191224192907/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225105513/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225091727/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225103534/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225104508/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/test/upp-all/20191225103528/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/yt-supervise-jiangsu/20191225113612/yt-supervise-jiangsu-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/pay/uat/ytd-pf-web/20191225134636/ytd-pf-web.war",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225143018/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225144501/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225150010/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225161135/upp-all-bin-1.0.0.zip",
"http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225164907/upp-all-bin-1.0.0.zip"
]
}
TASK [shell] ***********************************************************************************************************
changed: [localhost]
TASK [debug] ***********************************************************************************************************
ok: [localhost] => {
"result.stdout": "http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225164907/upp-all-bin-1.0.0.zip"
}
TASK [debug] ***********************************************************************************************************
ok: [localhost] => {
"msg": "http://192.168.132.37:7443/repository/pkg/med3/uat/upp-all/20191225164907/upp-all-bin-1.0.0.zip"
}
PLAY RECAP *************************************************************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
优先级从上到下,系统的学习。
能看几本是几本吧
sh 那块使用双引号,变量使用${PKGNEXUSURL}
试试
活动快照:
这是都是应用类了的。关于 kubernetes 的安装部署,请看https://lework.github.io/category/#kubernetes systemd 可以写到配置文件中。我加到了计划里了需求统计
收录到运维备忘单
啥意思,如果想提交的话,去 github 上加入组织哈。
常用软件国内镜像 , 这里面我收集了一些常用的镜像。没有的也可以推荐下哈