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=' ')
相关推荐
一起努力啊~2 小时前
算法刷题--长度最小的子数组
开发语言·数据结构·算法·leetcode
小北方城市网2 小时前
第1课:架构设计核心认知|从0建立架构思维(架构系列入门课)
大数据·网络·数据结构·python·架构·数据库架构
好易学·数据结构2 小时前
可视化图解算法77:零钱兑换(兑换零钱)
数据结构·算法·leetcode·动态规划·力扣·牛客网
独自破碎E3 小时前
【归并】单链表的排序
数据结构·链表
L_09073 小时前
【C++】高阶数据结构 -- 平衡二叉树(AVLTree)
数据结构·c++
冰冰菜的扣jio3 小时前
Redis基础数据结构
数据结构·数据库·redis
Qhumaing3 小时前
C++学习:【PTA】数据结构 7-2 实验6-2(图-邻接表)
数据结构·c++·学习
方便面不加香菜3 小时前
基于顺序表实现通讯录项目
c语言·数据结构
MQLYES4 小时前
03-BTC-数据结构
数据结构·算法·哈希算法
无限进步_5 小时前
【数据结构&C语言】对称二叉树的递归之美:镜像世界的探索
c语言·开发语言·数据结构·c++·算法·github·visual studio