{"id":35316,"date":"2024-11-26T09:10:15","date_gmt":"2024-11-26T01:10:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35316\/"},"modified":"2024-11-26T09:10:15","modified_gmt":"2024-11-26T01:10:15","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e7%bb%99%e6%9c%89%e5%ba%8f%e6%95%b0%e7%bb%84%e6%8e%92%e5%90%8d%e6%ac%a1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35316\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u7ed9\u6709\u5e8f\u6570\u7ec4\u6392\u540d\u6b21"},"content":{"rendered":"<blockquote><p>\n  \u5728java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528arrays.binarysearch()\u65b9\u6cd5\u7ed9\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6392\u540d\u6b21\u3002\u4f7f\u7528\u65b9\u6cd5\uff1a\u6570\u7ec4\u5fc5\u987b\u5df2\u6392\u5e8f\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u76ee\u6807\u5143\u7d20\u7684\u7d22\u5f15\u3002\u5982\u679c\u76ee\u6807\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a\u8d1f\u6570\uff0c\u8868\u793a\u5176\u63d2\u5165\u70b9\u3002\u8be5\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3ao(log n)\uff0c\u5176\u4e2dn\u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/04\/2024110400073664192.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u7ed9\u6709\u5e8f\u6570\u7ec4\u6392\u540d\u6b21\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u7ed9\u6709\u5e8f\u6570\u7ec4\u6392\u540d\u6b21\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u4e2d\u7ed9\u6709\u5e8f\u6570\u7ec4\u6392\u540d\u6b21<\/strong><\/p>\n<p>\u5728Java\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Arrays.binarySearch()\u65b9\u6cd5\u7ed9\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6392\u540d\u6b21\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u7ed9\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a\u8d1f\u6570\uff08\u63d2\u5165\u70b9\uff09\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5, 6, 7};\nint target = 5;\nint rank = Arrays.binarySearch(arr, target);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>4<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>arr\u662f\u5df2\u6392\u5e8f\u6570\u7ec4\u3002<\/li>\n<li>target\u662f\u6211\u4eec\u8981\u67e5\u627e\u7684\u5143\u7d20\u3002<\/li>\n<li>rank\u662ftarget\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0c\u5176\u503c\u662f4\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff1a<\/strong><\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5, 6, 7};\nint target = 8;\nint rank = Arrays.binarySearch(arr, target);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>-8<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>target\u4e0d\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\u3002<\/li>\n<li>\u8d1f\u53f7\u8868\u793a\u63d2\u5165\u70b9\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u662f-8\uff0c\u8868\u793atarget\u5e94\u63d2\u5165\u5230\u6570\u7ec4\u7d22\u5f157\u4e4b\u524d\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u6570\u7ec4\u5fc5\u987b\u662f\u6392\u597d\u5e8f\u7684\uff0c\u5426\u5219\u8be5\u65b9\u6cd5\u5c06\u4e0d\u8d77\u4f5c\u7528\u3002<\/li>\n<li>\u5982\u679c\u5b58\u5728\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\uff0c\u5219\u8be5\u65b9\u6cd5\u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/li>\n<li>\u8be5\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(log n)\uff0c\u5176\u4e2dn\u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u7ed9\u6709\u5e8f\u6570\u7ec4\u6392\u540d\u6b21\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528arrays.binarysearch()\u65b9\u6cd5\u7ed9\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6392\u540d\u6b21\u3002\u4f7f\u7528\u65b9\u6cd5\uff1a\u6570\u7ec4\u5fc5\u987b\u5df2\u6392\u5e8f\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u76ee\u6807\u5143\u7d20\u7684\u7d22\u5f15\u3002\u5982\u679c\u76ee\u6807\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a\u8d1f\u6570\uff0c\u8868\u793a\u5176\u63d2\u5165\u70b9\u3002\u8be5\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3ao(log n)\uff0c\u5176\u4e2dn\u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002 Java\u4e2d\u7ed9\u6709\u5e8f\u6570\u7ec4\u6392\u540d\u6b21 \u5728Java\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Arrays.binarySearch()\u65b9\u6cd5\u7ed9\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6392\u540d\u6b21\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u7ed9\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a\u8d1f\u6570\uff08\u63d2\u5165\u70b9\uff09\u3002 \u4f7f\u7528\u65b9\u6cd5\uff1a int[] arr = {1, 2, 3, 4, 5, 6, 7}; int target = 5; int rank = Arrays.binarySearch(arr, target); \u767b\u5f55\u540e\u590d\u5236 \u8f93\u51fa\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 4 \u767b\u5f55\u540e\u590d\u5236 \u89e3\u91ca\uff1a arr\u662f\u5df2\u6392\u5e8f\u6570\u7ec4\u3002 target\u662f\u6211\u4eec\u8981\u67e5\u627e\u7684\u5143\u7d20\u3002 rank\u662ftarget\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0c\u5176\u503c\u662f4\u3002 \u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff1a int[] arr = {1, 2, 3, 4, 5, 6, 7}; int target = 8; int rank = Arrays.binarySearch(arr, target); \u767b\u5f55\u540e\u590d\u5236 \u8f93\u51fa\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-35316","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=35316"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35316\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}