七叶笔记 » golang编程 » 面试工作中可能用得到的几个golang方法

面试工作中可能用得到的几个golang方法

 /****
 * 判断字符串里面是否有重复的字符串
 */func haveRepeat(str string) bool{
for {
if len(str) == 0{
return false
}
data := str[0:1]
if strings.Count(str,data)>1{
return true
}
str = str[1:]
}
}

/***
 * str需要反转的字符串
 * 反转字符串
 */func reverse(str string)string{
var count = len(str)
var restr = make([]string,count)
for index,_ := range str{
lastIndex := count-1-index
temp := str[lastIndex]
data := str[index]
if index >= lastIndex{
restr[index] = string(temp)
return strings.Join(restr,"")
}
restr[index] = string(temp)
restr[lastIndex] = string(data)
}
return ""
}

/***
 *  给出匹配符,根据匹配符判断字符串中的匹配符是否匹配
 *  {<>()}这种是匹配的
 *  遇到左边的匹配符就压栈;
 *  遇到右边的匹配符就出栈刚才的栈,找出对应的右边匹配符是否与找到的右边匹配符相等,如果不等就不匹配
 *
 *  str要匹配的字符串
 *  myMap要用来做匹配用的分隔符,如:map[string]string{"<":">","{":"}","(":")","[":"]"}
 */func matchDelimiter(str string,myMap map[string]string)bool{
var rightSilce =[]string{}
var leftSilce = []string{}
for key,val := range myMap {
leftSilce = append(leftSilce,key)
rightSilce = append(rightSilce,val)
}
var left = []string{}
leftval := ""
for _,item := range str{
if in_array(string(item),leftSilce){
left = append(left, string(item))
}
if in_array(string(item),rightSilce){
if len(left) == 0{
return false
}
leftval,left  = pop(left)
tmp := myMap[leftval]
if string(item) != tmp {
return false
}
}
}
if len(left) > 0{
return false
}
return true
}

/***
 * 数组出栈
 */func pop(arr []string)(string,[]string){
maxIndex := len(arr)-1
return arr[maxIndex],arr[0:maxIndex]
}

func in_array(data string,arr []string) bool{
for _,val := range arr{
if data == val{
return true
}
}
return false
}

/***
 * 两个字符串大小写是不同的字符,排序后是否一样
 */func compareSortString(str1 string,str2 string)bool{
len1 := len(str1)
len2 := len(str2)
if len1 != len2{
return false
}
for _,item := range str1 {
if strings.Count(str1,string(item)) != strings.Count(str2, string(item)) {
return false
}
}
return true
}

/**
 *  str被处理的字符串
 *  des将被替换成的字符
 *  delChar将被过滤掉的字符集合
 *  replace将被替换成des字符的字符集合
 *  示例:
 *     str := "abcd  12343 dfa"
 *   des := replaceSpace(str,"%20",[]string{},[]string{" "})
 *     des的结果:abcd%20%2012343%20dfa
 */func replaceSpace(str string,des string,delChar []string,replace []string)string{
var result = []string{}
var delCharStr = strings.Join(delChar,"")
var replaceStr = strings.Join(replace,"")
for _,item := range str{
if strings.Count(replaceStr,string(item))>0{
result = append(result,des)
continue
}
if strings.Count(delCharStr,string(item)) != 0{
continue
}
result = append(result,string(item))
}
return strings.Join(result,"")
}

/***
 * 给出指定的字符串,然后按k进行分段,不足k的不分段,最后倒叙展示
 * 例如:1->2->3->4->5->6->7->8按照3分段,则出来的结果是 1->2->5->4->3->8->7->6 
 * data   给定的原始字符串
 * k      指定的分段长度
 * result 用来存储分段排序完成后的结果
 *
 * 调用示例:
 *       str := "12345678"
 *       k := 3
 *       result := []string{}
 *       fmt.Println(getData(str,3,result))
 * 程序运行结果:
 *       12543876
 */  
func getData(data string,k int,result[]string)string{
len1 := len(data)
if len1 < k {
result = append(result,reverse(data))
return reverse(strings.Join(result,""))

}
result = append(result,data[len1-k:])
return getData(data[0:len1-k],k,result)
}  

相关文章