【LeetCode刷题日记】算法1-9


1. 两数之和

给定一个整数数组nums和一个整数目标值target,请你在该数组中找出 和为目标值target的那两个整数,并返回它们的数组下标。

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        for i in range(len(nums)):
            for j in range(i+1, len(nums)):
                sum = nums[i] + nums[j]
                if sum == target:
                    return [i, j]

2. 两数相加(中等)

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。请你将两个数相加,并以相同形式返回一个表示和的链表。
(小学数学题思想,个位相加,逢10进1)

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        result = ListNode(0)
        r = result
        carry = 0
        while l1 or l2:
            x = l1.val if l1 else 0
            y = l2.val if l2 else 0
            s = carry + x + y
            carry = s //10 
            r.next = ListNode(s % 10)
            r = r.next
            if (l1!=None):l1=l1.next
            if (l2!=None):l2=l2.next
        if carry>0:
            r.next = ListNode(1)

        return result.next

3. 无重复字符的最长子串(中等)

给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        max_s = ''
        temp = ''

        for i in s:
            is_same = False
            for j in temp:
                if i == j:
                    is_same = True
                    break
                else:
                    continue
            
            if is_same:
                temp = temp[temp.index(i)+1:] + i
            else:
                temp += i
                if len(temp) >= len(max_s):
                    max_s = temp

        return len(max_s)

4. 寻找两个正序数组的中位数(困难)

给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1nums2。请你找出并返回这两个正序数组的 中位数 。

class Solution(object):
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        len1 = len(nums1)
        len2 = len(nums2)
        output_id = []
        if (len1 + len2) % 2 == 1:
            output_id.append(int((len1 + len2 - 1) / 2))
        else:
            output_id.append(int((len1 + len2) / 2 - 1))
            output_id.append(int((len1 + len2) / 2))
        
        i,j = 0,0
        all_list = []
        while i < len1 and j < len2:
            if nums1[i] < nums2[j]:
                all_list.append(nums1[i])
                i += 1
            else:
                all_list.append(nums2[j])
                j +=1
        if i >= len1:
            all_list += nums2[j:]
        if j >= len2:
            all_list += nums1[i:]

        result = [all_list[i] for i in output_id]
        re = float(sum(result)) / len(result)

        return re

5. 最长回文子串(中等)

给你一个字符串 s,找到 s 中最长的回文子串。

class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        for i in range(len(s), 0, -1):
            for j in range(len(s) - i + 1):
                s_t = s[j: j+i]
                s_t_1 = s_t[::-1]
                if s_t == s_t_1:
                    return s_t

6. Z 字形变换

将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

class Solution(object):
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows == 1:
            return s

        str_m = [""] * numRows
        id,sum = 0,0
        is_return = 1
        for i in s:
            str_m[id] += i
            id += is_return
            sum += 1
            if sum == numRows-1:
                is_return = -is_return
                sum = 0

        result = ''
        for i in str_m: result += i
        return result

7. 整数反转(中等)

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。
如果反转后整数超过 32 位的有符号整数的范围 [−231, 2^31 − 1] ,就返回 0。

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        if x == 0:
            return 0

        x = str(x)
        is_neg = False
        if x[0] == '-':
            is_neg = True
            x = x[1:]

        x = x[::-1]
        result = ''
        for i in range(len(x)):
            if x[i] != '0':
                result = x[i:]
                break
        
        result = -int(result) if is_neg else int(result)

        return result if -2**31 < result < 2**31 - 1 else 0

8. 字符串转换整数 (atoi)(中等)

请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。

class Solution(object):
    def myAtoi(self, s):
        """
        :type s: str
        :rtype: int
        """
        result = ''
        isn = 0 # 负号个数
        isp = 0 # 加号个数
        mm = True
        for i in s:
            if mm and i not in "1234567890":
                if i in ' -+':
                    if i == '-': 
                        isn += 1
                        if isn+isp==2:break         # 排除'+-'情况
                    elif i == '+':
                        isp += 1
                        if isn+isp==2:break         
                    elif (isp or isn) and i==' ':   # 排除' + 4'情况
                        break
                else:
                    break
            elif i in "1234567890" or mm:
                result += i
                mm = False
            else:
                break

        result = int(result) if result else 0
        result = -int(result) if isn==1 else int(result)
        result = max(min(result, 2**31-1), -2**31)
        
        return result

