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=' ')
相关推荐
Lsk_Smion7 小时前
力扣实训 _ [102].层序遍历--前序--后续_递归与非递归的实现
数据结构·算法·leetcode
Lsk_Smion8 小时前
力扣实训 _ [25].K个一组链表
数据结构·链表
小欣加油9 小时前
leetcode3751 范围内总波动值I
java·数据结构·c++·算法·leetcode
V搜xhliang024611 小时前
临床科研新范式:从选题到投稿,AI智能体如何接管全流程?
运维·数据结构·人工智能·算法·microsoft·数据挖掘·自动化
梦想的颜色12 小时前
MySQL 查询性能核武器
运维·服务器·数据结构·数据库·mysql
八解毒剂13 小时前
查找-从二分查找到二叉排序树
数据结构·c++·算法
_日拱一卒14 小时前
LeetCode:78子集
数据结构·算法·leetcode·职场和发展
FuckPatience16 小时前
C# new List<T>(IEnumerable<T> collection),链表初始化时传入已存在链表
链表·c#·list
東隅已逝,桑榆非晚16 小时前
数据结构:算法效率与复杂度分析详解
数据结构·笔记·算法
半夜修仙16 小时前
分治思想对数组进行排序-归并排序
数据结构·算法·排序算法