{"id":201320,"date":"2024-05-10T02:30:21","date_gmt":"2024-05-09T18:30:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/201320.html"},"modified":"2024-05-10T02:30:24","modified_gmt":"2024-05-09T18:30:24","slug":"java%e6%9c%89%e5%93%aa%e4%ba%9b%e7%bb%8f%e5%85%b8%e7%9a%84%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/201320.html","title":{"rendered":"Java\u6709\u54ea\u4e9b\u7ecf\u5178\u7684\u7b97\u6cd5"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24141533\/2b7ffd8a-6356-41e4-8b26-c8022585ee0b.webp\" alt=\"Java\u6709\u54ea\u4e9b\u7ecf\u5178\u7684\u7b97\u6cd5\" \/><\/p>\n<p><p>Java\u4f5c\u4e3a\u4e00\u79cd\u6d41\u884c\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5728\u8f6f\u4ef6\u5f00\u53d1\u548c\u8ba1\u7b97\u673a\u79d1\u5b66\u9886\u57df\u53d1\u6325\u4e86\u5de8\u5927\u7684\u4f5c\u7528\u3002\u5176\u7b97\u6cd5\u7684\u5b9e\u73b0\u4e0d\u4ec5\u6709\u52a9\u4e8e\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\uff0c\u8fd8\u80fd\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u6027\u80fd\u3002<strong>Java\u4e2d\u7684\u7ecf\u5178\u7b97\u6cd5\u4e3b\u8981\u5305\u62ec\u6392\u5e8f\u7b97\u6cd5\u3001\u641c\u7d22\u7b97\u6cd5\u3001\u5b57\u7b26\u4e32\u5904\u7406\u3001\u56fe\u7b97\u6cd5\u4ee5\u53ca\u52a8\u6001\u89c4\u5212<\/strong>\u3002\u5728\u8fd9\u4e9b\u7b97\u6cd5\u4e2d\uff0c<strong>\u6392\u5e8f\u7b97\u6cd5<\/strong>\u626e\u6f14\u7740\u5c24\u4e3a\u5173\u952e\u7684\u89d2\u8272\uff0c\u56e0\u4e3a\u6392\u5e8f\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u6700\u57fa\u672c\u4e14\u5e7f\u6cdb\u5e94\u7528\u7684\u95ee\u9898\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u6392\u5e8f\u7b97\u6cd5<\/h2>\n<\/p>\n<p><h3>\u63d2\u5165\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u63d2\u5165\u6392\u5e8f\u662f\u6700\u7b80\u5355\u76f4\u89c2\u7684\u6392\u5e8f\u65b9\u5f0f\u4e4b\u4e00\uff0c\u5176\u6838\u5fc3\u601d\u60f3\u662f\u53d6\u672a\u6392\u5e8f\u533a\u95f4\u4e2d\u7684\u5143\u7d20\uff0c\u5728\u5df2\u6392\u5e8f\u533a\u95f4\u4e2d\u627e\u5230\u5408\u9002\u7684\u63d2\u5165\u4f4d\u7f6e\u5c06\u5176\u63d2\u5165\uff0c\u5e76\u4fdd\u8bc1\u5df2\u6392\u5e8f\u533a\u95f4\u6570\u636e\u4e00\u76f4\u6709\u5e8f\u3002\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n^2)\uff0c\u5728\u6570\u636e\u91cf\u5c0f\u6216\u51e0\u4e4e\u6709\u5e8f\u7684\u60c5\u51b5\u4e0b\u8868\u73b0\u826f\u597d\u3002<\/p>\n<\/p>\n<p><h3>\u5f52\u5e76\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u5f52\u5e76\u6392\u5e8f\u5219\u662f\u5178\u578b\u7684\u5206\u6cbb\u7b56\u7565\u5e94\u7528\uff0c\u5c06\u6570\u636e\u4e0d\u65ad\u5206\u5272\u6210\u8f83\u5c0f\u7684\u5b50\u5e8f\u5217\u5904\u7406\uff0c\u6700\u540e\u518d\u5c06\u6392\u5e8f\u597d\u7684\u5b50\u5e8f\u5217\u5408\u5e76\u3002\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7a33\u5b9a\u5728O(nlogn)\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u6392\u5e8f\u3002\u5f52\u5e76\u6392\u5e8f\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u7a33\u5b9a\u3001\u6548\u7387\u8f83\u9ad8\uff0c\u4f46\u9700\u8981\u989d\u5916\u7684\u5b58\u50a8\u7a7a\u95f4\u7528\u4e8e\u5408\u5e76\u8fc7\u7a0b\u4e2d\u7684\u4e34\u65f6\u5b58\u50a8\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u641c\u7d22\u7b97\u6cd5<\/h2>\n<\/p>\n<p><h3>\u4e8c\u5206\u67e5\u627e<\/h3>\n<\/p>\n<p><p>\u4e8c\u5206\u67e5\u627e\u662f\u5728\u6709\u5e8f\u5e8f\u5217\u4e2d\u67e5\u627e\u7279\u5b9a\u5143\u7d20\u7684\u9ad8\u6548\u7b97\u6cd5\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(logn)\uff0c\u901a\u8fc7\u6bcf\u6b21\u67e5\u627e\u5c06\u641c\u7d22\u8303\u56f4\u7f29\u5c0f\u4e00\u534a\uff0c\u6781\u5927\u7684\u63d0\u9ad8\u4e86\u67e5\u627e\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08DFS\uff09\u4e0e\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08BFS\uff09<\/h3>\n<\/p>\n<p><p>\u5728\u56fe\u5f62\u548c\u6811\u7ed3\u6784\u7684\u641c\u7d22\u95ee\u9898\u4e2d\uff0c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u548c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u662f\u6700\u57fa\u672c\u7684\u7b56\u7565\u3002DFS\u901a\u8fc7\u5c3d\u53ef\u80fd\u6df1\u7684\u641c\u7d22\u6811\u7684\u5206\u652f\u8fbe\u5230\u641c\u7d22\u76ee\u7684\uff0c\u800cBFS\u5219\u662f\u9010\u5c42\u8fdb\u884c\u641c\u7d22\u3002\u8fd9\u4e24\u79cd\u7b97\u6cd5\u5404\u6709\u4f18\u52bf\uff0cDFS\u5e38\u7528\u4e8e\u8def\u5f84\u7684\u63a2\u7d22\uff0cBFS\u5219\u9002\u7528\u4e8e\u627e\u5230\u6700\u77ed\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5b57\u7b26\u4e32\u5904\u7406<\/h2>\n<\/p>\n<p><h3>KMP\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u67e5\u627e\u662f\u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684\u95ee\u9898\uff0cKMP\u7b97\u6cd5\u5229\u7528\u5339\u914d\u5931\u8d25\u540e\u7684\u4fe1\u606f\uff0c\u51cf\u5c11\u5b57\u7b26\u4e32\u56de\u6eaf\u7684\u6b21\u6570\uff0c\u63d0\u9ad8\u641c\u7d22\u6548\u7387\u3002\u5b83\u7684\u6838\u5fc3\u5728\u4e8e\u6784\u9020\u4e00\u4e2a\u90e8\u5206\u5339\u914d\u8868\uff0c\u4f7f\u5f97\u6bcf\u6b21\u5339\u914d\u5931\u8d25\u65f6\uff0c\u53ef\u4ee5\u77e5\u9053\u4e0b\u4e00\u6b65\u7684\u6700\u4f73\u5339\u914d\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>Trie\u6811<\/h3>\n<\/p>\n<p><p>Trie\u6811\u6216\u8005\u79f0\u4e3a\u524d\u7f00\u6811\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u5b57\u7b26\u4e32\u96c6\u5408\u7684\u5feb\u901f\u68c0\u7d22\u95ee\u9898\u3002\u901a\u8fc7\u6811\u5f62\u7ed3\u6784\u5c06\u5171\u540c\u524d\u7f00\u7684\u5b57\u7b26\u4e32\u5408\u5e76\u5728\u4e00\u8d77\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u51cf\u5c11\u67e5\u8be2\u65f6\u95f4\uff0c\u4fc3\u8fdb\u9ad8\u6548\u7684\u8bcd\u9891\u6216\u8005\u5b57\u7b26\u4e32\u5b58\u5728\u6027\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u56fe\u7b97\u6cd5<\/h2>\n<\/p>\n<p><h3>Dijkstra\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u7528\u4e8e\u89e3\u51b3\u5e26\u6743\u6700\u77ed\u8def\u5f84\u95ee\u9898\u7684Dijkstra\u7b97\u6cd5\uff0c\u662f\u56fe\u8bba\u4e2d\u7684\u7ecf\u5178\u7b97\u6cd5\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u627e\u51fa\u4e00\u4e2a\u8282\u70b9\u5230\u5176\u4ed6\u6240\u6709\u8282\u70b9\u7684\u6700\u77ed\u8def\u5f84\uff0c\u4e3b\u8981\u7528\u4e8e\u6743\u503c\u4e3a\u975e\u8d1f\u7684\u6709\u5411\u56fe\u6216\u65e0\u5411\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u6700\u5c0f\u751f\u6210\u6811\u7b97\u6cd5\uff08Prim\u3001Kruskal\uff09<\/h3>\n<\/p>\n<p><p>\u5728\u8fde\u63a5\u6240\u6709\u8282\u70b9\u4e14\u8fb9\u7684\u6743\u91cd\u603b\u548c\u6700\u5c0f\u7684\u56fe\u4e2d\uff0c\u6784\u5efa\u6700\u5c0f\u751f\u6210\u6811\u662f\u7f51\u7edc\u8bbe\u8ba1\u7b49\u9886\u57df\u7684\u5e38\u89c1\u9700\u6c42\u3002Prim\u548cKruskal\u7b97\u6cd5\u662f\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u4e24\u79cd\u5178\u578b\u65b9\u6cd5\uff0c\u5b83\u4eec\u5206\u522b\u9002\u7528\u4e8e\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u7ed3\u6784\u5e76\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u5bfb\u627e\u6700\u4f18\u89e3\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u52a8\u6001\u89c4\u5212<\/h2>\n<\/p>\n<p><h3>\u80cc\u5305\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u80cc\u5305\u95ee\u9898\u662f\u52a8\u6001\u89c4\u5212\u7b97\u6cd5\u89e3\u51b3\u7684\u7ecf\u5178\u95ee\u9898\u4e4b\u4e00\uff0c\u6d89\u53ca\u9009\u62e9\u4e00\u7ec4\u7269\u54c1\u88c5\u5165\u5bb9\u91cf\u6709\u9650\u7684\u80cc\u5305\u4e2d\uff0c\u4f7f\u5f97\u5305\u5185\u7269\u54c1\u7684\u4ef7\u503c\u6700\u5927\u5316\u3002\u6b64\u7c7b\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u5c55\u793a\u4e86\u52a8\u6001\u89c4\u5212\u5904\u7406\u590d\u6742\u51b3\u7b56\u8fc7\u7a0b\u7684\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217<\/h3>\n<\/p>\n<p><p>\u5728\u4e24\u4e2a\u5e8f\u5217\u4e2d\uff0c\u627e\u51fa\u6240\u6709\u51fa\u73b0\u987a\u5e8f\u4e00\u81f4\u4e14\u957f\u5ea6\u6700\u957f\u7684\u5b50\u5e8f\u5217\uff0c\u662f\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\uff08LCS\uff09\u95ee\u9898\u3002\u901a\u8fc7\u52a8\u6001\u89c4\u5212\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3LCS\u95ee\u9898\uff0c\u5176\u89e3\u6cd5\u5728\u6587\u672c\u6bd4\u8f83\u3001\u751f\u7269\u4fe1\u606f\u5b66\u7b49\u9886\u57df\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>Java\u8bed\u8a00\u7684\u5e7f\u6cdb\u5e94\u7528\u8ba9\u8fd9\u4e9b\u7b97\u6cd5\u7684\u5b9e\u73b0\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u548c\u9ad8\u6548\u3002\u638c\u63e1Java\u4e2d\u7684\u8fd9\u4e9b\u7ecf\u5178\u7b97\u6cd5\u4e0d\u4ec5\u80fd\u591f\u63d0\u5347\u4e2a\u4eba\u7f16\u7a0b\u6280\u80fd\uff0c\u8fd8\u80fd\u5728\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u65f6\u63d0\u4f9b\u6709\u6548\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u54ea\u4e9b\u662fJava\u4e2d\u5e38\u7528\u7684\u7ecf\u5178\u7b97\u6cd5\uff1f<\/strong><\/p>\n<p>Java\u4e2d\u6709\u8bb8\u591a\u7ecf\u5178\u7684\u7b97\u6cd5\uff0c\u4ee5\u4e0b\u662f\u5176\u4e2d\u51e0\u4e2a\u5e38\u7528\u7684\uff1a<\/p>\n<p><strong>1. \u5feb\u901f\u6392\u5e8f\u7b97\u6cd5<\/strong>\uff1a\u5feb\u901f\u6392\u5e8f\u662f\u4e00\u79cd\u5206\u6cbb\u7b97\u6cd5\uff0c\u901a\u8fc7\u9009\u62e9\u4e00\u4e2a\u57fa\u51c6\u5143\u7d20\u5c06\u6570\u7ec4\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u7136\u540e\u5bf9\u5b50\u6570\u7ec4\u8fdb\u884c\u9012\u5f52\u6392\u5e8f\u3002\u5b83\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n log n)\uff0c\u662f\u6392\u5e8f\u7b97\u6cd5\u4e2d\u6027\u80fd\u8f83\u597d\u7684\u4e00\u79cd\u3002<\/p>\n<p><strong>2. \u54c8\u5e0c\u7b97\u6cd5<\/strong>\uff1a\u54c8\u5e0c\u7b97\u6cd5\u662f\u4e00\u79cd\u5c06\u6570\u636e\u6620\u5c04\u5230\u56fa\u5b9a\u5927\u5c0f\u7684\u6876\u4e2d\u7684\u7b97\u6cd5\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u54c8\u5e0c\u8868\u3001\u54c8\u5e0c\u96c6\u5408\u7b49\uff0c\u7528\u4e8e\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002Java\u4e2d\u5e38\u7528\u7684\u54c8\u5e0c\u7b97\u6cd5\u6709MD5\u3001SHA-1\u3001SHA-256\u7b49\u3002<\/p>\n<p><strong>3. \u52a8\u6001\u89c4\u5212\u7b97\u6cd5<\/strong>\uff1a\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u901a\u8fc7\u5206\u6790\u539f\u95ee\u9898\u4e0e\u5b50\u95ee\u9898\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u4ece\u800c\u5c06\u95ee\u9898\u62c6\u89e3\u4e3a\u591a\u4e2a\u5b50\u95ee\u9898\u6765\u89e3\u51b3\u7684\u7b97\u6cd5\u3002\u5b83\u5e38\u7528\u4e8e\u89e3\u51b3\u6700\u77ed\u8def\u5f84\u3001\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u7b49\u95ee\u9898\u3002<\/p>\n<p><strong>4. \u56fe\u7b97\u6cd5<\/strong>\uff1a\u56fe\u7b97\u6cd5\u662f\u7528\u4e8e\u89e3\u51b3\u56fe\u76f8\u5173\u95ee\u9898\u7684\u4e00\u7c7b\u7b97\u6cd5\u3002\u6bd4\u5982\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff08BFS\uff09\u548c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff08DFS\uff09\u7528\u4e8e\u904d\u5386\u56fe\uff1bDijkstra\u7b97\u6cd5\u548cA*\u7b97\u6cd5\u7528\u4e8e\u56fe\u4e2d\u7684\u6700\u77ed\u8def\u5f84\u95ee\u9898\u3002<\/p>\n<p><strong>5. \u8d2a\u5fc3\u7b97\u6cd5<\/strong>\uff1a\u8d2a\u5fc3\u7b97\u6cd5\u662f\u4e00\u79cd\u901a\u8fc7\u6bcf\u4e00\u6b65\u9009\u62e9\u5c40\u90e8\u6700\u4f18\u89e3\u6765\u8fbe\u5230\u5168\u5c40\u6700\u4f18\u89e3\u7684\u7b97\u6cd5\u3002\u5b83\u5e38\u7528\u4e8e\u89e3\u51b3\u80cc\u5305\u95ee\u9898\u3001\u4efb\u52a1\u8c03\u5ea6\u7b49\u4f18\u5316\u95ee\u9898\u3002<\/p>\n<p><strong>6. \u5206\u6cbb\u7b97\u6cd5<\/strong>\uff1a\u5206\u6cbb\u7b97\u6cd5\u662f\u4e00\u79cd\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u591a\u4e2a\u72ec\u7acb\u7684\u5b50\u95ee\u9898\uff0c\u7136\u540e\u5c06\u5b50\u95ee\u9898\u7684\u7ed3\u679c\u5408\u5e76\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u7684\u65b9\u6cd5\u3002\u5b83\u5e38\u7528\u4e8e\u89e3\u51b3\u5f52\u5e76\u6392\u5e8f\u3001\u6c42\u89e3\u9006\u5e8f\u5bf9\u7b49\u95ee\u9898\u3002<\/p>\n<p>\u8fd9\u4e9b\u7b97\u6cd5\u5728Java\u7f16\u7a0b\u4e2d\u5e7f\u6cdb\u5e94\u7528\uff0c\u6db5\u76d6\u4e86\u6392\u5e8f\u3001\u67e5\u627e\u3001\u4f18\u5316\u7b49\u591a\u4e2a\u9886\u57df\uff0c\u662fJava\u7a0b\u5e8f\u5458\u5fc5\u5907\u7684\u57fa\u7840\u77e5\u8bc6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Java\u4f5c\u4e3a\u4e00\u79cd\u6d41\u884c\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5728\u8f6f\u4ef6\u5f00\u53d1\u548c\u8ba1\u7b97\u673a\u79d1\u5b66\u9886\u57df\u53d1\u6325\u4e86\u5de8\u5927\u7684\u4f5c\u7528\u3002\u5176\u7b97\u6cd5\u7684\u5b9e\u73b0\u4e0d\u4ec5\u6709\u52a9\u4e8e\u89e3\u51b3\u5b9e\u9645\u95ee\u9898 [&hellip;]","protected":false},"author":3,"featured_media":201321,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/201320"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=201320"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/201320\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/201321"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=201320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=201320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=201320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}