当前位置:网站首页>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 .
边栏推荐
- Vulhub vulnerability recurrence 76_ XXL-JOB
- Su Weijie, a member of Qingyuan Association and an assistant professor at the University of Pennsylvania, won the first Siam Youth Award for data science, focusing on privacy data protection, etc
- notepad++如何统计单词数量
- 同一个job有两个source就报其中一个数据库找不到,有大佬回答下吗
- 【FreeRTOS】FreeRTOS学习笔记(7)— 手写FreeRTOS双向链表/源码分析
- Splicing plain text into JSON strings - easy language method
- BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment
- [FreeRTOS] FreeRTOS learning notes (7) - handwritten FreeRTOS two-way linked list / source code analysis
- 大厂技术专家:架构设计中常用的思维模型
- The difference between synchronized and lock
猜你喜欢
![[Valentine's day] - you can change your love and write down your lover's name](/img/ab/402872ad39f9dc58fd27dd6fc823ef.jpg)
[Valentine's day] - you can change your love and write down your lover's name

The cloud native programming challenge ended, and Alibaba cloud launched the first white paper on application liveliness technology in the field of cloud native

Bottom problem of figure

A real penetration test

Four sets of APIs for queues

NLP-文献阅读总结

【网络数据传输】基于FPGA的百兆网/兆网千UDP数据包收发系统开发,PC到FPGA

Deep profile data leakage prevention scheme

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

what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
随机推荐
win10微软拼音输入法输入文字时候下方不出现中文提示
If there are two sources in the same job, it will be reported that one of the databases cannot be found. Is there a boss to answer
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
MySQL 45 lecture learning notes (x) force index
云Redis 有什么用? 云redis怎么用?
校园网络问题
请问旧版的的常用SQL怎么迁移到新版本里来?
Introduction to rce in attack and defense world
JS common time processing functions
Zabbix agent主动模式的实现
Selection (022) - what is the output of the following code?
What is industrial computer encryption and how to do it
what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
高薪程序员&面试题精讲系列119之Redis如何实现分布式锁?
MySQL relearn 2- Alibaba cloud server CentOS installation mysql8.0
Introduction to spark core components
Deep understanding of redis -- a new type of bitmap / hyperloglgo / Geo
Cochez une colonne d'affichage dans une colonne de tableau connue
MySQL 45 lecture learning notes (XIV) count (*)
Electronic Association C language level 1 35, bank interest