{"id":177094,"date":"2020-04-24T10:46:15","date_gmt":"2020-04-24T02:46:15","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=177094"},"modified":"2020-04-18T10:47:15","modified_gmt":"2020-04-18T02:47:15","slug":"comparison-between-python","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/comparison-between-python.html","title":{"rendered":"Python \u548c c++\/c\/java \u5bf9\u4e8e\u8d1f\u6570\u7684\u5b58\u50a8\u65b9\u5f0f\u5bf9\u6bd4"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nPython \u4e2d\u7684\u6574\u578b\u662f\u8865\u7801\u5f62\u5f0f\u5b58\u50a8\u7684
\nPython \u4e2d bin \u4e00\u4e2a\u8d1f\u6570\uff08\u5341\u8fdb\u5236\u8868\u793a\uff09\uff0c\u8f93\u51fa\u7684\u662f\u5b83\u7684\u539f\u7801\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u52a0\u4e0a\u4e2a\u8d1f\u53f7\uff0c\u65b9\u4fbf\u67e5\u770b
\nPython \u4e2d bin \u4e00\u4e2a\u8d1f\u6570\uff08\u5341\u516d\u8fdb\u5236\u8868\u793a\uff09\uff0c\u8f93\u51fa\u7684\u662f\u5bf9\u5e94\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

1\u3001\u5728 python \u91cc\u9762\uff0c\u8d1f\u6570\u7684\u5b58\u50a8\u65b9\u5f0f<\/strong>
\n\u5b9e\u4f8b<\/p>\n

a = bin(-3)\r\nprint(a)\r\n \r\na = bin(3)\r\nprint(a)\r\n \r\nb = bin(-3 & 0xffffffff)\r\nprint(b)\r\n \r\nc = bin(0xfffffffd)\r\nprint(c)\r\n \r\n\/\/\u8f93\u51fa\r\n\/\/-0b11\r\n\/\/0b11\r\n\/\/0b11111111111111111111111111111101\r\n\/\/0b11111111111111111111111111111101<\/pre>\n

\u4e5f\u5c31\u662f\u8bf4\uff1a<\/p>\n

    \n
  1. Python \u4e2d\u7684\u6574\u578b\u662f\u8865\u7801\u5f62\u5f0f\u5b58\u50a8\u7684<\/li>\n
  2. Python \u4e2d bin \u4e00\u4e2a\u8d1f\u6570\uff08\u5341\u8fdb\u5236\u8868\u793a\uff09\uff0c\u8f93\u51fa\u7684\u662f\u5b83\u7684\u539f\u7801\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u52a0\u4e0a\u4e2a\u8d1f\u53f7\uff0c\u65b9\u4fbf\u67e5\u770b<\/li>\n
  3. Python \u4e2d bin \u4e00\u4e2a\u8d1f\u6570\uff08\u5341\u516d\u8fdb\u5236\u8868\u793a\uff09\uff0c\u8f93\u51fa\u7684\u662f\u5bf9\u5e94\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u3002<\/li>\n<\/ol>\n

    \u6240\u4ee5\u4f60\u4e3a\u4e86\u83b7\u5f97\u8d1f\u6570\uff08\u5341\u8fdb\u5236\u8868\u793a\uff09\u7684\u8865\u7801\uff0c\u9700\u8981\u624b\u52a8\u5c06\u5176\u548c\u5341\u516d\u8fdb\u5236\u6570 0xfffffffd \u8fdb\u884c\u6309\u4f4d\u4e0e\u64cd\u4f5c\uff0c\u5f97\u5230\u7ed3\u679c\u4e5f\u662f\u4e2a\u5341\u516d\u8fdb\u5236\u6570\uff0c\u518d\u4ea4\u7ed9 bin() \u8fdb\u884c\u8f93\u51fa\uff0c\u5f97\u5230\u7684\u624d\u662f\u4f60\u60f3\u8981\u7684\u8865\u7801\u8868\u793a\u3002<\/p>\n

    2\u3001\u4f46\u662f\u5728c\/c++\/java\u91cc\u9762\u8d1f\u6570\u90fd\u662f\u4ee5\u8865\u7801\u7684\u5f62\u5f0f\u8fdb\u884c\u5b58\u50a8\u7684\uff0c\u300a\u8ba1\u7b97\u673a\u539f\u7406\u300b\u663e\u793a\uff0c\u8ba1\u7b97\u673a\u5185\u90e8\u91c7\u75282\u7684\u8865\u7801\uff08Two's Complement\uff09\u8868\u793a\u8d1f\u6570\u3002<\/strong><\/p>\n

    3\u3001\u8fd9\u5c31\u51fa\u73b0\u4e86\u5728Python\u91cc\u9762\u9700\u8981\u5c06\u8d1f\u6570\u548c0xffffffff\u8fdb\u884c\u4e0e\u64cd\u4f5c\uff0c\u6765\u53bb\u6389\u8d1f\u6570\u524d\u9762\u7684\u8d1f\u53f7\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u8d85\u8fc732\u4f4d\u7684\u4e1c\u897f\u5c31\u4e0d\u8fdb\u884c\u8003\u8651\u4e86\uff0c\u8fd9\u8fdb\u884c\u4e0e\u64cd\u4f5c\u7684\u5177\u4f53\u6b65\u9aa4\u662f\uff1a\u5982\u679c\u662f\u6b63\u6570\uff0c\u76f4\u63a5\u4e0e\uff1b\u5982\u679c\u662f\u8d1f\u6570\uff0c\u5148\u53bb\u6389\u6700\u524d\u9762\u7684\u8d1f\u53f7\uff0c\u518d\u53d6\u53cd\uff0c\u518d\u52a01\uff0c\u518d\u8fdb\u884c\u4e0e\u64cd\u4f5c\u3002\u4ece\u800c\u5f97\u5230\u8d1f\u6570\u7684\u8865\u7801\u3002<\/strong><\/p>\n

    \u56e0\u6b64\u5bf9\u4e8e\u8f93\u51fa\u7684a\u6211\u4eec\u4e5f\u8981\u8fdb\u884c\u622a\u65ad\uff0c\u4f46\u662f\u4e0d\u80fd\u7b80\u5355\u7c97\u66b4\u5730\u76f4\u63a5&0xffffffff, \u56e0\u4e3a\u8fd9\u6837\u505a\u7684\u8bdd-1\u52a01\u662f\u5bf9\u4e86\uff0c\u7ed3\u679c\u662f\u6b63\u6570\u7684\u4e5f\u6ca1\u95ee\u9898\uff0c\u4f46\u662f\u5982\u679c\u672c\u6765\u7ed3\u679c\u662f\u8d1f\u6570\u7684\uff0c\u8fd9\u6837\u5c31\u53c8\u51fa\u5947\u602a\u7ed3\u679c\u4e86\u3002\u6700\u540e\u771f\u6b63\u7684\u89e3\u51b3\u65b9\u6848\u5982\u4e0b\uff1a<\/p>\n

    \u5b9e\u4f8b<\/p>\n

    def getSum(a,b):\r\n     while b!=0:\r\n         ta = a\r\n         a = a^b\r\n         b = ((ta&b)<<1)&0xffffffff\r\n     hibit = (a&0x80000000)>>31\r\n     if hibit==1:\r\n         return -(((~a)+1)&0xffffffff)\r\n     else:\r\n         return a&0xffffffff<\/pre>\n

    \u5176\u539f\u7406\u662f\u5148\u901a\u8fc7\u7b2c32\u4f4d\u7b26\u53f7\u4f4d\u5224\u65ad\u662f\u5426\u8d1f\u6570\uff0c\u662f\u8d1f\u6570\u5219\u5148\u53bb\u53cd\u52a01\u518d\u622a\u65ad\uff0c\u6700\u540e\u52a0\u4e0a\u8d1f\u53f7\uff1b\u6b63\u6570\u5219\u76f4\u63a5\u622a\u65ad\u3002\u7ed3\u679c\u53f7\u79f0\u7b80\u6d01\uff0c\u5bb9\u6613\u7684Python\u7248\u672c\u53d8\u6210\u4e86\u8fd9\u6837\uff0c\u592a\u5947\u8469\u4e86\u3002<\/p>\n

    4\u3001\u6240\u4ee5\u53ef\u4ee5\u67e5\u770b\u81ea\u5df1\u7684\u5199\u7684\u5251\u6307Offer\u7684\uff1a\u4e8c\u8fdb\u5236\u4e2d1\u7684\u4e2a\u6570\u7684\u6c42\u89e3\u3002\u5bf9\u4e8ec++\u7a0b\u5e8f\u548cpython\u7a0b\u5e8f\u7684\u533a\u522b\uff08\u8d1f\u6570\u8865\u7801\u7684\u533a\u522b\uff09\u3002<\/strong><\/p>\n

    \u800c\u4e14\u5728\u8fd9\u9053\u9898\u76ee\u91cc\u9762\uff0c\u8fd8\u8981\u6ce8\u610f\u548c1\u76f8\u51cf\u8fdb\u884c\u4e0e\u64cd\u4f5c\u7684\u8ba1\u7b97\u65b9\u5f0f\u6c42\u89e3\u4e2a\u6570<\/p>\n

    5\u3001\u6c42\u89e3\u4e8c\u8fdb\u5236\u4e2d1\u7684\u4e2a\u6570\uff0c\u7528python\u5199\uff0c\u5c31\u662f\u8fd9\u6837\u7684<\/strong><\/p>\n

    \u5b9e\u4f8b<\/p>\n

    class Solution:\r\n    def NumberOf1(self, n):\r\n        # write code here\r\n        if n<0:\r\n            n=n&0xffffffff #\u8fd9\u4e2a\u662fpython\u91cc\u9762\u7684\uff0cpython\u548c\u522b\u7684\u8bed\u8a00\u5b58\u50a8\u8d1f\u6570\u7684\u683c\u5f0f\u6709\u70b9\u533a\u522b\r\n        temp=0x00000001\r\n        count=0\r\n        for i in range(64):\r\n            if n&temp:\r\n                count=count+1\r\n            temp=temp<<1\r\n        return count<\/pre>\n

    6\u3001\uff08\u53e6\u4e00\u4e2a\u9898\uff0c\u4f46\u662f\u4e5f\u662f\u6309\u4f4d\u64cd\u4f5c\uff09\u4e8c\u8fdb\u5236\uff0864\u4f4d\uff09\u4e2d\u6709\u4e14\u53ea\u67091\u4e2a1\uff08\u60f3\u8981\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u4f4e\u7684\u5173\u952e\uff09\uff0c\u6c42\u89e3\u8fd9\u4e2a\u6570\u5b57\u7684\u7684\u7b2c\u51e0\u4f4d\u662f\u90a3\u4e2a1\u3002\u6bd4\u5982\u8f93\u51658\uff0c\u8f93\u51fa4\u3002<\/strong><\/p>\n

    \u65b9\u6cd51\uff1aO(n)\u7684\u65f6\u95f4\u590d\u6742\u5ea6<\/p>\n

    \u5b9e\u4f8b<\/p>\n

    def search_1(input_n):\r\n    if input_n<0:\r\n        input_n=input_n&0xffffffff\r\n    temp=0x00000001\r\n    for i in range(64):\r\n        if input_n&temp:\r\n            return i+1\r\n        temp=temp<<1\r\n    return 0<\/pre>\n

    \u65b9\u6cd52\uff1aO(logn)\uff0c\u4e3b\u8981\u662f\u4f7f\u7528\u4e8c\u5206\u6cd5\u6c42\u89e3\uff0c\u4f46\u662f\u5173\u952e\u7684\u4e00\u70b9\u662f\u9700\u8981\u5224\u65ad\u4ed6\u7684\u503c\u7684\u5927\u5c0f\u3002\u5176\u5b9e\u4e5f\u53ef\u4ee5\u4f7f\u7528math.log(input_n,2)\u8fdb\u884c\u6c42\u89e3\uff08\u4f46\u662f\u8fd9\u4e2a\u5e93\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u4e0d\u592a\u6e05\u695a\u4e86\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"

    1\u3001\u5728 python \u91cc\u9762\uff0c\u8d1f\u6570\u7684\u5b58\u50a8\u65b9\u5f0f \u5b9e\u4f8b a = bin(-3) print(a) a = bin(3 […]<\/p>\n","protected":false},"author":1470,"featured_media":177096,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-177094","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/177094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=177094"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/177094\/revisions"}],"predecessor-version":[{"id":177100,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/177094\/revisions\/177100"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/177096"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=177094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=177094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=177094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}