第 1 题:两数之和
问题描述
给定一个整数数组和一个目标值,请找出数组中两个数,使它们的和等于目标值,输出这两个数的下标(从 0 开始)。数据保证唯一解。
输入格式
第一行:n 表示数组长度第二行:n 个整数,表示数组元素第三行:一个整数,表示目标值 target
输出格式
一行两个整数,用空格隔开,表示两个数的下标
样例输入
plaintext
4 2 7 11 15 9样例输出
plaintext
0 1
python
n=int(input())
a=list(map(int,input().split()))
target=int(input())
for i in range(n):
for j in range(i+1,n):
if a[i]+a[j]==target:
print(i,j)
break
第 2 题:移除元素
问题描述
给定一个数组和一个值 val,原地移除所有等于 val 的元素,返回新数组的长度。
输入格式
第一行:n 表示数组长度第二行:n 个整数,表示数组元素第三行:一个整数 val
输出格式
一个整数,表示移除后的新长度
样例输入
plaintext
4 3 2 2 3 3样例输出
plaintext
2
python
n = int(input())
nums = list(map(int, input().split()))
val = int(input())
cnt = 0
for x in nums:
if x != val:
cnt += 1
print(cnt)
第 3 题:搜索插入位置
问题描述
给定一个升序数组和目标值,若存在返回下标;不存在返回它应该被插入的位置。
输入格式
第一行:n 表示数组长度第二行:n 个升序整数第三行:目标值 target
输出格式
一个整数,表示下标或插入位置
样例输入
plaintext
4 1 3 5 6 5样例输出
plaintext
2
pythonn=int(input()) arr=list(map(int,input().split())) target=int(input()) for i in range(n): if arr[i]>=target: print(i) exit() print(n)
第 4 题:最后一个单词的长度
问题描述
给定一行字符串,单词由空格隔开,求最后一个单词的长度。无单词则输出 0。
输入格式
一行字符串(可能包含空格)
输出格式
一个整数,表示最后一个单词长度
样例输入
plaintext
Hello World样例输出
plaintext
5
pythons=input() word=s.split() if len(word)==0: print(0) else: print(len(word[-1]))
第 5 题:验证回文串
问题描述
给定一个字符串,只看字母和数字,忽略大小写、符号、空格,判断是否为回文串。是输出 True,否则输出 False。
输入格式
一行字符串
输出格式
True 或 False
样例输入
plaintext
A man, a plan, a canal: Panama
样例输出
plaintext
True
python
s=input().lower()
t=''
for c in s:
if c.isalnum():
t+=s
print(t==t[::-1])
第 6 题:移动零
问题描述
将数组中所有 0 移到末尾,非零元素保持原顺序,输出处理后的数组。
输入格式
第一行:n 表示数组长度第二行:n 个整数
输出格式
一行整数,用空格隔开
样例输入
plaintext
5 0 1 0 3 12样例输出
plaintext
1 3 12 0 0
python
n=int(input())
nums=list(map(int,input().split()))
a=[]
b=[]
for x in nums:
if x !=0:
a.append(x)
else:
b.append(x)
res=a+b
print(' '.join(map(str,res)))
题目名称
在排序数组中查找元素的第一个和最后一个位置
问题描述
给定一个非递减排序 的整数数组,找到目标值 target 第一次出现的位置和最后一次出现的位置。如果数组中不存在 target,输出
-1 -1。
输入格式
第一行:一个整数 n,表示数组长度第二行:n 个非递减排列的整数第三行:一个整数 target
输出格式
一行两个整数,表示第一次出现位置 和最后一次出现位置 ,用空格隔开。不存在则输出
-1 -1。
样例输入
plaintext
6 5 7 7 8 8 10 8样例输出
plaintext
3 4
样例输入 2
plaintext
6 5 7 7 8 8 10 6样例输出 2
plaintext
-1 -1
python
n=int(input())
arr=list(map(int,input().split()))
target=int(input())
first=-1
for i in range(n):
if arr[i]==target:
first=i
break
last=-1
for i in range(n):
if arr[i]==target:
last=i
print(first,last)
问题描述
给定一个只包含 0、1、2 的数组,请你将它们按 0、1、2 顺序排列,输出排序后的数组。(对应原题:荷兰国旗问题)
输入格式
第一行:一个整数 n第二行:n 个整数,只包含 0、1、2
输出格式
一行整数,按 0、1、2 顺序排列,空格分隔
样例输入
plaintext
6 2 0 2 1 1 0样例输出
plaintext
0 0 1 1 2 2
python
n=int(input())
arr=list(map(int,input().split()))
arr.sort()
print(' '.join(map(str,arr)))
python
n=int(input())
arr=list(map(int,input().split()))
res=[]
for x in arr:
if x==0:
res.append(x)
for x in arr:
if x==1:
res.append(x)
for x in arr:
if x==2:
res.append(x)
print(' '.join(map(str,arr)))
题目:合并两个有序数组
问题描述
给定两个非递减排序 的整数数组,请把它们合并成一个非递减排序的数组,并输出。
输入格式
第一行:n 表示第一个数组长度第二行:n 个整数第三行:m 表示第二个数组长度第四行:m 个整数
输出格式
输出合并并排序后的数组,空格隔开
python
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
# 合并 + 直接排序,完事!
c = a + b
c.sort()
print(' '.join(map(str, c)))
问题描述
给定一个数组,找出数组中第 K 个最大的元素。
输入格式
第一行:n 表示数组长度第二行:n 个整数第三行:整数 K
输出格式
一个整数,表示第 K 大的数
样例输入
plaintext
6 3 2 1 5 6 4 2样例输出
plaintext
5
python
n=int(input())
arr=list(map(int,input().split()))
k=int(input())
#从大到小
arr.sort(reverse=True)
print(arr[k-1])
问题描述
给定一个升序数组 ,查找目标值 target 是否存在。存在输出它的下标,不存在输出 -1。
输入格式
第一行:n 数组长度第二行:n 个升序整数第三行:target 目标值
输出格式
输出下标,没找到输出 -1
样例输入
plaintext
6 -1 0 3 5 9 12 9样例输出
plaintext
4样例输入 2
plaintext
6 -1 0 3 5 9 12 2样例输出 2
plaintext
-1
python
n=int(input())
arr=list(map(int,input().split()))
target=int(input())
for i in range(n):
if arr[i]==target:
print(i)
exit()
print(-1)
两数相加
问题描述
给你两个非负整数 ,每一位数字按逆序 存储在链表中。请你计算两个数的和,并同样以逆序链表输出结果。
输入格式
第一行:第一个数的各位数字(逆序)第二行:第二个数的各位数字(逆序)
输出格式
输出和的逆序数组
样例输入
plaintext
2 4 3 5 6 4样例输出
plaintext
7 0 8
python
# 读入逆序数组
a = list(map(int, input().split()))
b = list(map(int, input().split()))
# 逆序变正常字符串
s1 = ''.join(map(str, a[::-1]))
s2 = ''.join(map(str, b[::-1]))
# 直接转整数相加
num1 = int(s1)
num2 = int(s2)
total = num1 + num2
# 结果再逆序输出
res = list(map(int, str(total)[::-1]))
print(' '.join(map(str, res)))
问题描述
给定一个链表(我们直接用数组 代替),请你删除倒数第 n 个元素,输出删除后的数组。
输入格式
第一行:数组长度 m第二行:m 个整数,表示链表元素第三行:整数 n,表示要删除倒数第几个
输出格式
删除后的数组,空格隔开
样例输入
plaintext
5 1 2 3 4 5 2样例输出
plaintext
1 2 3 5
python
m = int(input())
arr = list(map(int, input().split()))
n = int(input())
# 倒数第 n 个 → 下标是:长度 - n
pos = m - n
# 删除这个位置
arr.pop(pos)
# 输出
print(' '.join(map(str, arr)))
python
# 读入
m = int(input())
arr = list(map(int, input().split()))
n = int(input())
# 倒数第n个 → 正数下标:len(arr) - n
pos = len(arr) - n
# 删除这个位置
del arr[pos]
# 输出
print(' '.join(map(str, arr)))
问题描述
给定一个数组(模拟链表),把里面的元素两两交换:第 0 和 1 换,第 2 和 3 换,第 4 和 5 换...最后多出来一个就不动。
输入格式
第一行:n第二行:n 个整数
输出格式
两两交换后的数组
样例输入
plaintext
4 1 2 3 4样例输出
plaintext
2 1 4 3样例输入 2
plaintext
5 1 2 3 4 5样例输出 2
plaintext
2 1 4 3 5
python
n = int(input())
arr = list(map(int, input().split()))
# 两两交换:每次跳 2 格
for i in range(0, n - 1, 2):
arr[i], arr[i + 1] = arr[i + 1], arr[i]
print(' '.join(map(str, arr)))
问题描述
给你一个数组(模拟链表),把它向右旋转 k 次。每旋转一次,最后一个数移到最前面。
输入格式
第一行:n 数组长度第二行:n 个整数第三行:k 旋转次数
输出格式
旋转后的数组
样例输入
plaintext
5 1 2 3 4 5 2样例输出
plaintext
4 5 1 2 3
pythonn=int(input()) arr=list(map(int,input().split())) k=int(input()) for i in range(k): last=arr.pop() arr.insert(last) arr.insert(0,last) print(' '.join(map(str,arr)))把 last 这个数,插到数组的第 0 号位置(最前面)
给你一个数组(模拟链表),判断里面有没有重复出现的数字 。如果有,说明有环,输出True;没有输出False。省三简化版:不用真链表,判断数组是否有重复即可。
输入格式
第一行:n第二行:n 个整数
输出格式
True 或 False
样例输入
plaintext
4 3 2 0 -4样例输出
plaintext
False有环样例
plaintext
4 1 2 3 2输出
plaintext
True
python
n = int(input())
arr = list(map(int, input().split()))
# 暴力:看长度是不是和去重后一样
if len(set(arr)) == n:
print(False) # 没重复 = 无环
else:
print(True) # 有重复 = 有环