本报讯 柬埔寨王宫事务部9月24日发布通告称,最受柬埔寨民众尊敬、爱戴的诺罗敦·莫尼列·西哈努克王太后陛下已年过花甲,目前正在北京的医院接受悉心治疗。...
2025-09-25 8
青少年编程竞赛交流群」已成立(适合6至18周岁的青少年),公众号后台回复【Scratch】或【Python】,即可进入,如果加入了之前的社群不需要重复加入。进群之后大家可以参与每周日晚20:00的升级打怪活动以及每个月的青少年编程组队学习活动。
青少年编程竞赛交流群」已成立(适合6至18周岁的青少年),公众号后台回复【Scratch】或【Python】,即可进入,如果加入了之前的社群不需要重复加入。进群之后大家可以参与每周日晚20:00的升级打怪活动以及每个月的青少年编程组队学习活动。
本期训练营根据孩子们对知识的掌握情况分成两个打卡群(Leetcode简单打卡群、Leetcode中等打卡群), 每个打卡群针对不同需求的孩子,这就使得本次活动更具有针对性,对孩子学习编程帮助更大。
活动的时间 从9月1日至9月21日,每天一道编程题。
参与的小朋友可以 ,让他邀请你到本次相应的打卡群并把本次的打卡题目发给你。
微信号:ProgrammingAssistant
Day19: 160_相交链表
难度:简单
/
难度:简单
/
难度:简单
/
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。
图示两个链表在节点 c1 开始相交:
展开全文
题目数据 保证整个链式结构中不存在环。
注意,函数返回结果后,链表必须 保持其原始结构。
自定义评测:
评测系统的输入如下(你设计的程序 不适用此输入):
intersectVal - 相交的起始节点的值。如果不存在相交节点,这一值为 0
listA - 第一个链表
listB - 第二个链表
skipA - 在 listA 中(从头节点开始)跳到交叉节点的节点数
skipB - 在 listB 中 (从头节点开始)跳到交叉节点的节点数
intersectVal - 相交的起始节点的值。如果不存在相交节点,这一值为 0
listA - 第一个链表
listB - 第二个链表
skipA - 在 listA 中(从头节点开始)跳到交叉节点的节点数
skipB - 在 listB 中 (从头节点开始)跳到交叉节点的节点数
评测系统将根据这些输入创建链式数据结构,并将两个头节点 headA 和 headB 传递给你的程序。如果程序能够正确返回相交节点,那么你的解决方案将被 视作正确答案。
示例 1:
输入:intersectVal = 8, listA = [ 4, 1, 8, 4, 5], listB = [ 5, 0, 1, 8, 4, 5], skipA = 2, skipB = 3
输出:Intersected at '8'
解释:相交节点的值为 8(注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [ 4, 1, 8, 4, 5],链表 B 为 [ 5, 0, 1, 8, 4, 5]。
在 A 中,相交节点前有 2个节点;在 B 中,相交节点前有 3个节点。
示例 2:
输入:intersectVal = 2, listA = [ 0, 9, 1, 2, 4], listB = [ 3, 2, 4], skipA = 3, skipB = 1
输出:Intersected at '2'
解释:相交节点的值为 2(注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [ 0, 9, 1, 2, 4],链表 B 为 [ 3, 2, 4]。
在 A 中,相交节点前有 3个节点;在 B 中,相交节点前有 1个节点。
示例 3:
输入:intersectVal = 0, listA = [ 2, 6, 4], listB = [ 1, 5], skipA = 3, skipB = 2
输出:null
解释:从各自的表头开始算起,链表 A 为 [ 2, 6, 4],链表 B 为 [ 1, 5]。
由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
这两个链表不相交,因此返回 null 。
注意:
listA 中节点数目为 m
listB 中节点数目为 n
0 <= skipA <= m
0 <= skipB <= n
如果 listA 和 listB 没有交点, intersectVal 为 0
如果 listA 和 listB 有交点, intersectVal == listA[skipA] == listB[skipB]
listA 中节点数目为 m
listB 中节点数目为 n
0 <= skipA <= m
0 <= skipB <= n
如果 listA 和 listB 没有交点, intersectVal 为 0
如果 listA 和 listB 有交点, intersectVal == listA[skipA] == listB[skipB]
进阶:你能否设计一个时间复杂度 O(m + n) 、仅用 O(1) 内存的解决方案?
实现
思路:通过集合的方法。
C# 语言
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
publicclassSolution
publicListNode GetIntersectionNode(ListNode headA, ListNode headB)
HashSet<ListNode> hash = newHashSet<ListNode>;
ListNode temp = headA;
while(temp != null)
hash.Add(temp);
temp = temp.next;
temp = headB;
while(temp != null)
if(hash.Contains(temp))
returntemp;
temp = temp.next;
returnnull;
Python 语言
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
classSolution(object):
defgetIntersectionNode(self, headA, headB):
:type head1, head1: ListNode
:rtype: ListNode
h = set
temp = headA
whiletemp isnotNone:
h.add(temp)
temp = temp.next
temp = headB
whiletemp isnotNone:
iftemp inh:
returntemp
temp = temp.next
returnNone
Day19:59_螺旋矩阵II
难度:中等
/
难度:中等
/
难度:中等
/
示例1:
输入:n = 3
输出:[[ 1, 2, 3],[ 8, 9, 4],[ 7, 6, 5]]
示例 2:
输入:n = 1
输出:[[ 1]]
提示:
1 <= n <= 20
1 <= n <= 20
C# 语言
publicclassSolution
publicint[][] GenerateMatrix( intn)
int[][] matrix = newint[n][];
for( inti = 0; i < n; i++)
matrix[i] = newint[n];
intstart = 0; //起始位置
intend1 = n - 1; //最左边位置
intend2 = n - 1; //最下边位置
intcount = 1;
while(start < end1 && start < end2)
LeftToRight(start, end1, start, matrix, ref count);
TopToBottom(start + 1, end2, end1, matrix, ref count);
RightToLeft(end1 - 1, start, end2, matrix, ref count);
BottomToTop(end2 - 1, start + 1, start, matrix, ref count);
start++;
end1 = n - 1- start;
end2 = n - 1- start;
if(n% 2== 1)
matrix[start][start] = count;
returnmatrix;
privatevoidLeftToRight( intstart, intend, introwIndex, int[][] matrix, ref intfrom)
for( inti = start; i <= end; i++)
matrix[rowIndex][i] = from;
from++;
privatevoidTopToBottom( intstart, intend, intcolIndex, int[][] matrix, ref intfrom)
for( inti = start; i <= end; i++)
matrix[i][colIndex] = from;
from++;
privatevoidRightToLeft( intstart, intend, introwIndex, int[][] matrix, ref intfrom)
for( inti = start; i >= end; i--)
matrix[rowIndex][i] = from;
from++;
privatevoidBottomToTop( intstart, intend, intcolIndex, int[][] matrix, ref intfrom)
for( inti = start; i >= end; i--)
matrix[i][colIndex] = from;
from++;
Python 语言
classSolution(object):
defgenerateMatrix(self, n):
:type n: int
:rtype: List[List[int]]
self.count = 1
self.matrix = [[ 0fori inrange(n)] fori inrange(n)]
start, end1, end2 = 0, n - 1, n - 1
whilestart < end1 andstart < end2:
self.LeftToRight(start, end1, start)
self.TopToBottom(start + 1, end2, end1)
self.RightToLeft(end1 - 1, start, end2)
self.BottomToTop(end2 - 1, start + 1, start)
start += 1
end1 = n - 1- start
end2 = n - 1- start
ifn % 2== 1:
self.matrix[start][start] = self.count
returnself.matrix
defLeftToRight(self, start, end, rowIndex):
fori inrange(start, end + 1):
self.matrix[rowIndex][i] = self.count
self.count += 1
defTopToBottom(self, start, end, colIndex):
fori inrange(start, end + 1):
self.matrix[i][colIndex] = self.count
self.count += 1
defRightToLeft(self, start, end, rowIndex):
fori inrange(start, end - 1, -1):
self.matrix[rowIndex][i] = self.count
self.count += 1
defBottomToTop(self, start, end, colIndex):
fori inrange(start, end - 1, -1):
self.matrix[i][colIndex] = self.count
self.count += 1
青少年编程升级打怪计划
把电子学会的青少年编程能力等级测评作为游戏的关卡,带着小朋友们升级打怪。
每周日晚20:00,利用腾讯会议进行直播分享,之后安排一个测试(与等级测评的题目数量一致)考察小朋友们对知识的掌握情况。
为了,让各个阶段的小朋友都能参与到学习中,我们每个月都会组织Scratch、Python、C++的青少年编程学习活动 ,为小朋友们三助力,即学习编程助力、实践知识助力、 结识伙伴助力。
一键三连,一起学习⬇️
相关文章
本报讯 柬埔寨王宫事务部9月24日发布通告称,最受柬埔寨民众尊敬、爱戴的诺罗敦·莫尼列·西哈努克王太后陛下已年过花甲,目前正在北京的医院接受悉心治疗。...
2025-09-25 8
自从特鲁多政府在美国的怂恿下,对中国产电动汽车、钢铁和铝产品加征高额关税以来,加拿大与中国的关系就如同滑下了拼命向下的轨道。中国这一举动无疑是给加拿大...
2025-09-25 7
据报道,近年来的中美博弈已不仅仅体现在贸易战、科技封锁和军事对抗等领域,金融和资本市场的较量同样成为了全球关注的焦点。中国连续大规模减持美国国债,特别...
2025-09-25 7
近日,四川成都一自行车车迷赛中发生选手集体摔车事故,一名年轻男选手疑似膝盖伤情严重,被传“膝盖骨没了”,引发众多网友热议。日前,成都市体育局回应新黄河...
2025-09-25 5
【环球网快讯】据《以色列时报》25日最新消息,以色列军方宣布,一名以色列国防军士兵在加沙城遭巴勒斯坦伊斯兰抵抗运动(哈马斯)狙击手袭击身亡。 报道称,...
2025-09-25 7
花费不菲入住五星级酒店 半夜却无奈躲在阳台 究竟发生了什么? 近日 30岁的孙先生反映 夫妻俩入住上海星河湾酒店后 自己眼角膜受伤,视物不清 妻子肺部...
2025-09-25 5
每经编辑|何小桃 据新黄河9月24日报道,记者近日获悉,浙江省绍兴市轨道交通2号线9月13日晚曾发生一起地铁列车撞上保洁员致多人伤亡的安全事故。 多名...
2025-09-25 5
资料图:上海绿捷 本文综合上观新闻、正观新闻、采招网、21世纪经济报道等 昨天(9月23日)深夜,针对公众关注的“臭味学生餐”事件,上海官方发布通报称...
2025-09-25 6
发表评论