字符串|替换空格,

要求
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy

class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        # write code here
        return s.replace(' ', '%20')

要求

输入一个链表,从尾到头打印链表每个节点的值

思路
1)倒置指针——会改变原始输入信息,需注意是否允许改变原数据
2)基于递归实现——先输出内层的,注意当链表很长时会导致调用栈溢出
3)基于栈,用循环实现

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        if not listNode:
            return []
        stack = []
        current = listNode
        while (current is not None):
            stack.append(current.val)
            current = current.next
        result = []
        # 注意range的使用,倒序第二个如果为-1,可以打到0,如果是0就打不出来0
        for i in range(len(listNode)-1, -1, -1):
            result.append(stack[i])
        return result

参考链接:https://codingcat.cn/article/21

猜你喜欢

转载自blog.csdn.net/kylin_learn/article/details/88928864