随笔分类 -  力扣-算法

https://leetcode-cn.com/ 详细内容请看力扣
摘要:写一个程序,输出从 1 到 n 数字的字符串表示。(第一个和第二个是我写的,最直接都是这么朴实无华) class Solution(object): def fizzBuzz1(self, n): """ :type n: int :rtype: List[str] """ ret_list = [ 阅读全文
posted @ 2021-01-26 11:05 楠海 阅读(195) 评论(0) 推荐(0)
摘要:假设你是程序员,你们n次git提交之前的都是对的,n+1次之后提交的代码都有问题,下在需要知道是从那次开始出现错误的,请设计算法isBadVersion函数可以判断这个版本是否是错误的(firstBadVersion1是我写的,难受啊越学越菜啊) class Solution(object): de 阅读全文
posted @ 2021-01-26 09:27 楠海 阅读(50) 评论(0) 推荐(0)
摘要:将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。(一开始没思路,然后看了一下官方题解,稀里糊涂就做出来了,我也蒙了) # Definition for a binary tree node. class TreeNode(object): def __init__(self, x): s 阅读全文
posted @ 2021-01-25 13:40 楠海 阅读(43) 评论(0) 推荐(0)
摘要:给定一个二叉树,检查它是否是镜像对称的。(随便看看吧,这个题真滴难-只有最后两个是对的前面的都不是对的) # Definition for a binary tree node. class TreeNode(object): def __init__(self, val=None, left=No 阅读全文
posted @ 2021-01-22 14:55 楠海 阅读(58) 评论(0) 推荐(0)
摘要:如何判断一个链表是回文链表。(第一个我写的,下面我根据其他人改编的) # Definition for singly-linked list. class ListNode(object): def __init__(self, val=0, next=None): self.val = val s 阅读全文
posted @ 2021-01-21 10:36 楠海 阅读(113) 评论(0) 推荐(0)
摘要:设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。(第一个我写的,但是在获取最小值上面时间耗时太久了) class MinStack1(object): def __init__(self): """ initialize your data structure 阅读全文
posted @ 2021-01-20 14:14 楠海 阅读(46) 评论(0) 推荐(0)
摘要:编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 '1' 的个数(第一个我弄得,但是二进制更好) class Solution(object): def hammingWeight1(self, n): """ :type n: int :rtype: int 阅读全文
posted @ 2021-01-19 14:38 楠海 阅读(45) 评论(0) 推荐(0)
摘要:将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。(我写的第一个) class Solution(object): def mergeTwoLists1(self, l1, l2): """我的思路是,既然要合并,直接遍历出来,排序重构 :type l1 阅读全文
posted @ 2021-01-19 14:00 楠海 阅读(48) 评论(0) 推荐(0)
摘要:通过一个函数来查找数组中所有的字符串最长公共前缀。(第一个是我写的,后面有几个很经典的,看不懂请看力扣官网) class Solution(object): def longestCommonPrefix1(self, strs): """ :type strs: List[str] :rtype: 阅读全文
posted @ 2021-01-18 14:06 楠海 阅读(40) 评论(0) 推荐(0)
摘要:给定一个正整数 n ,输出外观数列的第 n 项。(前两个我写的,后面一个你猜,反正比我的好) class Solution(object): def countAndSay1(self, n): """ :type n: int :rtype: str """ if n == 1: return s 阅读全文
posted @ 2021-01-17 11:06 楠海 阅读(91) 评论(0) 推荐(0)
摘要:给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。(第一个我理解错了,然后看了以前代码,第二三个我跟根据官网改编的,最后的几个你猜啊) class Solution(object): def maxProfit1(self, prices): """ :type prices: Lis 阅读全文
posted @ 2021-01-15 15:55 楠海 阅读(67) 评论(0) 推荐(0)
摘要:反转一个单链表。(第一个我写的,其他的你猜) class Solution(object): def reverseList1(self, head): """ :type head: ListNode :rtype: ListNode """ new_head = ListNode(-1) tem 阅读全文
posted @ 2021-01-15 14:42 楠海 阅读(66) 评论(0) 推荐(0)
摘要:给定一个二叉树,判断其是否是一个有效的二叉搜索树。(写了一堆错误代码哎) class TreeNode(object): def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.ri 阅读全文
posted @ 2021-01-14 16:19 楠海 阅读(79) 评论(0) 推荐(0)
摘要:给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。(两个都是我写的,暴力破解搞得还可以写,但是kmp算法,我只能去网上找的改编成python代码) 估计没有十年脑血栓我是写 阅读全文
posted @ 2021-01-14 09:36 楠海 阅读(79) 评论(0) 推荐(0)
摘要:请你来实现一个 atoi 函数,使其能将字符串转换成整数。(前两个我写的,效率不是很高) class Solution(object): def myAtoi1(self, s): """ :type s: str :rtype: int """ temp = s.strip() if not te 阅读全文
posted @ 2021-01-12 16:34 楠海 阅读(105) 评论(0) 推荐(0)
摘要:给你一个整数数组 nums ,设计算法来打乱一个没有重复元素的数组。(这个我写的,但是我感觉用别人的随机算法是挺好,但是就是不知道原理) class Solution(object): def __init__(self, nums): """ :type nums: List[int] """ s 阅读全文
posted @ 2021-01-12 11:15 楠海 阅读(73) 评论(0) 推荐(0)
摘要:给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 true ;否则,返回 false 。(第一个我写的) class Solution(object): def isPowerOfThree1(self, n): """我的偷懒解法 :type n: int :rtype: boo 阅读全文
posted @ 2021-01-11 11:09 楠海 阅读(75) 评论(0) 推荐(0)
摘要:给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。(第二个不是我写的其他的都两个是我写的) class Solution(object): def isPalindrome1(self, s): """ :type s: str :rtype: bool """ len 阅读全文
posted @ 2021-01-08 18:09 楠海 阅读(61) 评论(0) 推荐(0)
摘要:给定一个 n × n 的二维矩阵表示一个图像。将图像顺时针旋转 90 度。(这次没有抄,都是我写的) class Solution(object): def rotate1(self, matrix): """ :type matrix: List[List[int]] :rtype: None D 阅读全文
posted @ 2021-01-08 16:18 楠海 阅读(62) 评论(0) 推荐(0)
摘要:统计所有小于非负整数 n 的质数的数量。 (前三个我写的[其实就以前看过]超时了,后面自己想吧) class Solution(object): def countPrimes1(self, n): """ :type n: int :rtype: int """ if n <= 2: return 阅读全文
posted @ 2021-01-07 17:09 楠海 阅读(97) 评论(0) 推荐(0)