{"id":476,"date":"2013-08-10T13:24:18","date_gmt":"2013-08-10T05:24:18","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=476"},"modified":"2013-08-10T13:24:18","modified_gmt":"2013-08-10T05:24:18","slug":"java%e8%ae%a1%e7%ae%97%e7%bb%84%e5%90%88","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=476","title":{"rendered":"[Java]\u8ba1\u7b97\u7ec4\u5408"},"content":{"rendered":"<p>\u57fa\u672c\u529f\u4e0d\u624e\u5b9e\uff0c\u5199\u4e86\u534a\u5929\u5199\u51fa\u6765\u3002<br \/>\n\u8f93\u5165\uff1a\u4e00\u4e2a\u957f\u5ea6n\u6570\u7ec4\uff0c\u6bd4\u5982{0,1,3,5}\uff0c\u4ee5\u53cam<br \/>\n\u8f93\u51fa\uff1a\u7ec4\u5408CNM<br \/>\n===============<\/p>\n<pre>\n\/**\n\t * \u8ba1\u7b97\u7ec4\u5408\u7684\n\t * @param input \u8f93\u5165\u7684\u5e8f\u53f7\uff0c\u5982{0,1,2,3,5}\n\t *\n\t * @param m \u62bd\u53d6\u6570\u91cf\n\t * @return \u8f93\u51fa\u4e3a\u7ec4\u5408\uff0c\u6bd4\u5982{{0,1},{0,2},{1,2}}\n\t *\/\n\tpublic static List<int[]> ComputeCombines(int[] input, int m){\n\t\tList<int[]> result = new ArrayList<int[]>();\n\t\tfor(int i=0; i<input.length; i++){\n\t\t\tint currNumber = input[i];\n\t\t\tif(m == 1){\n\t\t\t\t\/\/\u662f\u6700\u540e\u4e00\u4e2a\u4e86\n\t\t\t\tresult.add(new int[]{currNumber});\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tint[] rest = getRest(input, i);\n\t\t\tif(rest == null)\n\t\t\t\tcontinue;\n\t\t\tList<int[]> prev_result = ComputeCombines(rest, m-1); \/\/\u66f4\u7ec6\u5206\u7684\u7ed3\u679c\n\t\t\tfor(int[] results: prev_result){\n\t\t\t\tint[] temp = new int[results.length +1];\n\t\t\t\tSystem.arraycopy(results, 0, temp, 0, results.length);\n\t\t\t\ttemp[temp.length-1] = currNumber;\n\t\t\t\tresult.add(temp);\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\t\/**\n\t * \u4ece\u8f93\u5165\u5e8f\u5217\u4e2d\u53d6\u51fa\u4eceindex\u5f00\u59cb\uff08\u4e0d\u542b\uff09\u5230\u672b\u5c3e\u7684\u5143\u7d20\n\t * @param input\n\t * @param index\n\t * @return \u5982\u679c\u6ca1\u5f97\u641e\u4e86\u8fd4\u56denull\n\t *\/\n\tprivate static int[] getRest(int[] input, int index) {\n\t\tint result_size = input.length - index - 1;\n\t\tif(result_size <= 0)\n\t\t\treturn null;\n\t\tint[] rest_array = new int[result_size];\n\t\tSystem.arraycopy(input, index +1, rest_array, 0, result_size);\n\t\treturn rest_array;\n\t}\n\tpublic static void main(String[] args){\n\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\t\t\/\/\u6d4b\u8bd5\u7528\n\t\tint[] input = {0,1,2,3};\n\t\tList<int[]> result = ComputeCombines(input, 2);\n\t\tfor(int[] r : result){\n\t\t\tfor(int entry : r){\n\t\t\t\tSystem.out.print(entry);\n\t\t\t}\n\t\t\tSystem.out.print(\"n\");\n\t\t}\n\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\t}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u529f\u4e0d\u624e\u5b9e\uff0c\u5199\u4e86\u534a\u5929\u5199\u51fa\u6765\u3002 \u8f93\u5165\uff1a\u4e00\u4e2a\u957f\u5ea6n\u6570\u7ec4\uff0c\u6bd4\u5982{0,1,3,5}\uff0c\u4ee5\u53cam \u8f93\u51fa\uff1a\u7ec4\u5408CNM =============== \/** * \u8ba1\u7b97\u7ec4\u5408\u7684 * @param input \u8f93\u5165\u7684\u5e8f\u53f7\uff0c\u5982{0,1,2,3,5} * * @param m \u62bd\u53d6\u6570\u91cf * @return \u8f93\u51fa\u4e3a\u7ec4\u5408\uff0c\u6bd4\u5982{{0,1},{0,2},{1,2}} *\/ public static List ComputeCombines(int[] input, int m){ List result = new ArrayList(); for(int i=0; i<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[60],"class_list":["post-476","post","type-post","status-publish","format-standard","hentry","category-study","tag-java"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=476"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/476\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}