【面试题】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)}')
相关推荐
MageGojo3 小时前
百度热搜API接入实战:数据结构解析与工程化调用指南
python·数据抓取·api集成·热点数据·接口调试
TechWayfarer3 小时前
查IP归属地接入实战:保险理赔如何做动态风险监控与预警
网络·python·tcp/ip·安全·flask
speop3 小时前
AMD | task02
python
lili00123 小时前
2026 企业 AI 选型新范式:OpenRouter Fusion 证明多模型融合性价比远超单模型,企业该如何重构技术栈? - 微元算力(weytoken)
java·人工智能·python·重构·ai编程
Keano Reurink3 小时前
搜索API与GSC数据对比:发现数据盲区
数据库·python·数据挖掘
大黄说说4 小时前
深入理解 Go 协程 Goroutine:并发编程的核心精髓
java·数据库·python
cd988804 小时前
2026年,电销机器人哪家强?
python
搏博4 小时前
多传感器融合基础之一图像空间(Image Space)全面解析
图像处理·python·图像空间·融合感知
2601_961875244 小时前
花生十三资源盘|电子版|全科
python·django·flask·virtualenv·scikit-learn·pygame·tornado
郝学胜-神的一滴4 小时前
完全二叉树与堆底层原理深度剖析 | 手写C++大顶堆实现
java·开发语言·数据结构·c++·python·算法