面试题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. Python中的encode和decode

    原文地址:http://www.cnblogs.com/tingyugetc/p/5727383.html 1.Python3中对文本和二进制数据进行了比较清晰的区分,文本总是 unicode ,由  ...

  2. PANIC: ANDROID_SDK_HOME is defined but could not find Nexus_5_API_23.ini file in $ANDROID_SDK_HOME\

    运行模拟器总是出现这个错误 后来把系统环境变量中的ANDROID_SDK_HOME 删掉就好了 我去,好神奇的操作

  3. linux epoll ET边沿触发

    /***EPOLL ET 触发必须使用非阻塞,LT触发可以阻塞/非阻塞.*read 函数 非阻塞读需 忙轮寻 soket关闭返回0,循环读完数据*如果已经读完再读read返回 -1,errno=11( ...

  4. [tire+最短路]Bless You Autocorrect!

    [tire+最短路]Bless You Autocorrect! Typing on phones can be tedious. It is easy to make typing mistakes ...

  5. Mysql_常规操作

    001.数据库 全局操作 # 连接数据库: # mysql -h主机地址 -u用户名 -p(登陆用户密码)     ​# 修改用户密码 mysqladmin # mysqladmin -u root ...

  6. python编程练习题目

    github上面的一个项目,分为level1,level2,level3 三个等级的难度. 题目地址 一部分中文翻译 python教程 剑指offer,python3实现 python进阶 练习题1: ...

  7. text-overflow属性

    text-overflow属性有两个值, 默认值是clip:当对象内文本溢出时不显示裁切掉. 另一个就是:ellipsis:对象内文本溢出时显示省略标记(...). 使用text-overflow:e ...

  8. 什么是CDN

    1.什么是cdn    cdn全称是内容分发网络.其目的是让用户能够更快速的得到请求的数据.简单来讲,cdn就是用来加速的,他能让用户就近访问数据,这样就更更快的获取到需要的数据.举个例子,现在服务器 ...

  9. java高并发之线程池

    Java高并发之线程池详解   线程池优势 在业务场景中, 如果一个对象创建销毁开销比较大, 那么此时建议池化对象进行管理. 例如线程, jdbc连接等等, 在高并发场景中, 如果可以复用之前销毁的对 ...

  10. JS如何按时间粒度获取date的时间差

    获取date类型 var type = _xAxis[0].split(/-|:|\s/).length; 按时间粒度获取date的时间差 function getDateDiffByType(new ...