Leetcode刷题记录 剑指offer
面试题3:数组中重复数字
# 使用set,时间复杂度O(n),空间复杂度O(n)
class Solution(object):
def findRepeatNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a = set([])
for num in nums:
if num in a:
return num
a.add(num)
# 桶思想,时间复杂度O(n),空间复杂度O(1)
class Solution(object):
def findRepeatNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
for i in range(len(nums)):
val = nums[i]
if val != i and val == nums[val]:
return val
nums[i], nums[val] = nums[val], nums[i]
面试题4:二维数组中的查找
# 从右上往左下推
class Solution(object):
def findNumberIn2DArray(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if matrix == [] or matrix == [[]]:
return False
c = len(matrix[0])-1
l = 0
while True:
if matrix[l][c] == target:
return True
if matrix[l][c] > target:
c -= 1
else:
l += 1
if l > len(matrix)-1 or c < 0:
return False
面试题5:替换空格
class Solution(object):
def replaceSpace(self, s):
"""
:type s: str
:rtype: str
"""
l = [''] * len(s)
for i in range(len(s)):
if s[i] == ' ':
l[i] = '%20'
else:
l[i] = s[i]
return ''.join(l)
面试题6:从尾到头打印链表
# 非递归
class Solution(object):
def reversePrint(self, head):
"""
:type head: ListNode
:rtype: List[int]
"""
result = []
while head:
result.append(head.val)
head = head.next
return result[::-1]
# 递归
class Solution(object):
def reversePrint(self, head):
"""
:type head: ListNode
:rtype: List[int]
"""
result = []
def helper(root):
if not root:
return
helper(root.next)
result.append(root.val)
helper(head)
return result
面试题7:重建二叉树
class Solution(object):
def buildTree(self, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: TreeNode
"""
def helper(inc_start, inc_end):
if inc_start == inc_end:
return None
inc_value = preorder[self.pre_index]
root = TreeNode(inc_value)
self.pre_index += 1
root.left = helper(inc_start, inc_value_map[inc_value])
root.right = helper(inc_value_map[inc_value] + 1, inc_end)
return root
self.pre_index = 0
inc_value_map = {v: k for k, v in enumerate(inorder)}
return helper(0, len(inorder))
面试题9:用两个栈实现队列
class CQueue(object):
def __init__(self):
self.l1 = []
self.l2 = []
def appendTail(self, value):
"""
:type value: int
:rtype: None
"""
self.l2.append(value)
def deleteHead(self):
"""
:rtype: int
"""
if not self.l1:
if not self.l2:
return -1
for i in range(len(self.l2)):
self.l1.append(self.l2.pop())
return self.l1.pop()
面试题10-I:斐波那契数列
class Solution(object):
def fib(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0:
return 0
a = 0
b = 1
while n != 1:
a, b = b, a+b
n -= 1
return b % 1000000007
面试题10-II:青蛙跳台阶问题
class Solution(object):
def numWays(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0:
return 1
a = 1
b = 1
while n != 1:
a, b = b, a+b
n -= 1
return b % 1000000007
Leetcode刷题记录 剑指offer的更多相关文章
- #刷题记录--剑指 Offer 07. 重建二叉树
输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字. 抓住一点,通过递归进行节点创建时,是按照 前序遍历数组 进行创建的. 根节点,根节点的左 ...
- 二维数组的查找,刷题成功——剑指Offer
今天又做了一道题目,通过啦,欧耶! https://www.nowcoder.net/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&tqI ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
- leetcode 刷题记录(java)-持续更新
最新更新时间 11:22:29 8. String to Integer (atoi) public static int myAtoi(String str) { // 1字符串非空判断 " ...
- leetcode刷题记录——数组与矩阵
@ 目录 283. 移动零 566. 重塑矩阵 485. 最大连续1的个数 240. 搜索二维矩阵 II 378. 有序矩阵中第K小的元素 645. 错误的集合 287. 寻找重复数 667. 优美的 ...
- LeetCode刷题记录(python3)
由于之前对算法题接触不多,因此暂时只做easy和medium难度的题. 看完了<算法(第四版)>后重新开始刷LeetCode了,这次决定按topic来刷题,有一个大致的方向.有些题不止包含 ...
- LeetCode 刷题记录(二)
写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...
- LeetCode 刷题记录
写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...
随机推荐
- docker---设置镜像加速器
国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器,国内很多云服务商都提供了国内加速器服务,如: Azure 中国镜像: https://dockerhub.azk8s.cn ...
- Python入门方法推荐,哪些基础知识必学?
很多想入门的小伙伴还不知道Python应该怎么学,哪些知识必学,今天我们就来盘点一下. 01.入门方法推荐 总体来讲,找一本靠谱的书,由浅入深,边看边练. 网上的学习教程有很多,多到不知道如何选择.所 ...
- drf序列化单改-整改-局部改-群改接口的实现
整体单改 路由层.模型层.序列化层不需要做修改,只需要处理视图层:views.py """ 1) 单整体改,说明前台要提供修改的数据,那么数据就需要校验,校验的数据应该在 ...
- Maven--排除依赖
传递性依赖会给项目隐式地引入很多依赖,这极大地简化了项目依赖的管理,但是有些时候这种特性也会带来问题. 例如,当前项目有一个第三方依赖,而这个第三方的依赖由于某些原因依赖了另外一个类库的 SNAPSH ...
- memcached安装使用相关-php
1.windows下面: 为什么memcache官方没有for windows的版本下载地址,现在怎么办? https://segmentfault.com/q/1010000002219198 32 ...
- ESLint javascript格式要求
首行缩进2个空格 eslint: indent functionhello (name) { console.log('hi', name) } 字符串使用单引号(除了避免转义) eslint: qu ...
- SQL触发器笔记
触发器(Trigger)是在对表进行插入.更新.删除等操作时自动执行的存储过程. 触发器是一种特殊的存储过程,它在执行语言事件时自动生效,采用事件驱动机制.当某个触发事件发生时,定义在触发器中的功能将 ...
- CodeForces - 977E
题:https://codeforces.com/problemset/problem/977/E 题意:给你一个图,问你有几个没有杂边的单环(度全为2) 分析:单环点的度数一定是2,连续边,判断是否 ...
- Spire.doc jar包实现word文件添加水印demo
/** * @create: 2020-02-23 21:50 */ import com.spire.doc.*; import com.spire.doc.documents.WatermarkL ...
- tensorflow(四)
tensorflow数据处理方法, 1.输入数据集 小数据集,可一次性加载到内存处理. 大数据集,一般由大量数据文件组成,因为数据集的规模太大,无法一次性加载到内存,只能每一步训练时加载数据,可以采用 ...