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=' ')
相关推荐
maomi_952610 分钟前
《线性表、顺序表与链表》教案(C语言版本)
c语言·数据结构·链表
Run_Teenage19 分钟前
C语言 数据结构 【队列】动态模拟实现
数据结构
蒙奇D索大23 分钟前
【数据结构】邻接矩阵完全指南:原理、实现与稠密图优化技巧
c语言·数据结构·考研·图论·改行学it
半桔27 分钟前
哈希表(开散列)的实现
数据结构·c++·面试·散列表·哈希
牛奶咖啡.8542 小时前
树和图论(详细整理,简单易懂!)
数据结构·c++·算法·深度优先·图论
2401_881244403 小时前
416. 分割等和子集
数据结构·算法
butterfly_onfly3 小时前
C# 多线程并发编程基础
数据结构·算法·c#
python_chai6 小时前
Python核心数据结构详解:元组、集合与字典
java·数据结构·python
爱编程的王小美6 小时前
8876行详解数据结构,从入门到入坟
数据结构
编程绿豆侠8 小时前
力扣HOT100之链表:19. 删除链表的倒数第 N 个结点
算法·leetcode·链表