go切片的Add与Del

func Add(s []int, index int, value int) []int {
	len := len(s)
	if 0 > index || len <= index {
		panic("error")
	}
	s = append(s, s[len - 1])
	temp := len - index
	for temp > 0 {
		if len - 2 >= 0 {
			s[len - 1] = s[len - 2]
		}
		temp--
		len--
	}
	s[index] = value
	return s
}

func Del(s []int, index int) []int {
	len := len(s)
	if 0 > index || index > len {
		panic("error")
	}

	temp := len - index - 1
	for temp > 0 {
		s[index] = s[index + 1]
		index++
		temp--
	}
	s1 := s[0:len - 1]
	return s1
}

  

posted @ 2021-08-07 12:26  末世  阅读(141)  评论(0编辑  收藏  举报