hero image

Welcome to my tech blog. I'm Clark Ke

Background image generated by AI prompt by Clark Ke. A tribute to the painter Van Gogh.

algorithms
python algorithms
microservices
microservices note
Intro Page

Intro Page

I'm a Python programmer.
the page is updating and will translate ASAP.
Artificial Intelligence (AI) technology is evolving rapidly and I will be updating my knowledge about AI soon
If you have any question, welcome send email to clark1990@me.com


Mr.ClarkLess than 1 minute
用两个栈实现队列

#用两个栈实现队列

'''用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,
分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
示例 1:
输入:
["CQueue","appendTail","deleteHead","deleteHead","deleteHead"]
[[],[3],[],[],[]]
输出:[null,null,3,-1,-1]
示例 2:

输入:
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
输出:[null,-1,null,null,5,2]
'''
class CQueue(object):

    def __init__(self):
        self.stack1 = []
        self.stack2 = []

    def appendTail(self, value):
        """
        :type value: int
        :rtype: None
        """

        self.stack1.append(value)

    def deleteHead(self):
        """
        :rtype: int
        """
        if self.stack2 != []:
            return self.stack2.pop()

        if self.stack1 == []:
            return -1

        while self.stack1 != []:
            self.stack2.append(self.stack1.pop())
        return self.stack2.pop()

Mr.ClarkLess than 1 minute
从尾到头打印链表

#从尾到头打印链表

'''输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。

示例 1:
输入:head = [1,3,2]
输出:[2,3,1]
'''

# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution(object):
    def reversePrint(self, head):
        """
        :type head: ListNode
        :rtype: List[int]
        """
        stack = []
        while (head != None):
            stack.append(head.val)
            head = head.next
        stack.reverse()
        return stack

Mr.ClarkLess than 1 minute
替换字符串空格

#替换字符串中的空格

'''输入:s = "We are happy."
输出:"We%20are%20happy."'''

s = "We are happy."

#基础解法
def replaceStr(str):
    list = []
    for i in str:
        if i == " ":
            list.append("%20")
        else:
            list.append(i)
    return "".join(list)

print(replaceStr(s))

#快速解法
newstr = s.replace(" ", "%20")
print(newstr)


Mr.ClarkLess than 1 minute
判断字符串是否表示数值

判断字符串是否表示数值

请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。

部分数值列举如下:
["+100", "5e2", "-123", "3.1416", "-1E-16", "0123"]
部分非数值列举如下:
["12e", "1a3.14", "1.2.3", "+-5", "12e+5.4"]
示例 1:
输入:s = "0"
输出:true
示例 2:
输入:s = "e"
输出:false
示例 3:
输入:s = "."
输出:false
示例 4:
输入:s = "    .1  "
输出:true

Mr.ClarkAbout 2 min
反转链表
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

Mr.ClarkLess than 1 minute
最小栈

最小栈

设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 实现 MinStack 类: MinStack() 初始化堆栈对象。 void push(int val) 将元素val推入堆栈。 void pop() 删除堆栈顶部的元素。 int top() 获取堆栈顶部的元素。 int getMin() 获取堆栈中的最小元素。

输入:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]

输出:
[null,null,null,null,-3,null,0,-2]

Mr.ClarkAbout 1 min
验证栈序列

验证栈序列

给定 pushed 和 popped 两个序列,每个序列中的 值都不重复,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true;否则,返回 false 。

示例 1:

输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
输出:true
解释:我们可以按以下顺序执行:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1


Mr.ClarkLess than 1 minute
复杂链表的复制

复杂链表的复制

请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。

输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]

Mr.ClarkLess than 1 minute
左旋转字符串

左旋转字符串

字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。

示例 1:
输入: s = "abcdefg", k = 2
输出: "cdefgab"
示例 2:
输入: s = "lrloseumgh", k = 6
输出: "umghlrlose"

Mr.ClarkLess than 1 minute
2