当前位置:网站首页>2022-021rts: from the second half of the year
2022-021rts: from the second half of the year
2022-07-04 07:13:00 【FlashSu】
ARTS:Algorithm、Review、Tip、Share
- Algorithm Algorithm problem
- Review English articles
- Tip Think back to a small skill learned at work this week
- Share Think about a technical point of view 、 Social hot spots 、 A product or a puzzle
Algorithm
25. K A set of flip list
Give you the head node of the list head , Every time k Group of nodes to flip , Please return to the modified linked list .
k Is a positive integer , Its value is less than or equal to the length of the linked list . If the total number of nodes is not k Integer multiple , Please keep the last remaining nodes in the original order .
You can't just change the values inside the node , It's about actually switching nodes .
Example 1:
Input :head = [1,2,3,4,5], k = 2
Output :[2,1,4,3,5]
Example 2:
Input :head = [1,2,3,4,5], k = 3
Output :[3,2,1,4,5]
Tips :
The number of nodes in the linked list is n
1 <= k <= n <= 5000
0 <= Node.val <= 1000
Advanced : You can design one that uses only O(1) Does the algorithm of extra memory space solve this problem ?
source : Power button (LeetCode)
link :https://leetcode.cn/problems/reverse-nodes-in-k-group
Solution description
although LeetCode Mark the difficulty level on the question , But it's not very difficult after reading the solution . It mainly groups the linked list of input parameters , Then reverse each group , The process involves the situation that the header element may change , The trick is to regenerate a virtual node in front of the head node , In this way, even if it is reversed , You can also use the next The pointer gets the final header .
Specific implementation of reverse linked list , It is very common in interviews , I remember I was 2018 The first algorithm question asked during the interview in was a simple reverse linked list question , It also corresponds to LeetCode Of 206. Reverse a linked list At that time, I didn't pay attention to this aspect of learning 、 No practice , After thinking for a long time, I didn't write it out , In fact, in the algorithm topic , There is no sophisticated algorithm for linked list related topics , The main thing is to investigate coding The ability to , Just pay more attention to practice at ordinary times .
coded
public ListNode reverseKGroup(ListNode head, int k) {
ListNode originalHeadPre = new ListNode(-1);
originalHeadPre.next = head;
ListNode pre = originalHeadPre;
while (head != null) {
ListNode tail = pre;
// In turn k The first and last elements of elements
for (int i = 0; i < k; i++) {
tail = tail.next;
if (tail == null) {
return originalHeadPre.next;
}
}
ListNode nextHead = tail.next;
// Reverse 、 Connect back to the source linked list
ListNode[] htGroup = this.myReverse(head, tail);
pre.next = htGroup[0];
htGroup[1].next = nextHead;
pre = htGroup[1];
head = nextHead;
}
return originalHeadPre.next;
}
ListNode[] myReverse(ListNode head, ListNode tail) {
ListNode newNext = tail.next;
ListNode cur = head;
while (newNext != tail) {
ListNode curNext = cur.next;
cur.next = newNext;
newNext = cur;
cur = curNext;
}
return new ListNode[]{
tail, head};
}
Complexity analysis
Time complexity O(N)
Spatial complexity O(1)
Review
How does one begin to tackle understanding a large open source code base?
Tip
Often asked in an interview Spring Managed bean What is the specific life cycle of ? We may find it in many places , from Spring Root interface BeanFactory Of notes Can get the official answer :
Bean factory implementations should support the standard bean
lifecycle interfaces as far as possible. The full set of
initialization methods and their standard order is:
- BeanNameAware’s setBeanName
- BeanClassLoaderAware’s setBeanClassLoader
- BeanFactoryAware’s setBeanFactory
- EnvironmentAware’s setEnvironment
- EmbeddedValueResolverAware’s setEmbeddedValueResolver
- ResourceLoaderAware’s setResourceLoader (only applicable when running in an application context)
- ApplicationEventPublisherAware’s setApplicationEventPublisher (only applicable when running in an application context)
- MessageSourceAware’s setMessageSource (only applicable when running in an application context)
- ApplicationContextAware’s setApplicationContext (only applicable when running in an application context)
- ServletContextAware’s setServletContext (only applicable when running in a web application context)
- postProcessBeforeInitialization methods of BeanPostProcessors
- InitializingBean’s afterPropertiesSet
- a custom init-method definition
- postProcessAfterInitialization methods of BeanPostProcessors
On shutdown of a bean factory, the following lifecycle methods apply:
- postProcessBeforeDestruction methods of DestructionAwareBeanPostProcessors
- DisposableBean’s destroy
- a custom destroy-method definition
Share
Last weekend, I finally launched a recently busy project , Last week's summary was also negative , Complained about his negative emotions , This week is also a rest , Take this opportunity to reflect on your shortcomings , In the past, I failed to do many things well , Often distracted in the process 、 Unable to focus , There are many things at hand , There is no primary or secondary , Plus being interrupted intentionally or unintentionally 、 Active interruption , What's more, the accumulation is poor , Although it is also intended to change , But it will return to its original state after two days . Recently read 《 The courage to be hated 》, The book says that it's useless to dwell on the past , The important thing is in the present , What is our purpose , Whether you want to change , I will think more later , What to do , What not to do , Which priorities are higher , And make yourself more focused 、 Concentrate on .
Not only has it arrived unconsciously 22 Second half of , Expect yourself to become better and better .
边栏推荐
- ABCD four sequential execution methods, extended application
- BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment
- Design of test cases
- Status of the thread
- Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
- Summary of MySQL common judgment functions!! Have you used it
- com. alibaba. nacos. api. exception. NacosException
- "Sword finger offer" 2nd Edition - force button brush question
- BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment
- 在已经知道表格列勾选一个显示一列
猜你喜欢

Splicing plain text into JSON strings - easy language method

selenium驱动IE常见问题解决Message: Currently focused window has been closed.

"Sword finger offer" 2nd Edition - force button brush question

Solution of running crash caused by node error

Vulhub vulnerability recurrence 76_ XXL-JOB

The number of patent applications in China has again surpassed that of the United States and Japan, ranking first in the world for 11 consecutive years

How to share the source code anti disclosure scheme

云Redis 有什么用? 云redis怎么用?

Cell reports: Wei Fuwen group of the Institute of zoology, Chinese Academy of Sciences analyzes the function of seasonal changes in the intestinal flora of giant pandas

响应式移动Web测试题
随机推荐
Label management of kubernetes cluster
Chain ide -- the infrastructure of the metauniverse
How can the old version of commonly used SQL be migrated to the new version?
flask-sqlalchemy 循环引用
Splicing plain text into JSON strings - easy language method
the input device is not a TTY. If you are using mintty, try prefixing the command with ‘winpty‘
在已經知道錶格列勾選一個顯示一列
关于IDEA如何设置快捷键集
输入年份、月份,确定天数
Summary of June 2022
图的底部问题
Why does the producer / consumer mode wait () use while instead of if (clear and understandable)
电子协会 C语言 1级 34 、分段函数
MySQL 45 lecture learning notes (VI) global lock
tornado之目录
大厂技术专家:架构设计中常用的思维模型
Four sets of APIs for queues
Tar source code analysis 6
响应式移动Web测试题
【GF(q)+LDPC】基于二值图GF(q)域的规则LDPC编译码设计与matlab仿真