剑指offer——优化时间与空间效率

面试题49:丑数

  • 题目描述:

把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

  • 详细代码:
# -*- coding:utf-8 -*-
class Solution:
    def GetUglyNumber_Solution(self, index):
        # write code here
        if index <= 0:
        	return 0

		res = [1]
		t1 = t2 = t3 =0
		nextIndex = 1
		while nextIndex < index:
			min_val = min(res[t1] * 2, res[t2] * 3, res[t3] * 5)
			res.append(min_val)
			while res[t1] * 2 <= min_val:
				t1 += 1
			while res[t2] * 3 <= min_val:
				t2 += 1
			while res[t3] * 5 <= min_val:
				t3 += 1
			nextIndex += 1
		return res[index - 1] 

面试题50:第一个只出现一次的字符

  • 题目描述:

在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置。

  • 详细代码:
# -*- coding:utf-8 -*-
class Solution:
    def FirstNotRepeatingChar(self, s):
        # write code here
        if len(s) <= 0:
        	return -1
        
        dict = {}
        for i in s:
        	if i in dict:
        		dict[i] += 1
        	else:
        		dict[i] = 1
        for index, val in enumerate(s):
        	if dict[val] == 1:
        		return index
        
        return -1

题目拓展:字符流中第一个只出现一次的字符

  • 题目描述:

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。如果当前字符流没有存在出现一次的字符,返回#字符。

  • 详细代码:
# -*- coding:utf-8 -*-
class Solution:
	def __init__(self):
		self.alist = []
		self.dict = {}

	def Insert(self, char):
		if char in self.dict.keys():
			self.dict[char] = 2
		else:
			self.dict[char] = 1
			self.alist.append(char)

	def FirstAppearingOnce(self):
		while len(self.alist) > 0 and self.dict[self.alist[0]] > 1:
			self.alist.pop(0)
		if len(self.alist) > 0:
			return self.alist[0]
		return '#"

面试题51:数组中的逆序对

  • 题目描述:

在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007

  • 详细代码:(python编程超时)
# -*- coding:utf-8 -*-
class Solution:
    def InversePairs(self, data):
        # write code here
        if len(data) <= 0:
        	return 0

		copy = []
		count = 0
		for i in range(len(data)):
			copy.append(data[i])
		i = 0
		while len(copy) > i:
			count += data.index(copy[i])
			data.remove(copy[i])
			i += 1
		return count % 1000000007			

面试题52:两个链表的第一个公共节点

  • 题目描述:

输入两个链表,找出它们的第一个公共节点。

  • 详细代码:
# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def FindFirstCommonNode(self, pHead1, pHead2):
        # write code here
        len1 = self.GetLength(pHead1)
        len2 = self.GetLength(pHead2)
        if len1 > len2:
        	pLong = pHead1
        	pShort = pHead2
        else:
        	pLong = pHead2
        	pShort = pHead1
        
        d = abs(len1 - len2)
        for i in range(d):
        	pLong = pLong.next
        whiel pLong and pShort and pLong != pShort:
        	pLong = pLong.next
        	pShort = pShort.next
        return pLong

	def GetLength(self, pHead):
		length = 0
		while pHead:
			pHead = pHead.next
			length += 1
		return length
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值