【面试题】python代码

1.99乘法表

复制代码
for i in range(1, 10):
    for j in range(1, i+1):
        print(f'{i}*{j}={i*j}', end=' ')
    print()

2.求1-100内能被7整除和包含7的数

复制代码
l = []
for i in range(7,101):
    if i % 7 == 0:
        l.append(i)
    elif i % 10 == 7:
        l.append(i)
print(l)

3. 判断回文数的三种方法

双指针(时间复杂度和空间复杂度均为O(n)
复制代码
    def func(num):
        if not num:
            return False
        else:
            s = str(num)
            start_index = 0
            end_index = len(s)-1
            while start_index <= end_index:
                if s[start_index] != s[end_index]:
                    return False
                start_index += 1
                end_index -= 1
            return True


    print(func('0'))
逆序(时间复杂度和空间复杂度均为O(n))
复制代码
def func(s):
    if not s:
        return False
    s = str(s)
    return s[::] == s[::-1]


print(func('srtrs'))

4.给定字符串,不论大小写,统计文中出现的字符个数。

时间复杂度O(n).
  • 因为需要for循环遍历一次所有的字符,n为字符串长度
空间复杂度O(n)
  • 因为最坏的情况下,所有字符不同

    def func(s):
    char_dic = {}
    if not s:
    return None
    else:
    s = s.lower()
    for i in s:
    if i not in char_dic:
    char_dic[i] = 1
    else:
    char_dic[i] += 1
    return char_dic

    if name == 'main':
    t = "12w234WooOuU"
    print(f'字符个数分别是:{func(t)}')

5.1返回列表中的多数元素,多数元素是指元素重复出现的次数大于列表长度的一半

时间复杂度O(n)
  • 因为所有操作主要是线性操作的。for循环遍历可迭代对象中所有元素
空间复杂度O(n)
  • 针对字段list_dic和element,最坏的情况是存储所有元素。

    def func(obj):
    list_dic = {}
    if not obj:
    return None
    elif len(obj) == 1:
    return obj[0]
    else:
    for i in obj:
    if i not in list_dic:
    list_dic[i] = 1
    else:
    list_dic[i] += 1
    element = [key for key, value in list_dic.items() if value > len(obj)/2]
    return element

5.2返回列表中的多数元素,多数元素是指元素重复出现的次数大于列表长度的一半

时间复杂度O(n)
  • for循环遍历所有元素

空间复杂度O(1)
  • candidate变量只会存储一个值

复制代码
  def func(obj):
      candidate = None
      count = 0
      # 找出可能的多数元素.由于多数元素的数量超过列表长度的一半,所以即使其他元素试图"抵消"它的计数,它最终也会成为候选。
      for num in obj:
          if not count:
              candidate = num
              count += 1
          elif num == candidate:
              count += 1
          else:
              count -= 1
      # 判断是否为真的多数元素
      count = 0
      for num in obj:
          if num == candidate:
              count += 1
      if count > len(obj)//2:
          return candidate
      else:
          return None


  if __name__ == '__main__':
      t = [3, 3, 4, 2, 4, 4, 2, 4, 4]
      print(f'多数元素是:{func(t)}')
相关推荐
明月看潮生15 分钟前
青少年编程与数学 02-016 Python数据结构与算法 25课题、量子算法
python·算法·青少年编程·量子计算·编程与数学
水w17 分钟前
【Python爬虫】详细入门指南
开发语言·爬虫·python·scrapy·beautifulsoup
weixin_4450547222 分钟前
力扣刷题-热题100题-第35题(c++、python)
c++·python·leetcode
_x_w1 小时前
【17】数据结构之图及图的存储篇章
数据结构·python·算法·链表·排序算法·图论
pianmian12 小时前
arcgis几何与游标(1)
开发语言·python
冬天vs不冷2 小时前
SpringBoot条件注解全解析:核心作用与使用场景详解
java·spring boot·python
nanzhuhe2 小时前
python中参数前**的含义
开发语言·python
hello_ejb32 小时前
聊聊Spring AI Alibaba的PdfTablesParser
windows·python·spring
Kairo_012 小时前
秘密任务 2.0:如何利用 WebSockets + DTOs 设计实时操作
python
ghost1432 小时前
Python自学第2天:条件语句,循环语句
开发语言·python·学习