
Leetcode
Cappuccanio
这个作者很懒,什么都没留下…
展开
-
Leetcode——删除排序数组中的重复项——python3
第二次题目:删除排序数组中的重复项class Solution: def removeDuplicates(self, nums): a = []; for num in nums: if num in a: continue else: ...原创 2018-07-09 11:13:24 · 739 阅读 · 0 评论 -
Leetcode——验证回文字符串——python3
# 验证回文字符串# 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。# 本题中,我们将空字符串定义为有效的回文串。class Solution(object): def is_palindrome(self, s): """ :type s: str :rtype: bool "...原创 2018-09-30 11:21:44 · 959 阅读 · 0 评论 -
Leetcode——字符串中的第一个唯一字符——python3
# 字符串中的第一个唯一字符class Solution(object): def first_uniq_char(self, s): # 方法一: # a = list(s) # b = [] # for i in range(len(a)): # b = a[:] # b = a 是浅拷...原创 2018-09-28 17:09:40 · 279 阅读 · 0 评论 -
Leetcode——颠倒整数——python3
# 颠倒整数# 给定一个 32 位有符号整数,将整数中的数字进行反转。class Solution(object): def reverse(self, x): # 提交通过 l = list(str(x)) # 把x作为字符串提取进来 n = len(l) if l[n - 1] != '0': # 判断末...原创 2018-09-28 11:20:21 · 174 阅读 · 0 评论 -
Leetcode——反转字符串——python3
# 反转字符串# 编写一个函数,其作用是将输入的字符串反转过来。class Solution(object): def reverse_string(self, s): # 方法一 a = list(s) # 将字符串转换乘列表 b = a[::-1] # 倒序输出列表 c = "".join(b) ...原创 2018-09-27 21:09:45 · 301 阅读 · 0 评论 -
Leetcode——存在重复元素——python3
# 给定一个整数数组,判断是否存在重复元素。# 如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。import collections # 导入python的标准库—collections模块的Counter类class Solution(object): def containsduplicate(self, nums):...原创 2018-09-22 17:13:27 · 481 阅读 · 0 评论 -
Leetcode——移动零——python3
# 给定一个数组,编写一个函数将所有0移动到数组的末尾。同时保持非零元素的相对顺序。class Solution(object): def move_zeroes(self,nums): # 方法一,简单 # n = nums.count(0) # for i in range(n): # nums.remov...原创 2018-09-22 15:14:29 · 287 阅读 · 0 评论 -
Leetcode——加一——python3
# 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一# 最高位数字存放在数组的首位,数组中每个元素只存储一个数字class Solution: def plusOne(self, digits): if len(digits) == 0: # 当这个矩阵为0的时候,直接化简操作 digits = [1] e...翻译 2018-09-22 14:48:34 · 162 阅读 · 0 评论 -
Leetcode——有效的数独——python3
# 有效的数独# 1.数字 1-9 在每一行只能出现一次。# 2.数字 1-9 在每一列只能出现一次。# 3.数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。class Solution(object): def is_valid_sudoku(self, board): # 将board中的.全部改成数字0 length ...原创 2018-09-27 17:03:28 · 253 阅读 · 0 评论 -
Leetcode——两个数组的交集_2——python3
# 给定两个数组,编写一个函数来计算它们的交集。# 说明:1.输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致。# 2.我们可以不考虑输出结果的顺序。class Solution(object): def intersect(self, nums1, nums2): import collections # 方法一:方法还有问...原创 2018-09-27 11:07:47 · 343 阅读 · 0 评论 -
Leetcode——字符串转整数——python3
# 实现atoi,将字符串转为整数# 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止。# 如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为整数的值。# 如果第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。# 字符串可以在形成整数的字符后面包括多余的字符,这些字符可以被忽略,它们对于函数...转载 2018-10-11 15:31:09 · 302 阅读 · 0 评论 -
Leetcode——买卖股票的最佳时机2——python3
# 买卖股票的最佳时机_2# 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。# 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。# 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。class Solution(object): def max_profit(self, prices): ...原创 2018-09-25 16:16:17 · 125 阅读 · 0 评论 -
Leetcode——旋转图像——python3
# 旋转图像# 给定一个 n × n 的二维矩阵表示一个图像,将图像顺时针旋转 90 度。# 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。class Solution(object): def rotate(self, matrix): # 第一种方法 length = len(matrix) #...原创 2018-09-25 11:16:09 · 418 阅读 · 0 评论 -
Leetcode——只出现一次的数字——python3
方法一:使用自带collections模块的counter函数,统计各元素出现的次数并返回一个键值对字典,再对字典进行遍历,选出其中出现次数小于2的元素。import collectionsclass Solution: def singleNumber(self,nums): dic = collections.Counter(nums); fo...原创 2018-08-06 16:00:24 · 535 阅读 · 0 评论 -
Leetcode——旋转数组——python3
第一种方法:将最后一位赋值给中间变量,然后将前一位赋值给后一位,依次类推。此种方法效率较低,耗时长。class Solution: def rotate(self, nums, k): """ :type nums: List[int] :type k: int :rtype: void Do not return anyth...原创 2018-07-15 21:28:17 · 764 阅读 · 0 评论 -
Leetcode——两数之和——python3
第一次写博客,也是记录在Leetcode上开始做题的时刻。class Solution(object): def twoSum(self, nums, target): dict = {} for index, num in enumerate(nums): # index是索引,num是内容 进行遍历 if target - n...原创 2018-07-06 10:48:06 · 694 阅读 · 0 评论 -
Leetcode——删除排序数组中的重复项_原地删除——python3
原地删除:删除数组元素的时候不需要额外的内存空间,空间复杂度为O(1)。(就是不占用内存的意思)class Solution: def removeDuplicates(self, nums): i = 0; while i < len(nums)-1: if nums[i] == nums[i+1]: ...原创 2018-07-09 13:42:28 · 1618 阅读 · 0 评论 -
Leetcode——strStr()——python3
# 实现strStr()# 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。class Solution(object): def str_str(self, haystack, needle): """ :type ...原创 2018-09-30 11:28:20 · 226 阅读 · 0 评论