9. 回文数

给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        is_pp = True
        x = str(x)
        len_x = len(x)

        for i in range(int(len_x/2)):
            if x[i] == x[len_x-i-1]:
                continue
            else:
                is_pp = False
                break

        return is_pp

参考

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面只是目标目录 ├─第1章-Shiro权限实战课程介绍 │ 1-1Shiro权限实战课程介绍.mp4 │ 1-2权限控制和初学JavaWeb处理访问权限控制.mp4 │ ├─第2章-大话权限框架核心知识ACL和RBAC │ 2-1权限框架设计之ACL和RBAC讲解.mp4 │ 2-2主流权限框架介绍和技术选型讲解.mp4 │ ├─第3章-ApacheShiro基础概念知识和架构讲解 │ 3-1Shiro核心知识之架构图交互和四大模块讲解.mp4 │ 3-2用户访问Shrio权限控制运行流程和常见概念讲解.mp4 │ ├─第4章-Springboot2.x整合ApacheShiro快速上手实战 │ 4-1SpringBoot2.x整合Shiro.mp4 │ 4-2快速上手之Shiro认证和授权流程实操上集.mp4 │ 4-3Shiro认证和授权流程和常用API梳理下集.mp4 │ ├─第5章-详细讲解ApacheShirorealm实战 │ 5-1Shiro安全数据来源之Realm讲解.mp4 │ 5-2快速上手之Shiro内置IniRealm实操.mp4 │ 5-3快速上手之Shiro内置JdbcRealm实操.mp4 │ 5-4ApacheShiro自定义Readl实战.mp4 │ 5-5深入Shiro源码解读认证授权流程.mp4 │ ├─第6章-Shiro权限认证Web案例知识点讲解 │ 6-1Shiro内置的Filter过滤器讲解.mp4 │ 6-2Shiro的Filter配置路径讲解.mp4 │ 6-3Shiro数据安全之数据加解密.mp4 │ 6-4Shiro权限控制注解和编程方式讲解.mp4 │ 6-5Shiro缓存模块讲解.mp4 │ 6-6ShiroSession模块讲解.mp4 │ ├─第7章-ApacheShiro整合SpringBoot2.x综合案例实战 │ 7-10使用ShiroLogout和加密处理.mp4 │ 7-1Shiro整合SpringBoot2.x案例实战介绍.mp4 │ 7-2基于RBAC权限控制实战之Mysql数据库设计.mp4 │ 7-3SpringBoot2.x项目框架和依赖搭建.mp4 │ 7-4案例实战之权限相关服务接口开发.mp4 │ 7-5案例实战之用户角色权限多对多关联查询SQL.mp4 │ 7-6案例实战自定义CustomRealm实战.mp4 │ 7-7项目实战之ShiroFilterFactoryBean配置实战.mp4 │ 7-8前后端分离自定义SessionManager验证.mp4 │ 7-9API权限拦截验证实战.mp4 │ ├─第8章-权限控制综合案例实战进阶 │ 8-1实战进阶之自定义ShiroFilter过滤器上集.mp4 │ 8-2实战进阶之自定义ShiroFilter过滤器下集.mp4 │ 8-3性能提升之Redis整合CacheManager.mp4 │ 8-4性能提升之Redis整合SessionManager.mp4 │ 8-5ShiroConfig常用bean类配置.mp4 │ ├─第9章-大话分布式应用的鉴权方式 │ 9-1单体应用到分布式应用下的鉴权方式介绍.mp4 │ 9-2Shiro整合SpringBoot下自定义SessionId.mp4 │ ├─第10章-Shiro课程总结 │ 10-1Apacheshiro从入门到高级实战课程总结.mp4 │ 10-2高级工程师到架构师-解决问思路+学习方法.mp4 │ └─课件资料.zip

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prymce-Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值