From 43fdefc0e896291cb2a8e51ad0c03203c17ec45f Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Thu, 22 Feb 2018 15:47:33 +0800 Subject: [PATCH] auto commit --- notes/Leetcode 题解.md | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/notes/Leetcode 题解.md b/notes/Leetcode 题解.md index 0727747e..2750f28f 100644 --- a/notes/Leetcode 题解.md +++ b/notes/Leetcode 题解.md @@ -56,10 +56,10 @@ ¶þ·Ö²éÕÒ˼Ïë¼òµ¥£¬µ«ÊÇÔÚʵÏÖʱÓÐһЩÐèҪעÒâµÄϸ½Ú£º -1. ÔÚ¼ÆËã mid ʱ²»ÄÜʹÓà mid = (l + h) / 2 ÕâÖÖ·½Ê½£¬ÒòΪ l + h ¿ÉÄܻᵼÖ¼ӷ¨Òç³ö£¬Ó¦¸ÃʹÓà mid = l + (h - l) / 2 ¡£ +1. ÔÚ¼ÆËã mid ʱ²»ÄÜʹÓà mid = (l + h) / 2 ÕâÖÖ·½Ê½£¬ÒòΪ l + h ¿ÉÄܻᵼÖ¼ӷ¨Òç³ö£¬Ó¦¸ÃʹÓà mid = l + (h - l) / 2¡£ 2. ¶Ô h µÄ¸³ÖµºÍÑ­»·Ìõ¼þÓйأ¬µ±Ñ­»·Ìõ¼þΪ l <= h ʱ£¬h = mid - 1£»µ±Ñ­»·Ìõ¼þΪ l < h ʱ£¬h = mid¡£ -½âÊÍÈçÏ£ºÔÚÑ­»·Ìõ¼þΪ l <= h ʱ£¬Èç¹û h = mid£¬»á³öÏÖÑ­»·ÎÞ·¨Í˳öµÄÇé¿ö£¬ÀýÈç l = 1£¬h = 1£¬´Ëʱ mid Ò²µÈÓÚ 1£¬Èç¹û´Ëʱ¼ÌÐøÖ´ÐÐ h = mid £¬ÄÇô¾Í»áÎÞÏÞÑ­»·£»ÔÚÑ­»·Ìõ¼þΪ l < h £¬Èç¹û h = mid - 1£¬»á´íÎóÌø¹ý²éÕÒµÄÊý£¬ÀýÈç¶ÔÓÚÊý×é 1,2,3 £¬Òª²éÕÒ 1 £¬×ʼ l = 0£¬h = 2£¬mid = 1£¬ÅÐ¶Ï key < arr[mid] Ö´ÐÐ h = mid - 1 = 0£¬´Ëʱѭ»·Í˳ö£¬Ö±½Ó°Ñ²éÕÒµÄÊýÌø¹ýÁË¡£ +½âÊÍÈçÏ£ºÔÚÑ­»·Ìõ¼þΪ l <= h ʱ£¬Èç¹û h = mid£¬»á³öÏÖÑ­»·ÎÞ·¨Í˳öµÄÇé¿ö£¬ÀýÈç l = 1£¬h = 1£¬´Ëʱ mid Ò²µÈÓÚ 1£¬Èç¹û´Ëʱ¼ÌÐøÖ´ÐÐ h = mid£¬ÄÇô¾Í»áÎÞÏÞÑ­»·£»ÔÚÑ­»·Ìõ¼þΪ l < h£¬Èç¹û h = mid - 1£¬»á´íÎóÌø¹ý²éÕÒµÄÊý£¬ÀýÈç¶ÔÓÚÊý×é [1,2,3]£¬Òª²éÕÒ 1£¬×ʼ l = 0£¬h = 2£¬mid = 1£¬ÅÐ¶Ï key < arr[mid] Ö´ÐÐ h = mid - 1 = 0£¬´Ëʱѭ»·Í˳ö£¬Ö±½Ó°Ñ²éÕÒµÄÊýÌø¹ýÁË¡£ 3. l µÄ¸³ÖµÒ»°ã¶¼Îª l = mid + 1¡£ @@ -113,7 +113,7 @@ The coins can form the following rows: Because the 4th row is incomplete, we return 3. ``` -ÌâÄ¿ÃèÊö£ºµÚ i ÐÐ°Ú i ¸ö£¬Í³¼ÆÄܹ»ÅÅÁеÄÐÐÊý¡£ +ÌâÄ¿ÃèÊö£ºµÚ i ÐÐ°Ú i ¸ö£¬Í³¼ÆÄܹ»°ÚµÄÐÐÊý¡£ ·µ»Ø h ¶ø²»ÊÇ l£¬ÒòΪ°ÚµÄÓ²±Ò×îºóÒ»Ðв»ÄÜËã½øÈ¥¡£ @@ -155,7 +155,7 @@ public int singleNonDuplicate(int[] nums) { int l = 0, h = nums.length - 1; while(l < h) { int m = l + (h - l) / 2; - if(m % 2 == 1) m--; // ±£Ö¤ l/h/m ¶¼ÔÚżÊýλ£¬Ê¹µÃ²éÕÒÇø¼ä´óСһֱ¶¼ÊÇ ÆæÊý + if(m % 2 == 1) m--; // ±£Ö¤ l/h/m ¶¼ÔÚżÊýλ£¬Ê¹µÃ²éÕÒÇø¼ä´óСһֱ¶¼ÊÇÆæÊý if(nums[m] == nums[m + 1]) l = m + 2; else h = m; } @@ -175,7 +175,7 @@ public int singleNonDuplicate(int[] nums) { ÒòΪ×îСµÄº¢×Ó×îÈÝÒ׵õ½Âú×㣬Òò´ËÏÈÂú×ã×îСº¢×Ó¡£¸øÒ»¸öº¢×ӵıý¸ÉÓ¦µ±¾¡Á¿Ð¡ÓÖÄÜÂú×ã¸Ãº¢×Ó£¬ÕâÑù´ó±ý¸É¾ÍÄÜÄÃÀ´¸øÂú×ã¶È±È½Ï´óµÄº¢×Ó¡£ -Ö¤Ã÷£º¼ÙÉèÔÚij´ÎÑ¡ÔñÖУ¬Ì°ÐIJßÂÔÑ¡Ôñ¸øµÚ i ¸öº¢×Ó·ÖÅäµÚ m ¸ö±ý¸É£¬²¢ÇÒµÚ i ¸öº¢×ÓÂú×ã¶È×îС£¬µÚ m ¸ö±ý¸ÉΪ¿ÉÒÔÂú×ãµÚ i ¸öº¢×ÓµÄ×îС±ý¸É£¬ÀûÓÃÌ°ÐIJßÂÔ×îÖÕ¿ÉÒÔÂú×ã k ¸öº¢×Ó¡£¼ÙÉè×îÓŲßÂÔÔÚÕâ´ÎÑ¡ÔñÖиø i ¸öº¢×Ó·ÖÅäµÚ n ¸ö±ý¸É£¬²¢ÇÒÕâ¸ö±ý¸É´óÓÚµÚ m ¸ö±ý¸É£¬ÄÇô×îÓŲßÂÔ×îÖÕÐèÒªÂú×ã´óÓÚ k ¸öº¢×Ó¡£ÎÒÃÇ·¢ÏÖʹÓÃµÚ m ¸ö±ý¸ÉÈ¥Ìæ´úµÚ n ¸ö±ý¸ÉÍêÈ«²»Ó°ÏìºóÐøµÄ½á¹û£¬Òò´ËÌ°ÐIJßÂÔ¾ÍÊÇ×îÓŲßÂÔ£¬Òò´ËÌ°ÐIJßÂÔÊÇ×îÓŵġ£ +Ö¤Ã÷£º¼ÙÉèÔÚij´ÎÑ¡ÔñÖУ¬Ì°ÐIJßÂÔÑ¡Ôñ¸øµÚ i ¸öº¢×Ó·ÖÅäµÚ m ¸ö±ý¸É£¬²¢ÇÒµÚ i ¸öº¢×ÓÂú×ã¶È×îС£¬µÚ m ¸ö±ý¸ÉΪ¿ÉÒÔÂú×ãµÚ i ¸öº¢×ÓµÄ×îС±ý¸É£¬ÀûÓÃÌ°ÐIJßÂÔ×îÖÕ¿ÉÒÔÂú×ã k ¸öº¢×Ó¡£¼ÙÉè×îÓŲßÂÔÔÚÕâ´ÎÑ¡ÔñÖиø i ¸öº¢×Ó·ÖÅäµÚ n ¸ö±ý¸É£¬²¢ÇÒÕâ¸ö±ý¸É´óÓÚµÚ m ¸ö±ý¸É¡£ÎÒÃÇ·¢ÏÖʹÓÃµÚ m ¸ö±ý¸ÉÈ¥Ìæ´úµÚ n ¸ö±ý¸ÉÍêÈ«²»Ó°ÏìºóÐøµÄ½á¹û£¬Òò´Ë²»´æÔÚ±ÈÌ°ÐIJßÂÔ¸üÓŵIJßÂÔ£¬¼´Ì°ÐIJßÂÔ¾ÍÊÇ×îÓŲßÂÔ¡£ ```java public int findContentChildren(int[] g, int[] s) { @@ -4884,11 +4884,9 @@ public int maxProduct(String[] words) { # ²Î¿¼×ÊÁÏ - [Leetcode](https://leetcode.com/problemset/algorithms/?status=Todo) -- ½£Ö¸ Offer -- ³ÌÐòÔ±ÃæÊÔ½ðµä -- ±à³ÌÖ®ÃÀ -- ³ÌÐòÔ±´úÂëÃæÊÔÖ¸ÄÏ - Êý¾Ý½á¹¹ÓëËã·¨·ÖÎö - Ëã·¨ -- ÍõµÀÂÛ̳¼ÆËã»ú¿¼ÑлúÊÔÖ¸ÄÏ +- ½£Ö¸ Offer +- ±à³ÌÖ®ÃÀ +- ³ÌÐòÔ±´úÂëÃæÊÔÖ¸ÄÏ