当前位置:网站首页>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 .
边栏推荐
- 由于dms升级为了新版,我之前的sql在老版本的dms中,这种情况下,如何找回我之前的sql呢?
- 期末周,我裂开
- 【FreeRTOS】FreeRTOS学习笔记(7)— 手写FreeRTOS双向链表/源码分析
- 2022年6月小结
- Google Chrome Portable Google Chrome browser portable version official website download method
- The important role of host reinforcement concept in medical industry
- Set JTAG fuc invalid to normal IO port
- 大厂技术专家:架构设计中常用的思维模型
- Selenium driver ie common problem solving message: currently focused window has been closed
- Tar source code analysis Part 7
猜你喜欢
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
Bottom problem of figure
Transition technology from IPv4 to IPv6
Introduction to spark core components
Mobile adaptation: vw/vh
SQL foundation 9 [grouping data]
[Mori city] random talk on GIS data (I)
uniapp小程序分包
The cloud native programming challenge ended, and Alibaba cloud launched the first white paper on application liveliness technology in the field of cloud native
校园网络问题
随机推荐
Tar source code analysis 4
[freertos] freertos Learning notes (7) - written freertos bidirectionnel Link LIST / source analysis
Mobile adaptation: vw/vh
Introduction to spark core components
Chapter 1 programming problems
com. alibaba. nacos. api. exception. NacosException
Why does the producer / consumer mode wait () use while instead of if (clear and understandable)
Uniapp applet subcontracting
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
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
jdbc连接es查询的时候,有遇到下面这种情况的大神嘛?
js 常用时间处理函数
tornado项目之路由装饰器
Google Chrome Portable Google Chrome browser portable version official website download method
Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
ABCD four sequential execution methods, extended application
The crackdown on Huawei prompted made in China to join forces to fight back, and another enterprise announced to invest 100 billion in R & D
电子协会 C语言 1级 34 、分段函数
How to buy financial products in 2022?
Tar source code analysis Part 7