【无标题】

1.反转链表

class listnode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class solution:
    def reverselist(self,head:listnode):
        cur = head
        pre = None
        while(cur!=None):
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        return pre

x=[1,2,3,4]
node = listnode(val=1,next=None)
head = node
for i in x[1:]:
    node.next = listnode(i)
    node = node.next
n=solution().reverselist(head)
while n:
    print(n.val)
    n = n.next
class listnode:
    def __init__(self,val,next=None):
        self.val=val
        self.next=next

class my_list:
    def __init__(self, input):
        self.length=len(input)
        if self.length==0:
            head=node=listnode(val=None)
        else:
            head=node=listnode(input[0])
            for i in input[1:]:
                node.next=listnode(i)
                node=node.next
        self.head=head
    def reverse_list(self):
        cur=self.head
        pre=None
        while (cur!=None):
            t=cur.next
            cur.next=pre
            pre=cur
            cur=t
        self.head=pre

    def print(self):
        n=self.head
        if self.length!=0:
            while n:
                print(n.val)
                n=n.next


test_case = [1246]
test_list = my_list(test_case)
test_list.reverse_list()
test_list.print()

2、合并有序链表

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        res = ListNode(0)
        l3 = res
        while l1 and l2:
            if l1.val < l2.val:
                l3.next = l1
                l1 = l1.next
            else:
                l3.next = l2
                l2 = l2.next
            l3 = l3.next
        if l1:
            l3.next = l1
        if l2:
            l3.next = l2
        return res.next

IOU

def iou( box1, box2 ):
    """
    :param box1:[x1,y1,x2,y2] 左上角的坐标与右下角的坐标
    :param box2:[x1,y1,x2,y2]
    :return: iou_ratio--交并比
    """
    width1 = abs(box1[2] - box1[0])
    height1 = abs(box1[1] - box1[3]) # 这里y1-y2是因为一般情况y1>y2,为了方便采用绝对值
    width2 = abs(box2[2] - box2[0])
    height2 = abs(box2[1] - box2[3])
    x_max = max(box1[0],box1[2],box2[0],box2[2])
    y_max = max(box1[1],box1[3],box2[1],box2[3])
    x_min = min(box1[0],box1[2],box2[0],box2[2])
    y_min = min(box1[1],box1[3],box2[1],box2[3])
    iou_width = x_min + width1 + width2 - x_max
    iou_height = y_min + height1 + height2 - y_max
    if iou_width <= 0 or iou_height <= 0:
        iou_ratio = 0
    else:
        iou_area = iou_width * iou_height # 交集的面积
        box1_area = width1 * height1
        box2_area = width2 * height2
        iou_ratio = iou_area / (box1_area + box2_area - iou_area) # 并集的面积
    return iou_ratio
box1 = [1,3,4,1]
box2 = [2,4,5,2]
print(iou(box1,box2))

NMS

import numpy as np
def nms(dets,thre):
    x1,y1,x2,y2,s=dets[:0],dets[:1],dets[:2],dets[:3],dets[:4]
  #计算每个检测框的面积,并对目标检测得分进行降序排序
    order=np.argsort(s)[::-1]
    area=(x2-x1+1)*(y2-y1+1)
    
    keep=[] #保留框的结果集合
    while order:
        i=order[0]
        keep.append(i) #保留该类剩余box中得分最高的一个
        # 计算最高得分矩形框与剩余矩形框的相交区域
        xx1 = np.maximum(x1[i],x1[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])
       #计算相交的面积,不重叠时面积为0
        w=np.maximum(xx2-xx1+1,0)
        h=np.maximum(yy2-yy1+1,0)
        inter=w*h
        #计算IoU:重叠面积 /(面积1+面积2-重叠面积)
        iou=inter/(area[i]+area[order[1:]]-inter)
         #保留IoU小于阈值的box
        inds=np.where(iou<=thre)[0]
        order=order[inds+1]
    return  keep
        dets = [[218, 322, 385, 491, 0.98],[247, 312, 419, 461, 0.83],[237, 344, 407, 510, 0.92],
            [757, 218, 937, 394, 0.96],[768, 198, 962, 364, 0.85],[740, 240, 906, 414, 0.83],
            [1101, 84, 1302, 303, 0.82], [1110, 67, 1331, 260, 0.97], [1123, 42, 1362, 220, 0.85]]
    dets = np.array(dets)
    print(py_cpu_nms(dets, 0.5))

快排

class Solution:
    def findKthLargest(self, nums, k):
        # 快排
        self._k = len(nums) - k
        return self.quicksort(nums, 0, len(nums) - 1)
    def quicksort(self, nums, left, right):
        if left == right:
            return nums[left]
        pivot = self.partition(nums, left, right)
        if pivot == self._k:
            return nums[pivot]
        elif pivot < self._k:
            return self.quicksort(nums, pivot + 1, right)
        else:
            return self.quicksort(nums, left, pivot - 1)
    def partition(self, nums, left, right):
        pivot = nums[left]
        i, j = left, right
        while i < j:
            while i < j and nums[j] >= pivot:
                j -= 1
            if i < j:
                nums[i] = nums[j]
                i += 1
            while i < j and nums[i] <= pivot:
                i += 1
            if i < j:
                nums[j] = nums[i]
                j -= 1
        nums[i] = pivot
        return i
        
    if __name__ == '__main__':
    nums = [1,4,8,5,0,4,9]
    solution = Solution()
    k = solution.findKthLargest(nums,3)
    print(k)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值