【面试题】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)}')
相关推荐
测试19982 小时前
软件测试 - 单元测试总结
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
曲幽4 小时前
我用了FastApiAdmin后,连夜把踩过的坑都整理出来了
redis·python·postgresql·vue3·fastapi·web·sqlalchemy·admin·fastapiadmin
前端若水6 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
涛声依旧-底层原理研究所7 小时前
残差连接与层归一化通俗易懂的详解
人工智能·python·神经网络·transformer
csdn_aspnet7 小时前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展
fantasy_arch7 小时前
pytorch人脸匹配模型
人工智能·pytorch·python
熊猫_豆豆7 小时前
广义相对论水星近日点进动完整详细数学推导
python·天体·广义相对论
web3.08889997 小时前
1688 图搜接口(item_search_img / 拍立淘) 接入方法
开发语言·python
AI算法沐枫8 小时前
深度学习python代码处理科研测序数据
数据结构·人工智能·python·深度学习·决策树·机器学习·线性回归
X1A0RAN9 小时前
解决Pycharm中部分文件或文件夹被隐藏不展示问题
ide·python·pycharm