当前位置:网站首页>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 .
边栏推荐
- Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
- jdbc连接es查询的时候,有遇到下面这种情况的大神嘛?
- Summary of June 2022
- Pangu open source: multi support and promotion, the wave of chip industry
- What is the use of cloud redis? How to use cloud redis?
- js 常用时间处理函数
- [Android reverse] function interception (use cache_flush system function to refresh CPU cache | refresh CPU cache disadvantages | recommended time for function interception)
- Since DMS is upgraded to a new version, my previous SQL is in the old version of DMS. In this case, how can I retrieve my previous SQL?
- Novel website program source code that can be automatically collected
- Boast about Devops
猜你喜欢

tornado项目之路由装饰器
![[GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph](/img/5e/7ce21dd544aacf23b4ceef1ec547fd.png)
[GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph

Redis - detailed explanation of cache avalanche, cache penetration and cache breakdown

Node connection MySQL access denied for user 'root' @ 'localhost' (using password: yes
![[MySQL transaction]](/img/4f/dbfa1bf999cfcbbe8f3b27bb1e932b.jpg)
[MySQL transaction]

uniapp小程序分包

Status of the thread

How to share the source code anti disclosure scheme

MySQL storage engine

The cloud native programming challenge ended, and Alibaba cloud launched the first white paper on application liveliness technology in the field of cloud native
随机推荐
How to input single quotation marks and double quotation marks in latex?
Centos8 install mysql 7 unable to start up
jdbc连接es查询的时候,有遇到下面这种情况的大神嘛?
Chain ide -- the infrastructure of the metauniverse
CMS source code of multi wechat management system developed based on thinkphp6, with one click curd and other functions
电子协会 C语言 1级 35 、银行利息
Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
Electronic Association C language level 1 34, piecewise function
[Mori city] random talk on GIS data (I)
There is no Chinese prompt below when inputting text in win10 Microsoft Pinyin input method
电脑通过Putty远程连接树莓派
响应式——媒体查询
tornado之目录
Transition technology from IPv4 to IPv6
The most effective futures trend strategy: futures reverse merchandising
How can the old version of commonly used SQL be migrated to the new version?
the input device is not a TTY. If you are using mintty, try prefixing the command with ‘winpty‘
MySQL storage engine
Selenium driver ie common problem solving message: currently focused window has been closed
Boast about Devops