{"id":6673,"date":"2024-07-31T13:01:01","date_gmt":"2024-07-31T05:01:01","guid":{"rendered":""},"modified":"2024-07-31T13:01:01","modified_gmt":"2024-07-31T05:01:01","slug":"\u6446\u52a8\u5e8f\u5217_\u6446\u52a8\u6027\u540d\u8bcd\u89e3\u91ca","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6673.html","title":{"rendered":"\u6446\u52a8\u5e8f\u5217_\u6446\u52a8\u6027\u540d\u8bcd\u89e3\u91ca"},"content":{"rendered":"<p><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"display: none;\"> \n <path stroke-linecap=\"round\" d=\"M5,0 0,2.5 5,5z\" id=\"raphael-marker-block\" style=\"-webkit-tap-highlight-color: rgba(0, 0, 0, 0);\"><\/path> \n<\/svg> <\/p>\n<h3>\u9898\u76ee<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20200219214914389.png\" =\"\" =\"Alt\" alt=\"\u6446\u52a8\u5e8f\u5217_\u6446\u52a8\u6027\u540d\u8bcd\u89e3\u91ca (https:\/\/mushiming.com\/)  \u7b2c1\u5f20\" title=\"\u6446\u52a8\u5e8f\u5217_\u6446\u52a8\u6027\u540d\u8bcd\u89e3\u91ca  \u7b2c1\u5f20-\u7a46\u4e16\u660e\u535a\u5ba2\" ><\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u9996\u5148\u7406\u89e3\u4e00\u4e0b\u4ec0\u4e48\u662f\u6446\u52a8\u5e8f\u5217\uff0c\u6446\u52a8\u5e8f\u5217\u5c31\u662f\u4e24\u4e24\u4e4b\u95f4\u7684\u5dee\u662f\u6b63\u6570\u548c\u8d1f\u6570\u4ea4\u66ff\uff0c\u90a3\u4e48\u9996\u5148\u6765\u5206\u6790\u4e00\u4e0b\u8be5\u9898\u7684\u8fb9\u754c\u95ee\u9898\uff1a<strong>\u7b2c\u4e00\u4e2a\u8fb9\u754c\u5c31\u662f\u5355\u4e2a\u5143\u7d20\u4e5f\u5c5e\u4e8e\u6446\u52a8\u5e8f\u5217\uff0c\u7b2c\u4e8c\u4e2a\u8fb9\u754c\u5c31\u662f\u4e00\u4e2a\u6446\u52a8\u5e8f\u5217\u53ea\u6709\u4e24\u4e2a\u5143\u7d20\uff08\u610f\u5473\u7740\u4ed6\u4eec\u7684\u5dee\u4e3a\u6b63\u6570\u6216\u8005\u8d1f\u6570\uff09<\/strong><br \/> \u90a3\u4e48\u6211\u4eec\u7684\u601d\u8def\u662f\u4ec0\u4e48\u5462\uff0c\u8be5\u9898\u7684\u6807\u7b7e\u4e3a\u8d2a\u5fc3\u7b97\u6cd5\u7684\u8303\u7574\uff0c\u90a3\u6211\u4eec\u5982\u4f55\u4ece\u8d2a\u5fc3\u7b97\u6cd5\u5165\u624b\u5462\uff1f<\/p>\n<h4>\u53c2\u8003\u601d\u8def\u4e00<\/h4>\n<p>\u65e2\u7136\u8981\u6c42\u6700\u957f\u6446\u52a8\u5e8f\u5217\u957f\u5ea6\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u6309\u7167\u6446\u52a8\u5e8f\u5217\u7684\u5b9a\u4e49\u6765\uff0c\u76f8\u6bd4\u8f83\u4e0a\u4e00\u6b21\u7684\u5dee\u4e3a\u6b63\u6570\u6216\u8005\u8d1f\u6570\uff0c\u6765\u9009\u62e9\u672c\u6b21\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u4e3a\u6b64\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u4e24\u4e2a\u53d8\u91cfpre\u548ccur\uff0c\u5206\u522b\u8868\u793a\u4e0a\u4e00\u6b21\u548c\u73b0\u5728\u7684\u5dee\u4e3a\u6b63\u6570\u6216\u8005\u8d1f\u6570\uff08\u6b63\u6570\u6211\u4eec\u75281\u8868\u793a\uff0c\u8d1f\u6570\u6211\u4eec\u7528-1\u8868\u793a\uff09\uff0c\u5982\u679cpre\u548ccur\u4e0d\u7b49\uff0c\u8bf4\u660e\u662f\u7b26\u5408\u6446\u52a8\u5e8f\u5217\u7684\u5b9a\u4e49\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5c06\u8be5\u5143\u7d20\u52a0\u5165\u5230\u6446\u52a8\u5e8f\u5217\u4e2d\uff0c\u5426\u5219\u5c31\u4e0d\u52a0\uff0c\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"language-java prettyprint linenums\"><code>class Solution { public: int wiggleMaxLength(vector&lt;int&gt;&amp; nums) { int size = nums.size(); if (size &lt; 2) return size; int pre = 0; int cur; int res = 1; for (int i = 1; i &lt; size; i++) { if (nums[i] == nums[i - 1]) continue; cur = nums[i] &gt; nums[i - 1] ? 1 : -1; res += cur != pre; pre = cur; } return res; } }; <\/code><\/pre>\n<p>\u9996\u5148\u6211\u4eec\u628a<strong>\u53d8\u91cfpre\u521d\u59cb\u5316\u4e3a0<\/strong>\uff0c\u8fd9\u6837\u5c31\u80fd\u89e3\u51b3\u8fb9\u754c\u95ee\u9898\uff0c\u56e0\u4e3a\u5bf9\u4e8e\u6700\u521d\u7684\u4e24\u4e2a\u6570\u7684\u5dee\uff0c\u65e0\u8bba\u4e3a\u6b63\u6570\u6216\u8005\u8d1f\u6570\uff0c\u90fd\u80fd\u5c06\u4e4b\u7eb3\u5165\u5230\u6446\u52a8\u5e8f\u5217\u8303\u56f4\u5185\uff0c\u8fd4\u56de\u7684\u53d8\u91cf\u521d\u59cb\u503c\u8bbe\u4e3a1\uff08<strong>\u8868\u793a\u8fb9\u754c<\/strong>\uff09<br \/> <strong>\u6bcf\u6b21\u6211\u4eec\u90fd\u5224\u65ad\u6b64\u65f6\u7684\u5dee\u4e3a\u6b63\u6570\u6216\u8005\u8d1f\u6570\uff08\u4fdd\u5b58\u5728cur\u53d8\u91cf\u4e2d\uff09\uff0c\u7136\u540e\u548cpre\u53d8\u91cf\u76f8\u6bd4\u8f83\uff0c\u4e0d\u76f8\u7b49\u7684\u5c31\u7eb3\u5165\u6446\u52a8\u5e8f\u5217\u8303\u56f4\uff0c\u540c\u65f6\u5c06pre\u53d8\u91cf\u66f4\u65b0\u4e3acur\u7684\u503c<\/strong><\/p>\n<h4>\u53c2\u8003\u601d\u8def\u4e8c<\/h4>\n<p>\u8fd9\u4e2a\u601d\u8def\u6bd4\u8f83\u5de7\u5999\uff0c\u6446\u52a8\u5e8f\u5217\u7528\u56fe\u50cf\u6765\u8868\u793a\u7684\u8bdd\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u4e00\u4e2a\u7531\u6ce2\u5cf0\u548c\u6ce2\u8c37\u4ea4\u66ff\u6784\u6210\u7684\u56fe\u50cf\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u4e24\u4e2a\u53d8\u91cfup\u548cdown\u5206\u522b\u8868\u793a\u6ce2\u5cf0\u548c\u6ce2\u8c37\u7684\u6570\u91cf\uff0c<strong>\u5728\u6446\u52a8\u5e8f\u5217\u4e2d\uff0c\u4efb\u610f\u4e00\u4e2a\u6ce2\u5cf0\u7684\u6570\u91cf\u7b49\u4e8e\u4e4b\u524d\u6ce2\u8c37\u7684\u6570\u91cf\u52a01\uff0c\u540c\u6837\u4efb\u610f\u4e00\u4e2a\u6ce2\u8c37\u7684\u6570\u91cf\u7b49\u4e8e\u4e4b\u524d\u7684\u6ce2\u5cf0\u7684\u6570\u91cf\u52a01<\/strong><br \/> \u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u5229\u7528\u4e0a\u8ff0\u7684\u6027\u8d28\u6765\u6c42\u51fa\u95ee\u9898\u7684\u89e3\uff0c\u76f8\u5173\u4ee3\u7801\u4e3a\uff1a<\/p>\n<pre class=\"language-java prettyprint linenums\"><code>class Solution { public: int wiggleMaxLength(vector&lt;int&gt;&amp; nums) { int n = nums.size(); if (n &lt; 2) { return n; } int up = 1; int down = 1; for (int i = 1; i &lt; n; i++) { if (nums[i] &gt; nums[i - 1]) { up = down + 1; } if (nums[i] &lt; nums[i - 1]) { down = up + 1; } } return max(up, down); } } <\/code><\/pre>\n<p>\u6211\u4eec\u5206\u522b\u521d\u59cb\u5316up\u548cdown\u7684\u503c\u4e3a1\uff0c\u8868\u793a\u7b2c\u4e00\u4e2a\u5143\u7d20\u80af\u5b9a\u662f\u6446\u52a8\u5e8f\u5217\u8303\u56f4\u5185\uff0c\u7136\u540e\u6211\u4eec\u518d\u904d\u5386\u6570\u7ec4\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u9012\u589e\u7684\u5e8f\u5217\uff0c\u4ed6\u4eec\u4e4b\u524d\u7684\u6ce2\u8c37\u7684\u6570\u91cf\u662f\u76f8\u540c\u7684\uff0c\u540c\u6837\u7684\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u9012\u51cf\u7684\u5e8f\u5217\uff0c\u6ce2\u5cf0\u7684\u6570\u91cf\u4e5f\u662f\u4e00\u6837\u7684\uff0c\u6700\u540e\u8fd4\u56de\u6ce2\u5cf0\u548c\u6ce2\u8c37\u4e2d\u7684\u6700\u5927\u503c<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u5728\u8be5\u9898\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6bd4\u8f83\u5728\u5f53\u524d\u9636\u6bb5\u5224\u65ad<strong>\u5f53\u524d\u5b50\u95ee\u9898\u7684\u72b6\u6001\uff08\u8fd9\u91cc\u6307\u7684\u662f\u4e24\u4e2a\u5143\u7d20\u4e4b\u95f4\u7684\u5dee\uff09<\/strong>\uff0c<strong>\u9009\u62e9\u6700\u4f18\u89e3\uff08\u6700\u4f18\u89e3\u6307\u7684\u662f\u5982\u679c\u6ee1\u8db3\u5219\u52a0\u5165\u5e8f\u5217\uff0c\u4e0d\u6ee1\u8db3\u5c31\u4e0d\u52a0\u5165\u5e8f\u5217\uff09<\/strong>\uff0c\u901a\u8fc7\u6bcf\u6b21\u505a\u51fa\u6700\u4f18\u7684\u9009\u62e9\uff0c\u6700\u7ec8\u5f97\u5230\u6574\u4f53\u7684\u6700\u4f18\u89e3\uff08\u56e0\u4e3a\u6bcf\u6b21\u6211\u4eec\u90fd\u9009\u62e9\u52a0\u5165\u80fd\u6784\u6210\u6446\u52a8\u5e8f\u5217\u7684\u5143\u7d20\uff0c\u6240\u4ee5\u6700\u540e\u6574\u4e2a\u5e8f\u5217\u4e5f\u786e\u5b9a\u662f\u6446\u52a8\u5e8f\u5217\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"\u6446\u52a8\u5e8f\u5217_\u6446\u52a8\u6027\u540d\u8bcd\u89e3\u91ca\u9898\u76ee\u601d\u8def\u9996\u5148\u7406\u89e3\u4e00\u4e0b\u4ec0\u4e48\u662f\u6446\u52a8\u5e8f\u5217\uff0c\u6446\u52a8\u5e8f\u5217\u5c31\u662f\u4e24\u4e24\u4e4b\u95f4\u7684\u5dee\u662f\u6b63\u6570\u548c\u8d1f\u6570\u4ea4\u66ff\uff0c\u90a3\u4e48\u9996\u5148\u6765\u5206\u6790\u4e00...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6673"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6673"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6673\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}