11.19.2024刷华为OD

文章目录

  • HJ51
  • [HJ53 杨辉三角](#HJ53 杨辉三角)
  • HJ56
  • [HJ57 高精度整数加法](#HJ57 高精度整数加法)
  • HJ58
  • [HJ60 简单题](#HJ60 简单题)
  • [HJ63 DNA序列(简单题)](#HJ63 DNA序列(简单题))
  • 语法知识记录

HJ51

https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d?tpId=37\&tags=\&title=\&difficulty=0\&judgeStatus=0\&rp=1\&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37

HJ53 杨辉三角

写打印杨辉三角,然后再判断就行了

HJ56

https://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84?tpId=37\&tags=\&title=\&difficulty=0\&judgeStatus=0\&rp=1\&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37

HJ57 高精度整数加法

https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6?tpId=37\&tags=\&title=\&difficulty=0\&judgeStatus=0\&rp=1\&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37

python 复制代码
a = input()
b = input()
lenA = len(a)
lenB = len(b)
if lenA >= lenB:
    length = lenA
    b = b.zfill(length)
if lenB >= lenA:
    length = lenB
    a = a.zfill(length)

# reverse
a = a[::-1]
b = b[::-1]
tag = 0
res = []

for ch1,ch2 in zip(a,b):
    add1 = int(ch1)
    add2 = int(ch2)
    if add1 + add2 + tag > 9:
        res.append(str(add1 + add2 + tag - 10))
        tag = 1
    else:
        res.append(str(add1 + add2 + tag))
        tag = 0
if tag == 1:
    res.append(str(tag))

res = res[::-1]
print(''.join(res))

HJ58

python越用越舒服,没什么难度

python 复制代码
dic1 = {}
str1 = input()
for ch in str1:
    if ch not in dic1:
        dic1[ch] = 1
    else:
        dic1[ch] += 1

list1 = []
for key,value in dic1.items():
    if value == 1:
        list1.append(key)

if len(list1) == 0:
    print(-1)
else:
    print(list1[0])

HJ60 简单题

一样是先写函数再改

HJ63 DNA序列(简单题)

https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a?tpId=37\&tags=\&title=\&difficulty=0\&judgeStatus=0\&rp=1\&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37

python 复制代码
def getGCnum(str1):
    length = len(str1)
    cnt = 0
    for ch in str1:
        if ch == 'C' or ch == 'G':
            cnt += 1
    return cnt

str1 = list(input())
n = int(input())

max_num = 0
for i in range(len(str1) - n + 1):
    sub_str = str1[i:i+n]
    # print(sub_str)
    gcNum = getGCnum(sub_str)
    if gcNum > max_num:
        res = sub_str
        max_num = gcNum
print(''.join(res))

语法知识记录

  • 这个语法是用来做多个输入案例的,当没有输入时,try会有异常
python 复制代码
    while True:
        try:

        except:
            break
  • Python 没有内置专门的 LinkedList、Queue、Stack 类,但它提供了非常灵活的数据结构和工具,可以轻松地实现这些数据结构。

  • Python 实现 LinkedList、Queue、Stack 的方式 使用内置的 list:

  • LinkedList: 可以用 list 来模拟链表,通过索引来访问元素,但插入和删除操作在列表中间会比较慢。 Queue: 用 list

    的 append() 方法在队尾添加元素,用 pop(0) 方法在队首删除元素来实现队列。 Stack: 用 list 的 append()

    方法入栈,用 pop() 方法出栈来实现栈。 使用 collections.deque:

  • deque 是双端队列,在列表头部和尾部插入或删除元素都非常高效。它比 list 更适合实现队列和栈。

  1. 一个做题的思路:

    快速写函数,然后在函数上面改

python 复制代码
    print(f"{i} ", end='')
    # print(i, end=' ')
相关推荐
不一样的故事1262 小时前
军工行业合规与基础认知
数据结构·经验分享·算法
闪电悠米14 小时前
力扣hot100-56.合并区间-排序详解
数据结构·算法·leetcode·贪心算法·排序算法
yuannl1016 小时前
图的存储方式
数据结构
s1805791636217 小时前
餐饮小程序GEO优化
华为od
会编程的小孩17 小时前
初识数据类型以及变量定义
数据结构·算法
梅梅绵绵冰17 小时前
数据结构-时间复杂度
数据结构·算法
cpp_250118 小时前
P6625 [省选联考 2020 B 卷] 卡牌游戏
数据结构·c++·算法·前缀和·贪心·洛谷题解·省选
起予者汝也19 小时前
Python 数据结构
开发语言·数据结构·python
Frostnova丶21 小时前
(17)LeetCode 41. 缺失的第一个正数
数据结构·算法·leetcode
程序猿乐锅1 天前
【数据结构与算法 | 第二篇】 双链表的增删改查
数据结构