Lintcode之温故知新

本文精选了链表操作、二叉树遍历、排序、质数判断等经典数据结构与算法题目,并提供了详细的解决方案。包括链表元素删除、二叉树的前中后序遍历、字符串排序及特定问题的解决思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.删除链表的元素

题目描述:

删除链表中等于给定值val的所有节点。

样例
给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5。

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


class Solution:
    """
    @param: head: a ListNode
    @param: val: An integer
    @return: a ListNode
    """
    def removeElements(self, head, val):
        # write your code here
        if head == None:
            return None
        while head.val == val:
            head = head.next
            if head == None:
                return None
        pre = head
        while pre.next is not None:
            if pre.next.val == val:
                pre.next = pre.next.next
                # pre = pre.next
            else:
                pre = pre.next
                
        return head

3.字符串排序


4.二叉树的前序遍历
"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""


class Solution:
    """
    @param: root: A Tree
    @return: Preorder in ArrayList which contains node values.
    """
    def preorderTraversal(self, root):
        # write your code here
        List = []
        if root is not None:
            List.append(root.val)
            if root.left is not None:
                List += self.preorderTraversal(root.left)
            if root.right is not None:
                List += self.preorderTraversal(root.right)
            
        return List

5.二叉树的中序遍历
"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""


class Solution:
    """
    @param: root: A Tree
    @return: Inorder in ArrayList which contains node values.
    """
    def inorderTraversal(self, root):
        # write your code here
        List = []
        if root is None:
            return List
        if root.left is None:
            List.append(root.val)
        if root.left is not None:
            List += self.inorderTraversal(root.left)
            List.append(root.val)
        if root.right is not None:
            List += self.inorderTraversal(root.right)
        return List
            

6.二叉树的后序遍历
"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""


class Solution:
    """
    @param: root: A Tree
    @return: Postorder in ArrayList which contains node values.
    """
    def postorderTraversal(self, root):
        # write your code here
        List = []
        if root is None:
            return List
        if root.left is not None:
            List += self.postorderTraversal(root.left)
        if root.right is not None:
            List += self.postorderTraversal(root.right)
            List.append(root.val)
        else:
            List.append(root.val)
        return List

7.Kth Prime Number

题目描述:给出质数n,输出它是第几个质数。

class Solution:
    """
    @param n: the number
    @return: the rank of the number
    """
    def kthPrime(self, n):
        # write your code here
        if n == 1:
            return n
        i = 2
        k = 0
        while i <= n:
            for j in range(2, int(i**0.5)+1):
                if i%j == 0:
                    break
            else:
                k += 1
            i += 1
        return k

8.
O(1)时间检测2的幂次

位运算
class Solution:
    """
    @param n: An integer
    @return: True or false
    """
    def checkPowerOf2(self, n):
        # write your code here
        if n <= 0:
            return False
        if n&(n-1) == 0:
            return True
        return False

8.多关键字排序

题目:给定n个学生(1到n编号)以及他们的考试成绩,这里有两个关键字,考试成绩以及学生考号。根据第一关键字对数组进行排序(降序 ),如果第一关键字相同则根据第二关键字进行排序(升序).

例:
给出 [[2,50],[1,50],[3,100]]
返回 [[3,100],[1,50],[2,50]]

使用sorted()方法的关键字key参数来进行排序。


class Solution:
    """
    @param array: the input array
    @return: the sorted array
    """
    def multiSort(self, array):
        # Write your code here
        mid_array = sorted(array, key=lambda x:x[0])
        result_array = sorted(mid_array, key=lambda x:x[1], reverse=True)
        
        return result_array


9.最长上升连续子序列

题目描述:给定一个整数数组(下标从0到n-1,n表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列)。

例:

给定[5, 4, 2, 1, 3],其最长上升连续子序列(LICS)为[5, 4, 2 , 1],返回 4.

给定[5, 1, 2, ,3, 4],其最长上升连续子序列(LICS)为[1, 2, 3 , 4],返回 4.

代码

class Solution:
    """
    @param A: An array of Integer
    @return: an integer
    """
    def longestIncreasingContinuousSubsequence(self, A):
        # write your code here
        if A == []:
            return 0
        List = [0]
        length = len(A)
        for i in range(1, length-1):
            if (A[i] > A[i-1] and A[i] > A[i+1]) or (A[i] < A[i-1] and A[i] < A[i+1]):
                List.append(i) 
        List.append(length-1)
        
        result = max([List[i]-List[i-1]+1 for i in range(1, len(List))])
        return result 


刷题数突破90。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值