面试题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的更多相关文章

  1. #刷题记录--剑指 Offer 07. 重建二叉树

    输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字. 抓住一点,通过递归进行节点创建时,是按照 前序遍历数组 进行创建的. 根节点,根节点的左 ...

  2. 二维数组的查找,刷题成功——剑指Offer

    今天又做了一道题目,通过啦,欧耶! https://www.nowcoder.net/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&tqI ...

  3. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  4. Leetcode刷题记录(python3)

    Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...

  5. leetcode 刷题记录(java)-持续更新

    最新更新时间 11:22:29 8. String to Integer (atoi) public static int myAtoi(String str) { // 1字符串非空判断 " ...

  6. leetcode刷题记录——数组与矩阵

    @ 目录 283. 移动零 566. 重塑矩阵 485. 最大连续1的个数 240. 搜索二维矩阵 II 378. 有序矩阵中第K小的元素 645. 错误的集合 287. 寻找重复数 667. 优美的 ...

  7. LeetCode刷题记录(python3)

    由于之前对算法题接触不多,因此暂时只做easy和medium难度的题. 看完了<算法(第四版)>后重新开始刷LeetCode了,这次决定按topic来刷题,有一个大致的方向.有些题不止包含 ...

  8. LeetCode 刷题记录(二)

    写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...

  9. LeetCode 刷题记录

    写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...

随机推荐

  1. LeetCode No.160,161,162

    No.160 GetIntersectionNode 相交链表 题目 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 输入:intersectVal ...

  2. spring 事物面试题

    1.spring 事物管理器中事物传播机制 2.spring中事物的隔离级别 读未提交-事物未提交,另一个事物可以读取到,脏读 读已提交-事物已提交,先前读取的数据与后来读取的数据不同,不可重复读 可 ...

  3. ant design for vue 解决 vue.esm.js?c5de:628 [Vue warn]: Invalid prop: custom validator check failed for prop "defaultValue". 的错误

    错误重现: 在使用ant design for vue 的选择器插件的时候, 设置默认为为id(为数字) 报错: 解决办法: id为数字, 而defaultValue 的key 值必须为字符串, 将i ...

  4. 学习数论 HDU 4709

    经过杭师大校赛的打击,明白了数学知识的重要性 开始学习数论,开始找题练手 Herding HDU - 4709 Little John is herding his father's cattles. ...

  5. 哈夫曼编码的理解(Huffman Coding)

    哈夫曼编码(Huffman Coding),又称霍夫曼编码,是一种编码方式,可变字长编码(VLC)的一种.Huffman于1952年提出一种编码方法,该方法完全依据字符出现概率来构造异字头的平均长度最 ...

  6. myeclipse 编写java代码提示 dead code 原因

    经常使用MyEclipse或Eclipse编辑器编写java代码的程序员,可能经常遇到一个黄线警告提示:dead code:一般程序员遇到这些问题都会置之不理,反正也不影响程序的编译执行.对,这不是b ...

  7. 6.Redis集群

    redis-cluster[集群]架构图 redis-cluster投票:容错 搭建Ruby环境 集群的搭建过程 连接集群 查看集群的命令 1.1 redis-cluster[集群]架构图 架构细节: ...

  8. docker容器中安装中文字体

    在项目中用到pdf导出功能,需要安装中文字体,项目使用docker部署,为了方便决定在将字体安装在镜像中. 1.在dockerfile文件中添加字体copy语句(本次用是的宋体,字体源文件放在dock ...

  9. 第二类错误|检验统计量|左偏|右偏|P值

    6 第二类错误在H0中的假设值差别越大时增大? 不对,第二类错误在H0中的假设值差别越大时变小. 检验统计量有哪些? 根据假设内容确定是左偏还是右偏? P值是在原假设为真的条件下,检验统计量大于或等于 ...

  10. ORs-3-OR Gene Family Phylogeny

    OR Gene Family Phylogeny 1.之前关于ORs基因构建系统生发树的研究中的不足:bootstrap support values在有些family中高,bootstrap sup ...