当前位置:网站首页>2022-021ARTS:下半年開始
2022-021ARTS:下半年開始
2022-07-04 07:12:00 【FlashSu】
ARTS:Algorithm、Review、Tip、Share
- Algorithm 算法題
- Review 英文文章
- Tip 回想一下本周工作中學到的一個小技巧
- Share思考一個技術觀點、社會熱點、一個產品或是一個困惑
Algorithm
25. K 個一組翻轉鏈錶
給你鏈錶的頭節點 head ,每 k 個節點一組進行翻轉,請你返回修改後的鏈錶。
k 是一個正整數,它的值小於或等於鏈錶的長度。如果節點總數不是 k 的整數倍,那麼請將最後剩餘的節點保持原有順序。
你不能只是單純的改變節點內部的值,而是需要實際進行節點交換。
示例 1:
輸入:head = [1,2,3,4,5], k = 2
輸出:[2,1,4,3,5]
示例 2:
輸入:head = [1,2,3,4,5], k = 3
輸出:[3,2,1,4,5]
提示:
鏈錶中的節點數目為 n
1 <= k <= n <= 5000
0 <= Node.val <= 1000
進階:你可以設計一個只用 O(1) 額外內存空間的算法解决此問題嗎?
來源:力扣(LeetCode)
鏈接:https://leetcode.cn/problems/reverse-nodes-in-k-group
解法描述
雖然LeetCode上標識這道題為困難的級別,但看完題解感覺並不是很難了。主要是將入參鏈錶進行分組,然後對每一組進行反轉,過程中涉及到頭元素可能會變更的情况,技巧是在頭結點前面再生成一個虛擬節點,這樣即使進行了反轉,也可以通過虛擬節點的next指針拿到最終的頭結點。
具體反轉鏈錶的實現,面試中可謂很常見了,我記得我在2018年面試時問到的第一道算法題就是單純的一道反轉鏈錶題目,也對應了LeetCode的 206. 反轉鏈錶 當時因為沒有重視這方面的學習、也沒有練習,想了半天也沒有寫出來,其實在算法題目中,鏈錶相關的題目沒有什麼高深的算法,主要就是考察coding的能力了,平時多注意練習一些就可以了。
編碼實現
public ListNode reverseKGroup(ListNode head, int k) {
ListNode originalHeadPre = new ListNode(-1);
originalHeadPre.next = head;
ListNode pre = originalHeadPre;
while (head != null) {
ListNode tail = pre;
// 依次得到k個元素的首尾元素
for (int i = 0; i < k; i++) {
tail = tail.next;
if (tail == null) {
return originalHeadPre.next;
}
}
ListNode nextHead = tail.next;
// 倒轉、接回源鏈錶
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};
}
複雜度分析
時間複雜度O(N)
空間複雜度O(1)
Review
How does one begin to tackle understanding a large open source code base?
Tip
面試中經常被問到Spring管理的bean的生命周期具體是有哪些?我們可能從很多地方都能查到,從Spring根接口BeanFactory的注釋中能够得到官方的答案:
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
上周末終於把最近忙碌的一個項目上線了,上周的總結也比較負能量,抱怨了一通自己的負面情緒,這一周也算是一個休整吧,趁此機會也好好反思下自己的不足,過去的很多事自己都沒能做好,做的過程中也經常分心、無法專注,手頭的事情一多,也沒有個主次,再加上有意無意的被打斷、主動打斷的情况,更是積累的可憐,雖然也有意改變,但堅持不了兩天就又恢複原狀。最近在讀《被討厭的勇氣》,書上說糾結於過去的原因是沒有任何用的,重要的是在當下,我們的目的是什麼,究竟是否想要改變,自己後面也會更多的去思考,要做哪些事情,不做哪些事,哪些優先級更高,以及讓自己更加專注、專心。
不只不覺中已經來到了22年的下半年,期待自己變得越來越好。
边栏推荐
- Chain ide -- the infrastructure of the metauniverse
- MySQL 45 lecture learning notes (XIII) delete half of the table data, and the table file size remains the same
- 测试用例的设计
- Selection (022) - what is the output of the following code?
- Boosting the Performance of Video Compression Artifact Reduction with Reference Frame Proposals and
- 【FreeRTOS】FreeRTOS學習筆記(7)— 手寫FreeRTOS雙向鏈錶/源碼分析
- Deep understanding of redis -- a new type of bitmap / hyperloglgo / Geo
- Industrial computer anti-virus
- Paddleocr prompt error: can not import AVX core while this file exists: xxx\paddle\fluid\core_ avx
- Redis interview question set
猜你喜欢

Review of enterprise security incidents: how can enterprises do a good job in preventing source code leakage?

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

Splicing plain text into JSON strings - easy language method

Chain ide -- the infrastructure of the metauniverse

BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment

Campus network problems

How to share the source code anti disclosure scheme
![[Android reverse] function interception (use cache_flush system function to refresh CPU cache | refresh CPU cache disadvantages | recommended time for function interception)](/img/5c/afb0d43665a8b46579dc604d983790.jpg)
[Android reverse] function interception (use cache_flush system function to refresh CPU cache | refresh CPU cache disadvantages | recommended time for function interception)

Pangu open source: multi support and promotion, the wave of chip industry

A real penetration test
随机推荐
flask-sqlalchemy 循环引用
The cloud native programming challenge ended, and Alibaba cloud launched the first white paper on application liveliness technology in the field of cloud native
There is no Chinese prompt below when inputting text in win10 Microsoft Pinyin input method
2022年6月小结
【森城市】GIS数据漫谈(一)
测试用例的设计
MySQL relearn 2- Alibaba cloud server CentOS installation mysql8.0
Review of enterprise security incidents: how can enterprises do a good job in preventing source code leakage?
[kubernetes series] kubesphere is installed on kubernetes
Selection (023) - what are the three stages of event propagation?
请问旧版的的常用SQL怎么迁移到新版本里来?
notepad++如何统计单词数量
CMS source code of multi wechat management system developed based on thinkphp6, with one click curd and other functions
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?
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
MySQL 45 lecture learning notes (XIII) delete half of the table data, and the table file size remains the same
Flink memory model, network buffer, memory tuning, troubleshooting
Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
SQL foundation 9 [grouping data]
MySQL 45 lecture learning notes (12) MySQL will "shake" for a while