剑指offer——代码的完整性

1、面试题16:数值的整数次方

  • 题目描述:

实现函数double Power(double base, int exponent),求base的exponent次方、不得使用库函数,同时不需要考虑大数问题。

  • 详细代码:
# -*- coding:utf-8 -*-
class Solution:
    def Power(self, base, exponent):
        # write code here
        if base == 0.0 and exponent < 0:
        	return 0.0

		if exponent >= 0:
			return self.PowerwithExponent(base, exponent)
		else:
			return 1.0 / self.PowerwithExponent(base, -exponent)

	def PowerwithExponent(self, base, exponent):
		if exponent == 0.0:
			return 1.0
		if exponent == 1.0:
			return base
		result = self.PowerwithExponent(base, exponeny >> 1)
		result *= result
		if exponent & 0x1 == 1:
			result *= base
		return result

2、面试题17:打印从1到最大的n位数

  • 题目描述:

输入数字n,按顺序打印出从1到最大的n位十进制数,比如输入3,则打印出1、2、3一直到最大的3位数999.

  • 详细代码:
# -*- coding:utf-8 -*-
class Solution:
    def Print1ToMaxOfNDigits(self, n):
    	if n <= 0:
    		return
    	number = ['0'] * n
    	while not self.Increment(number):
    		self.PrintNumber(number)

	def Increment(self, number):
		isOverflow = False
		nTakeover = 0
		nLength = len(number)
		for i in range(nLength-1, -1, -1):
			nSum = int(number[i]) + nTakeover
			if i == nLength - 1:
				nSum += 1
			if nSum >= 10:
				if i == 0:
					isOverflow = True
				else:
					nTakeover = 1
					nSum -= 1
					number[i] = str(nSum)
			else:
				number[i] = str(nSum)
				break
		return isOverflow

	def PrintNumber(self, number):
		isBeginning = True
		nLength = len(number)
		for i in range(nLength):
			if isBeginning and number[i] != 0:
				isBeginning = False
			if not isBeginning:
				print('%c'%number[i])
		print('\t')			

3、面试题18:删除链表的节点

题目一:在O(1)时间内删除链表节点

  • 题目描述:

给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点。

  • 详细代码:
class ListNode:
    def __init__(self):
        self.value = None
        self.next_node = None

class Solution:
    def delete_node(self,head_node,del_node):
        """
        删除指定节点
        """
        if not (head_node and del_node):
        	return False
        #删除节点在链表中间
         if del_node.next_node:
        	del_node_next = del_node.next_node
         	del_node.value = del_node_next.value
         	del_node.next_node = del_node_next.next_node
         	del_node_next.value = None
         	del_node_next.noex_node = None

		#链表只有一个节点
		elif del_node == head_node:
			del_node = None
			head_node = None

		#删除节点是尾节点
		else:
			node = head_node
			while node.next != del_node:
				node = node.next_node
			node.next_node = None
			del_node = None
		return head_node

题目二:删除链表中重复的节点

  • 题目描述:

在一个排序的链表中,请删除重复的节点,如1-2-3-3-4-4-5在重复的节点被删除后为1-2-5。

  • 详细代码:
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        first = ListNode(-1)
        last = first
        first.next = head

		while head and head.nex:
			if head.val == head.next.val:
				val = head.val
				while head and head.val == val:
					head = head.next
				last.next = head

			else:
				last = head
				head = head.next
				
		return first.next

4、面试题19:正则表达式匹配

  • 题目描述:

请实现一个函数用来匹配包括’.‘和’‘的正则表达式。模式中的字符’.‘表示任意一个字符,而’'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但是与"aa.a"和"ab*a"均不匹配。

  • 详细代码:
class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        if not p:
        	return not s

		first_match = bool(s) and p[0] in {s[0], '.'}

		if len(p) > 1 and p[1] = '*':
			return (self.isMatch(s, p[2:]) or first_match and self.isMatch(s[1:], p))
		else:
			return first_match and self.isMatch(s[1:], p[1:])

5、面试题20:表示数值的字符串

  • 题目描述:

请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示数值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是。

  • 详细代码:
