Go and Swift Implementation of Merging Strings Alternately
func\u0020mergeAlternately(word1,\u0020word2\u0020string)\u0020string\u0020{\n\t\tn,\u0020m\u0020:=\u0020len(word1),\u0020len(word2)\n\t\tans\u0020:=\u0020make([]byte,\u00200,\u0020n+m)\n\t\tfor\u0020i\u0020:=\u00200;\u0020i\u0020<\u0020n\u0020||\u0020i\u0020<\u0020m;\u0020i++\u0020{\n\t\t if\u0020i\u0020<\u0020n\u0020{\n\t\t ans\u0020=\u0020append(ans,\u0020word1[i])\n\t\t }\n\t\t if\u0020i\u0020<\u0020m\u0020{\n\t\t ans\u0020=\u0020append(ans,\u0020word2[i])\n\t\t }\n\t\t}\n\t\treturn\u0020string(ans)\n}//转换为Swift内容:func\u0020mergeAlternately(word1:\u0020String,\u0020word2:\u0020String)\u0020->\u0020String\u0020{\n\t\tlet\u0020n\u0020=\u0020word1.count\n\t\tlet\u0020m\u0020=\u0020word2.count\n\t\tvar\u0020ans\u0020=\u0020""\n\t\tlet\u0020minLength\u0020=\u0020min(n,\u0020m)\n\t\tlet\u0020word1\u0020=\u0020Array(word1)\n\t\tlet\u0020word2\u0020=\u0020Array(word2)\n\t\t\n\t\tfor\u0020i\u0020in\u00200..<minLength\u0020{\n\t\t ans.append(word1[i])\n\t\t ans.append(word2[i])\n\t\t}\n\t\t\n\t\tif\u0020n\u0020>\u0020m\u0020{\n\t\t ans\u0020+=\u0020String(word1[minLength...])\n\t\t}else\u0020if\u0020n\u0020<\u0020m\u0020{\n\t\t ans\u0020+=\u0020String(word2[minLength...])\n\t\t}\n\t\t\n\t\treturn\u0020ans\n}
原文地址: https://www.cveoy.top/t/topic/qbvu 著作权归作者所有。请勿转载和采集!