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=' ')
相关推荐
iqay2 小时前
【C语言】填空题/程序填空题1
c语言·开发语言·数据结构·c++·算法·c#
makabaka_T_T3 小时前
25寒假算法刷题 | Day1 | LeetCode 240. 搜索二维矩阵 II,148. 排序链表
数据结构·c++·算法·leetcode·链表·矩阵
wclass-zhengge4 小时前
04树 + 堆 + 优先队列 + 图(D1_树(D6_B树(B)))
数据结构·b树
qq_433618446 小时前
哈夫曼树
数据结构·算法
励志成为美貌才华为一体的女子7 小时前
python算法和数据结构刷题[4]:查找算法和排序算法
数据结构·算法·排序算法
tt5555555555558 小时前
每日一题-判断是不是完全二叉树
数据结构·算法
嘻嘻哈哈的zl9 小时前
初级数据结构:栈和队列
c语言·开发语言·数据结构
小王努力学编程9 小时前
【C++篇】哈希表
数据结构·哈希算法·散列表
因兹菜9 小时前
[LeetCode]day4 977.有序数组的平方
数据结构·算法·leetcode
_周游9 小时前
【数据结构】_时间复杂度相关OJ(力扣版)
数据结构