# -*- coding:utf-8 -*-
class Solution:
    # s字符串
    def isNumeric(self, s):
        # write code here
        if not s:
            return False

		alist = [i.lower() for i in s]
		
		if 'e' in alist:
			index = alist.index('e')
			front = alist[:index]
			behind = alist[index + 1:]
			
			if '.' in behind or len(behind) == 0:
				return False
			isfront = self.isDigit(front)
			isbehind = self.isDigit(behind)
			return isfront and isbehind

		else:
			return self.isDigit(alist) 

	def isDigit(self, alist):
		dotNum = 0
		allow_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '.']
		for i in range(len(alist)):
			if alist[i] not in allow_list:
				return False
			if alist[i] == '.':
				dotNum += 1
			if alist[i] in '+-' and i != 0:
				return False
		if dotNum > 1:
			return False
		return True

6、面试题21:调整数组的顺序使奇数位于偶数前面

  • 题目描述:

输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分。

  • 详细代码:
# -*- coding:utf-8 -*-
class Solution:
    def reOrderArray(self, array):
        # write code here
        pBegin = 0
        pEnd = len(array) - 1

		while pBegin < pEnd:
			while pBegin < pEnd and not self.isEven(array[pBegin]):
				pBegin += 1
			while pBegin < pEnd and self.isEven(array[pEnd]):
				pEnd -= 1
			if pBegin < pEnd:
				temp = array[pBegin]
				array[pBegin] = array[pEnd]
				array[pEnd] = temp

		return array

	def isEven(self, number):
		return number & 0x1 == 0
  • 其他题目:在上题的基础上,要求奇数和奇数,偶数和偶数的相对位置保持不变。

  • 详细代码:

#奇数和偶数的相对位置不变
# -*- coding:utf-8 -*-
class Solution:
    def reOrderArray(self, array):
        # write code here
		if not array:
			return array

		res1 = []
		res2 = []

		for i in array:
			if i & 0x1 != 0:
				res1.append(i)
			else:
				res2.append(i)

		return res1 + res2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Offer》 1. 赋值运算函数 2. 单例设计模式 3. 二维数组中查找目标值 4. 替换字符串中的空格 5. 从尾到头打印链表 6. 由前序和中序遍历重建二叉树 7. 用两个栈实现队列 8. 求旋转数组的最小数字 9. 斐波那契数列的第n项(青蛙跳台阶) 10. 二进制中1的个数 11. 数值的整数次方 12. 打印1到最大的n位数 13. O(1)时间删除链表节点 14. 使数组中的奇数位于偶数前面 15. 找链表中倒数第K个节点 16. 输出反转后的链表 17. 合并两个有序链表 18. 判断二叉树A中是否包含子树B 19. 二叉树的镜像 20. 顺时针打印矩阵 21. 包含min函数的栈 22. 判断一个栈是否是另一个栈的弹出序列 23. 层序遍历二叉树 24. 后序遍历二叉搜索树 25. 二叉树中和为某值的路径 26. 复杂链表的复制 27. 二叉搜索树转换为双向链表 28. 打印字符串中所有字符的排列 29. 数组中出现次数超过一半的数字 30. 找出最小的K个数 31. 连续子数组的最大和 32. 从1到整数n中1出现的次数 33. 把数组中的数排成一个最小的数 34. 求第N个丑数 35. 第一个出现一次的字符 36. 数组中逆序对的个数 37. 两个链表的第一个公共节点 38. 数字在排序数组中出现的次数 39. 二叉树的深度 40. 数组中只出现一次的两个数,而其他数都出现两次 41. 和为s的连续整数序列 42. 翻转字符串 43. n个骰子的点数及出现的概率 44. 扑克牌的顺子 45. 圆圈中最后剩下的数 46. 1+2+3+...+n的和 47. 不用加减乘除做加法 48. 不能被继承的类 49. 字符串转换为整数 50. 树中两个节点的最低公共祖先 51. 找出重复的数 52. 构建乘积数组 53. 正则表达式匹配 54. 表示数值的字符串 55. 字符流中第一个不重复的字符 56. 链表中环的入口节点 57. 删除链表中重复的节点 58. 二叉树的下一个节点 59. 对称的二叉树 60. 按之字形顺序打印二叉树 61. 把二叉树打印成多行 62. 序列化二叉树 63. 二叉搜索树的第K个节点 64. 数据流中的中位数 65. 滑动窗口的最大值 66. 矩阵中的路径 67. 机器人的运动范围

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值