《go语言圣经》练习3.12

xiayanshangda · · 3744 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

# 题目 **编写一个函数,判断两个字符串是否是相互打乱的,也就是它们有着相同的字符,但是对应不同的顺序** # 代码 ```go package main import ( "fmt" "os" ) func disorder(s1, s2 string) bool { //判断两个字符串是否是相互打乱的 var count1, count2 int //count1 记录两个字符串中是否都有某个字符, //count2 记录若都有这个字符,那么位置是否相等 if len(s1) != len(s2) { //若两个字符串不相等,那么肯定不是相互打乱的 return false } for index1, value1 := range s1 { for index2, value2 := range s2 { if value1 == value2 { count1++ //对于s1中的某个字符,若s2有相等字符,则count1加1 if index1 == index2 { count2++ //若不仅都有这个字符,而且位置也相等,则count2加1 } break //既然在s2中找到了相等的字符,就进入s1下一个字符查找 } if index2 == len(s2)-1 { //s2遍历到最后一个元素时,还找不到与s1相等的字符,则 return false //肯定不是相互打乱的 } } } if count1 == count2 { //到这一步,说明s1和s2的字符相等,若位置相等的次数与字符相等 return false //的次数一样,说明s1和s2顺序一样,不是相互打乱的 }else{ return true } } func main() { a := disorder(os.Args[1], os.Args[2]) if a == true { fmt.Printf("%s 和 %s 是相互打乱的", os.Args[1], os.Args[2]) }else { fmt.Printf("%s 和 %s 不是相互打乱的", os.Args[1], os.Args[2]) } } ``` # 测试 ![base64图片](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfsAAABqCAYAAAC7xviAAAASR0lEQVR4nO2d0XEcOQ6GJZUDcxCbwEbgBLxyDI7ACTgIJXG7++7z+/oS0GmqliqKAgiAAEh29/9VddmeJkEABAmS3TO+//vnr+fPnx/vvn/7egcAAACA8/GwWgEAAAAA5IJkDwAAAJwcJHsAAADg5CDZAwAAACcHyR4AAAA4OS/J/n61DgAAAABIBDt7AAAA4OR8aD94enp68++PHz9OUmUvih+uav+RqWP4SP2XOfZm+MSrf5T9NzlUXUn+rDF/xbklMv6yY5mSf9Q5pebNzr4OQspICUvZq9Lz0Wr/rW4/Ck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dZlnqjtQHAIARXpN9xs77SETaX5Lljrs0rU69hZRnV9zW6y0oen1g7Z/2UcWu7BYvAIBzkPaCXsbuVkoWO7Fjws/UhUqg1PF9BJqdu6VOJjv1PwDgurwm+zo5tf+ud0PtzqhMZG39+p5GvoSl3Ahe+zXttmUomZyN3sXTSNKxlJfKzkh4SKwAAEDDvqDX/lvz/NY64UcmE6qc93mqxX7NZysTUVTbGe9KHA3PY4CdHyEAAM7LYb5nr2Gn5GrFejLS3p9BneAzEn7EM/re44SevhZbuAVle+oj1W3r13pceTEFAEjgzx+/nn/7/dPzy18Pdb1Mhst1OMul8SVX5va5dGnkUeUy+rgnc5eY2kUPXLhwnee6vyX7x8fHu+/fvt4BAAAA4Hw8rFYAAAAAALkg2QMAAAAnB8keAAAAODkf2v/htn07+KpvBHu/1w7WsfIbCx4yx94Mn3j1j7Kf+yaDJB9jHpyZNzv7OtgtPxbT1gc8EV8vy2J1+1F4ksRK6nFH4dEvO4Fp5g5N7I/OPVIbGvmedgHYnf8DfXaU6H0BAAEAAAAASUVORK5CYII=)

有疑问加站长微信联系(非本文作者))

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

3744 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传