581. Shortest Unsorted Continuous Subarray

題目解析

Given an integer array, you need to find one?continuous subarray?that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.

You need to find the?shortest?such subarray and output its length.

找到最短的連續(xù)子數(shù)組,滿足子數(shù)組有序后整個(gè)數(shù)組有序。


算法思路

1)i、j,i用于找子數(shù)組起始位置,j遍歷整個(gè)數(shù)組并且找子數(shù)組結(jié)束位置;

2)minSub、maxSub為子數(shù)組中的最大最小值,每當(dāng)i、j移動(dòng)都需要判斷兩個(gè)變量并賦值;

3)結(jié)果子數(shù)組需要滿足的條件,子數(shù)組中最小值minSum>=nums[i-1],子數(shù)組中的最大值maxSum<=nums[j+1];

4)在該循環(huán)里j不斷向后遍歷,但每當(dāng)minSub的值改變需要重新判斷i的位置;

5)跳出循環(huán)的條件,j遍歷到數(shù)組結(jié)尾,同時(shí)滿足minSum>=nums[i-1]。

ps:這個(gè)循環(huán)條件調(diào)試了好久啊??!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容