diff --git a/notes/剑指 offer 题解.md b/notes/剑指 offer 题解.md index 156b86a5..6af4e970 100644 --- a/notes/剑指 offer 题解.md +++ b/notes/剑指 offer 题解.md @@ -302,6 +302,10 @@ public TreeLinkNode GetNext(TreeLinkNode pNode) { ## 9. ÓÃÁ½¸öջʵÏÖ¶ÓÁÐ +**½âÌâ˼·** + +Ìí¼Óµ½Õ»ÖеÄÐòÁÐ˳Ðò»á±»·´×ª£¬Èç¹û½øÐÐÁ½´Î·´×ª£¬ÄÇôµÃµ½µÄÐòÁÐÒÀÈ»ÊÇÕýÏòµÄ¡£Òò´Ë£¬Ìí¼ÓµÄÊý¾ÝÐèҪͬʱѹÈëÁ½¸öÕ»Ö®ºó²ÅÄܳöÕ»£¬ÕâÑù¾ÍÄܱ£Ö¤³öÕ»µÄ˳ÐòΪÏȽøÏȳö¡£ + ```java Stack stack1 = new Stack(); Stack stack2 = new Stack(); @@ -381,12 +385,14 @@ public int RectCover(int target) { } ``` - ## 11. ÐýתÊý×éµÄ×îСÊý×Ö **ÌâÄ¿ÃèÊö** -°ÑÒ»¸öÊý×é×ʼµÄÈô¸É¸öÔªËØ°áµ½Êý×éµÄĩ⣬ÎÒÃdzÆ֮ΪÊý×éµÄÐýת¡£ ÊäÈëÒ»¸ö·ÇµÝ¼õÅÅÐòµÄÊý×éµÄÒ»¸öÐýת£¬Êä³öÐýתÊý×éµÄ×îСԪËØ¡£ ÀýÈçÊý×é {3, 4, 5, 1, 2} Ϊ {1, 2, 3, 4, 5} µÄÒ»¸öÐýת£¬¸ÃÊý×éµÄ×îСֵΪ 1¡£ NOTE£º¸ø³öµÄËùÓÐÔªËض¼´óÓÚ 0£¬ÈôÊý×é´óСΪ 0£¬Çë·µ»Ø 0¡£ +°ÑÒ»¸öÊý×é×ʼµÄÈô¸É¸öÔªËØ°áµ½Êý×éµÄĩ⣬ÎÒÃdzÆ֮ΪÊý×éµÄÐýת¡£ÊäÈëÒ»¸ö·ÇµÝ¼õÅÅÐòµÄÊý×éµÄÒ»¸öÐýת£¬Êä³öÐýתÊý×éµÄ×îСԪËØ¡£ÀýÈçÊý×é {3, 4, 5, 1, 2} Ϊ {1, 2, 3, 4, 5} µÄÒ»¸öÐýת£¬¸ÃÊý×éµÄ×îСֵΪ 1¡£NOTE£º¸ø³öµÄËùÓÐÔªËض¼´óÓÚ 0£¬ÈôÊý×é´óСΪ 0£¬Çë·µ»Ø 0¡£ + + +O(N) ʱ¼ä¸´ÔӶȽⷨ£º ```java public int minNumberInRotateArray(int[] array) { @@ -398,6 +404,23 @@ public int minNumberInRotateArray(int[] array) { } ``` +O(lgN) ʱ¼ä¸´ÔӶȽⷨ£º + +```java +public int minNumberInRotateArray(int[] array) { + if (array.length == 0) return 0; + int l = 0, r = array.length - 1; + int mid = -1; + while (array[l] >= array[r]) { + if (r - l == 1) return array[r]; + mid = l + (r - l) / 2; + if (array[mid] >= array[l]) l = mid; + else if (array[mid] <= array[r]) r = mid; + } + return array[mid]; +} +``` + ## 12. ¾ØÕóÖеÄ·¾¶ **ÌâÄ¿ÃèÊö**