{"id":549,"date":"2025-01-18T16:53:57","date_gmt":"2025-01-18T08:53:57","guid":{"rendered":"http:\/\/sqtcw.cn\/?p=549"},"modified":"2025-02-15T15:49:33","modified_gmt":"2025-02-15T07:49:33","slug":"python-%e6%b1%82%e5%bc%82%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"http:\/\/www.cnitw.com\/?p=549","title":{"rendered":"Python\u966a\u7ec31 \u6c42\u5f02\u5b9e\u4f8b &#8212;\u627e\u5355\u72ec\u7684\u6570"},"content":{"rendered":"\n<p>\u5728\u4e00\u4e2a\u73ed\u7ea7\u4e2d\uff0c\u6bcf\u4f4d\u540c\u5b66\u90fd\u62ff\u5230\u4e86\u4e00\u5f20\u5361\u7247\uff0c\u4e0a\u9762\u6709\u4e00\u4e2a\u6574\u6570\u3002\u6709\u8da3\u7684\u662f\uff0c\u9664\u4e86\u4e00\u4e2a\u6570\u5b57\u4e4b\u5916\uff0c\u6240\u6709\u7684\u6570\u5b57\u90fd\u6070\u597d\u51fa\u73b0\u4e86\u4e24\u6b21\u3002\u73b0\u5728\u9700\u8981\u4f60\u5e2e\u52a9\u73ed\u957f\u5c0fC\u5feb\u901f\u627e\u5230\u90a3\u4e2a\u62ff\u4e86\u72ec\u7279\u6570\u5b57\u5361\u7247\u7684\u540c\u5b66\u624b\u4e0a\u7684\u6570\u5b57\u662f\u4ec0\u4e48\u3002<\/p>\n\n\n\n<p>\u8981\u6c42\uff1a<\/p>\n\n\n\n<p>\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\uff0c\u4f7f\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\uff0c\u5176\u4e2d n \u662f\u73ed\u7ea7\u7684\u4eba\u6570\u3002<br>\u5c3d\u91cf\u51cf\u5c11\u989d\u5916\u7a7a\u95f4\u7684\u4f7f\u7528\uff0c\u4ee5\u4f53\u73b0\u4f60\u7684\u7b97\u6cd5\u4f18\u5316\u80fd\u529b\u3002<br>\u6d4b\u8bd5\u6837\u4f8b<br>\u6837\u4f8b1\uff1a<\/p>\n\n\n\n<p>\u8f93\u5165\uff1acards = [1, 1, 2, 2, 3, 3, 4, 5, 5]<br>\u8f93\u51fa\uff1a4<br>\u89e3\u91ca\uff1a\u62ff\u5230\u6570\u5b57 4 \u7684\u540c\u5b66\u662f\u552f\u4e00\u4e00\u4e2a\u6ca1\u6709\u914d\u5bf9\u7684\u3002<\/p>\n\n\n\n<p>\u8f93\u5165\uff1a<code>cards = [0, 1, 0, 1, 2]<\/code><br>\u8f93\u51fa\uff1a<code>2<\/code><br>\u89e3\u91ca\uff1a\u6570\u5b57 2 \u53ea\u51fa\u73b0\u4e00\u6b21\uff0c\u662f\u72ec\u7279\u7684\u5361\u7247\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><br><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def solution(cards):     # \u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a find_unique_card \u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 cards\uff0c\u4ee3\u8868\u73ed\u7ea7\u4e2d\u540c\u5b66\u4eec\u62ff\u5230\u7684\u5361\u7247\u4e0a\u7684\u6574\u6570\u5217\u8868    \n    result = 0           #\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf result \u4e3a 0\u3002\u8fd9\u4e2a\u53d8\u91cf\u5c06\u7528\u4e8e\u5b58\u50a8\u901a\u8fc7\u5f02\u6216\u8fd0\u7b97\u5f97\u5230\u7684\u7ed3\u679c \n\n    for card in cards :  #\u8fd9\u662f\u4e00\u4e2a\u5faa\u73af\uff0c\u904d\u5386\u8f93\u5165\u5217\u8868 cards \u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\n        result ^= card   #\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u7b26 ^    \u4efb\u4f55\u6570\u4e0e\u81ea\u8eab\u5f02\u6216\u7ed3\u679c\u4e3a 0\uff0c\u800c <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">0 \u4e0e\u4efb\u4f55\u6570\u5f02\u6216\u7ed3\u679c\u4e3a\u8be5\u6570<\/mark><\/strong>\u3002result \u6700\u7ec8\u5c06\u5b58\u50a8\u90a3\u4e2a\u552f\u4e00\u7684\u6570\u5b57\u3002\n    return result        #\u51fd\u6570\u8fd4\u56de result\uff0c\u5373\u627e\u5230\u7684\u552f\u4e00\u7684\u6570\u5b57\n\n\ncards = &#91;1, 1, 2, 2, 3, 3, 4, 5, 5]     \nprint(solution(cards))   #\u8c03\u7528 find_unique_card \u51fd\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u6253\u5370\u51fa\u6765\n\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def solution(cards):     # \u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a find_unique_card \u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 cards\uff0c\u4ee3\u8868\u73ed\u7ea7\u4e2d\u540c\u5b66\u4eec\u62ff\u5230\u7684\u5361\u7247\u4e0a\u7684\u6574\u6570\u5217\u8868    \n    result = 0           #\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf result \u4e3a 0\u3002\u8fd9\u4e2a\u53d8\u91cf\u5c06\u7528\u4e8e\u5b58\u50a8\u901a\u8fc7\u5f02\u6216\u8fd0\u7b97\u5f97\u5230\u7684\u7ed3\u679c \n\n    for card in cards :  #\u8fd9\u662f\u4e00\u4e2a\u5faa\u73af\uff0c\u904d\u5386\u8f93\u5165\u5217\u8868 cards \u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\n        result ^= card   #\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u7b26 ^    \u4efb\u4f55\u6570\u4e0e\u81ea\u8eab\u5f02\u6216\u7ed3\u679c\u4e3a 0\uff0c\u800c 0 \u4e0e\u4efb\u4f55\u6570\u5f02\u6216\u7ed3\u679c\u4e3a\u8be5\u6570\u3002result \u6700\u7ec8\u5c06\u5b58\u50a8\u90a3\u4e2a\u552f\u4e00\u7684\u6570\u5b57\u3002\n    return result        #\u51fd\u6570\u8fd4\u56de result\uff0c\u5373\u627e\u5230\u7684\u552f\u4e00\u7684\u6570\u5b57\n\n\ncards = &#91;1, 1, 2, 2, 3, 3, 4, 5, 5]     \nprint(solution(cards))   #\u8c03\u7528 find_unique_card \u51fd\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u6253\u5370\u51fa\u6765\n'''\n\u53ef\u4ee5\u7528if __name__ == \"__main__\":\u5c01\u88c5\u4e00\u4e2a\u6a21\u5757\uff0c\u4f5c\u6d4b\u8bd5\u4ee3\u7801\n'''\nif __name__ == \"__main__\":   #\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\uff0c\u5176\u4f5c\u7528\u4e3b\u8981\u662f\u533a\u5206\u5f53\u524d\u811a\u672c\u662f\u4f5c\u4e3a\u4e3b\u7a0b\u5e8f\u76f4\u63a5\u8fd0\u884c\u8fd8\u662f\u88ab\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\u5230\u5176\u4ed6\u811a\u672c\u4e2d\u3002\n    cards = &#91;1, 1, 2, 2, 3, 3, 4, 5, 5]     \n    print(solution(cards))   #\u8c03\u7528 find_unique_card \u51fd\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u6253\u5370\u51fa\u6765\n\n    cards2 = &#91;0, 1, 0, 1, 2]\n    print(solution(cards2))<\/code><\/pre>\n\n\n\n<p>\u5728 Python \u4e2d\uff0c<code>if __name__ == \"__main__\":<\/code> \u662f\u4e00\u4e2a\u5e38\u7528\u7684\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\uff0c\u5176\u4f5c\u7528\u4e3b\u8981\u662f\u533a\u5206\u5f53\u524d\u811a\u672c\u662f\u4f5c\u4e3a\u4e3b\u7a0b\u5e8f\u76f4\u63a5\u8fd0\u884c\u8fd8\u662f\u88ab\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\u5230\u5176\u4ed6\u811a\u672c\u4e2d\u3002\u4ee5\u4e0b\u662f\u5bf9\u5b83\u7684\u8be6\u7ec6\u89e3\u6790\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>__name__<\/code> \u53d8\u91cf<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 Python \u4e2d\uff0c\u6bcf\u4e2a\u6a21\u5757\u90fd\u6709\u4e00\u4e2a\u540d\u4e3a <code>__name__<\/code> \u7684\u5185\u7f6e\u53d8\u91cf\u3002<\/li>\n\n\n\n<li>\u5f53\u4e00\u4e2a\u6a21\u5757\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c<code>__name__<\/code> \u7684\u503c\u88ab\u8bbe\u7f6e\u4e3a <code>\"__main__\"<\/code>\u3002<\/li>\n\n\n\n<li>\u5f53\u4e00\u4e2a\u6a21\u5757\u88ab\u5bfc\u5165\u5230\u5176\u4ed6\u6a21\u5757\u4e2d\u65f6\uff0c<code>__name__<\/code> \u7684\u503c\u88ab\u8bbe\u7f6e\u4e3a\u6a21\u5757\u7684\u540d\u79f0\uff08\u4e0d\u5305\u62ec <code>.py<\/code> \u6269\u5c55\u540d\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>if __name__ == \"__main__\":<\/code> \u7684\u4f5c\u7528<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u4e2a\u6761\u4ef6\u8bed\u53e5\u7528\u4e8e\u5224\u65ad\u5f53\u524d\u6a21\u5757\u662f\u5426\u4f5c\u4e3a\u4e3b\u7a0b\u5e8f\u76f4\u63a5\u8fd0\u884c\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u5f53\u524d\u6a21\u5757\u662f\u76f4\u63a5\u8fd0\u884c\u7684\uff0c\u90a3\u4e48\u6761\u4ef6\u4e3a\u771f\uff0c\u8bed\u53e5\u5757\u4e2d\u7684\u4ee3\u7801\u5c06\u88ab\u6267\u884c\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u5f53\u524d\u6a21\u5757\u662f\u88ab\u5bfc\u5165\u5230\u5176\u4ed6\u6a21\u5757\u4e2d\uff0c\u90a3\u4e48\u6761\u4ef6\u4e3a\u5047\uff0c\u8bed\u53e5\u5757\u4e2d\u7684\u4ee3\u7801\u5c06\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u5e38\u7528\u4e8e\u6d4b\u8bd5\u4ee3\u7801\uff1a\u53ef\u4ee5\u5728\u6a21\u5757\u4e2d\u7f16\u5199\u4e00\u4e9b\u6d4b\u8bd5\u51fd\u6570\u6216\u793a\u4f8b\u4ee3\u7801\uff0c\u5e76\u5c06\u5b83\u4eec\u653e\u5728 <code>if __name__ == \"__main__\":<\/code> \u8bed\u53e5\u5757\u4e2d\u3002\u8fd9\u6837\uff0c\u5f53\u76f4\u63a5\u8fd0\u884c\u8be5\u6a21\u5757\u65f6\uff0c\u6d4b\u8bd5\u4ee3\u7801\u4f1a\u88ab\u6267\u884c\uff0c\u800c\u5f53\u8be5\u6a21\u5757\u88ab\u5bfc\u5165\u5230\u5176\u4ed6\u6a21\u5757\u4e2d\u65f6\uff0c\u6d4b\u8bd5\u4ee3\u7801\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u4ee3\u7801\u6267\u884c\u3002<\/li>\n\n\n\n<li>\u65b9\u4fbf\u6a21\u5757\u7684\u590d\u7528\uff1a\u53ef\u4ee5\u5c06\u4e00\u4e9b\u901a\u7528\u7684\u529f\u80fd\u5c01\u88c5\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u5e76\u5728 <code>if __name__ == \"__main__\":<\/code> \u8bed\u53e5\u5757\u4e2d\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u7528\u6cd5\u6216\u6d4b\u8bd5\u7528\u4f8b\u3002\u8fd9\u6837\uff0c\u5176\u4ed6\u5f00\u53d1\u8005\u53ef\u4ee5\u5bfc\u5165\u8fd9\u4e2a\u6a21\u5757\u5e76\u4f7f\u7528\u5176\u4e2d\u7684\u529f\u80fd\uff0c\u800c\u4e0d\u4f1a\u53d7\u5230\u6d4b\u8bd5\u4ee3\u7801\u7684\u5e72\u6270\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if __name__ == \"__main__\":\n    cards = &#91;1, 1, 2, 2, 3, 3, 4, 5, 5]\n    print(find_unique_card(cards))<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u8fd9\u4e2a\u811a\u672c\u88ab\u76f4\u63a5\u8fd0\u884c\uff0c\u90a3\u4e48 <code>cards<\/code> \u5217\u8868\u5c06\u88ab\u521b\u5efa\uff0c\u5e76\u4e14 <code>find_unique_card<\/code> \u51fd\u6570\u5c06\u88ab\u8c03\u7528\uff0c\u8f93\u51fa\u627e\u5230\u7684\u552f\u4e00\u6570\u5b57\u3002\u5982\u679c\u8fd9\u4e2a\u811a\u672c\u88ab\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\u5230\u5176\u4ed6\u811a\u672c\u4e2d\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u53ea\u6709\u5728\u5176\u4ed6\u811a\u672c\u4e2d\u660e\u786e\u8c03\u7528 <code>find_unique_card<\/code> \u51fd\u6570\u65f6\uff0c\u8be5\u51fd\u6570\u624d\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e00\u4e2a\u73ed\u7ea7\u4e2d\uff0c\u6bcf\u4f4d\u540c\u5b66\u90fd\u62ff\u5230\u4e86\u4e00\u5f20\u5361\u7247\uff0c\u4e0a\u9762\u6709\u4e00\u4e2a\u6574\u6570\u3002\u6709\u8da3\u7684\u662f\uff0c\u9664\u4e86\u4e00\u4e2a\u6570\u5b57\u4e4b\u5916\uff0c\u6240\u6709\u7684\u6570\u5b57\u90fd\u6070\u597d\u51fa\u73b0\u4e86\u4e24\u6b21\u3002\u73b0\u5728\u9700\u8981\u4f60\u5e2e\u52a9\u73ed\u957f\u5c0fC\u5feb\u901f\u627e\u5230\u90a3\u4e2a\u62ff\u4e86\u72ec\u7279\u6570\u5b57\u5361\u7247\u7684\u540c\u5b66\u624b\u4e0a\u7684\u6570\u5b57\u662f\u4ec0\u4e48\u3002 \u8981\u6c42\uff1a \u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\uff0c\u4f7f\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\uff0c\u5176\u4e2d n \u662f\u73ed\u7ea7\u7684\u4eba\u6570\u3002\u5c3d\u91cf\u51cf\u5c11\u989d\u5916\u7a7a\u95f4\u7684\u4f7f\u7528\uff0c\u4ee5\u4f53\u73b0\u4f60\u7684\u7b97\u6cd5\u4f18\u5316\u80fd\u529b\u3002\u6d4b\u8bd5\u6837\u4f8b\u6837\u4f8b1\uff1a \u8f93\u5165\uff1acards = [1, 1, 2, 2, 3, 3, 4, 5, 5]\u8f93\u51fa\uff1a4\u89e3\u91ca\uff1a\u62ff\u5230\u6570\u5b57 4 \u7684\u540c\u5b66\u662f\u552f\u4e00\u4e00\u4e2a\u6ca1\u6709\u914d\u5bf9\u7684\u3002 \u8f93\u5165\uff1acards = [0, 1, 0, 1, 2]\u8f93\u51fa\uff1a2\u89e3\u91ca\uff1a\u6570\u5b57 2 \u53ea\u51fa\u73b0\u4e00\u6b21\uff0c\u662f\u72ec\u7279\u7684\u5361\u7247\u3002 \u5728 Python&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-549","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=549"}],"version-history":[{"count":1,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions"}],"predecessor-version":[{"id":710,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions\/710"}],"wp:attachment":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}