面试题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. PAT Advanced 1097 Deduplication on a Linked List (25) [链表]

    题目 Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplica ...

  2. List和Map集合详细分析

    1.Java集合主要三种类型(两部分): 第一部分:Collection(存单个数据,只能存取引用类型) (1).List :是一个有序集合,可以放重复的数据:(存顺序和取顺序相同) (2).Set ...

  3. dht算法原理描述

    dht原理 dht是P2P网络(结构化P2P)核心路由算法,主要是利用一致性hash,把节点和资源都表示成一个hash值,放入到这个大的hash环中,每个节点负责路由靠近它的资源. 一.重要概念:  ...

  4. 吴裕雄--天生自然TensorFlow高层封装:Keras-RNN

    # 1. 数据预处理. from keras.layers import LSTM from keras.datasets import imdb from keras.models import S ...

  5. 基于邻接表的长度为k的简单路径的求解

    描述 一个连通图采用邻接表作为存储结构.设计一个算法,判断无向图中任意给定的两点是否存在一条长度为k的简单路径. 输入 多组数据,每组m+3数据行.第一行有两个数字n,m和k,代表有n个顶点,m条边和 ...

  6. 【转】修改Ubuntu系统的登陆信息的简单方法

    转自http://www.jb51.net/os/Ubuntu/414663.html Ubuntu的登陆和欢迎信息控制/etc/issue和/etc/motd/etc/issue与/etc/motd ...

  7. java类为什么要实现Serializable接口

    什么是Serializable接口? 一个对象序列化的接口.一个类只有实现了Serializable接口,它的对象才能被序列化. 什么是序列化? 将对象的状态信息转换为可以存储或传输的形式的过程. 在 ...

  8. [ZJOI2019]Minimax搜索(线段树+动态DP+树剖)

    为什么我怎么看都只会10pts?再看还是只会50~70?只会O(n2(R-L+1))/O(nlogn(R-L+1))……一眼看动态DP可还是不会做…… 根节点的答案是叶子传上来的,所以对于L=R的数据 ...

  9. log4j日志配置和使用

    一.日志配置变量参数说明 1. 日志设置说明:# log4j.rootLogger = debug,stdout,D,E# 等号之后的值表示appender对象,每个apperder对象表示一个日志输 ...

  10. day48-线程-信号量

    #1.信号量,用来保证多个线程不会互相冲突. #2.迷你唱吧:每次只能有两人在里面唱k: from threading import Thread from threading import Sema ...