go 切片的 插入、删除

package main

import (
    "fmt"
)

func InsertSpringSliceCopy(slice, insertion []string, index int) []string {

    result :=make([]string, len(slice)+len(insertion))
    at := copy(result, slice[:index])
    at += copy(result[at:], insertion)
    copy(result[at:], slice[index:])
    return result
}

func InsertSpringSlice(slice, insertion []string, index int) []string  {
    return append(slice[:index], append(insertion, slice[index:]...)...)

}

func RemoveSpringSliceCopy(slice []string, start,end int) []string {
    result := make([]string, len(slice)-(end-start))
    at :=copy(result, slice[:start])
    copy(result[at:], slice[end:])
    return result
}

func RemoveSpringSlice(slice []string, start, end int) []string {
    return append(slice[:start], slice[end:]...)
}

func main() {
    s := []string{"C", "H", "A", "O"}
    x := InsertSpringSliceCopy(s, []string{"a", "b", "c"}, 0)
    fmt.Println(x)

    w := []string {"C", "H", "E", "N"}
    ww :=InsertSpringSlice(w, []string{"c", "h", "a", "o"}, len(w))
    fmt.Println(ww)

    d := []string {"a", "b", "c", "d", "e", "f"}
    dd := RemoveSpringSliceCopy(d, 2, 4)
    fmt.Println(dd)

    d2 := []string {"A", "B", "C", "D", "E", "F", "G"}
    dd2 := RemoveSpringSlice(d2, 2, 4)
    fmt.Println(dd2)
}
时间: 2024-08-01 15:05:20

go 切片的 插入、删除的相关文章

双向循环链表 初始化 插入 删除

#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR -1 #define TRUE 1 #define FALSE -1 #define NULL 0 #define OVERFLOW -2 #define ElemType int #define Status int typedef int ElemType typedef int Status #define LEN sizeof(DuLNode)

顺序表 初始化 插入 删除 查找 合并 交换 判断为空 求长度

#include <stdio.h> #include <stdlib.h> #define OK 1 #define TRUE 1 #define ERROR -1 #define FALSE -1 #define OVERFLOW -2 #define ElemType int #define Status int typedef int ElemType typedef int Status #define LEN sizeof(SqList) #define MLC (Li

静态链表 初始化 定位 Malloc Free 插入 删除

#include <stdio.h> #include <stdlib.h> #define OK 1 #define TRUE 1 #define ERROR -1 #define FALSE -1 #define OVERFLOW -2 #define ElemType int #define Status int typedef int ElemType typedef int Status #define MAX_SIZE 1000;//表最大空间 /* //线性表的基本操

单链表 初始化 创建 头插法 尾插法 插入 删除 查找 合并 长度

#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR -1 #define TRUE 1 #define FALSE -1 #define NULL 0 #define OVERFLOW -2 #define ElemType int #define Status int typedef int ElemType typedef int Status #define LEN sizeof(LNode) #

AVL树非递归插入删除思路

AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增加和删除可能需要通过一次或多次树旋转来重新平衡这个树.AVL树得名于它的发明者G.M. Adelson-Velsky和E.M. Landis,他们在1962年的论文<An algorithm for the organization of information>中发表了它. 节点的平衡因子是它的左子树的高度减去它的右子树的

JavaScript之jQuery-3 jQuery操作DOM(查询、样式操作、遍历节点、创建插入删除、替换、复制)

一.jQuery操作DOM - 查询 html操作 - html(): 读取或修改节点的HTML内容,类似于JavaScript中的innerHTML属性 文本操作 - text(): 读取或修改节点的文本内容,类似于JavaScript中的textContent属性 值操作 - val(): 读取或修改节点的value属性值,类似于 JavaScript 中的value值 属性操作 - attr(): 读取或者修改节点的属性 - removeAttr(): 删除节点的属性 二.jQuery操作

数据结构线性表插入删除的示例

昨天留的作业,数据结构书和c语言书真的不一样,需要自己加加补补,今天写的时候迷迷瞪瞪的,反正在我电脑上能用. 数据结构线性表插入删除的示例: 代码: #include<iostream> #include<cstdio> using namespace std; #define list_init_size 100 #define listincrement 10 typedef struct { int *elem; int length; int listsize; }sqli

剑指offer (5) 链表插入删除

我们在操作链表的时候,必须注意以下事项: 1. 链表指针为NULL的情况 2. 插入删除涉及到 链表第一个节点时,需要修改 链表的第一个节点: a. 因为 c语言都是传值的,如果需要修改一个变量,就必须通过 指向该变量的指针(即该变量的地址) 例如:例如 修改 int a,则输入参数必须是 int* a, 修改a 则是: *a = b; (b为int) 我们需要修改一个指针时,就必须通过 该指针的地址,也就是 指向该指针的指针,即二级指针 例如  修改 int* a, 则输入参数必须是 int*

代码错误:插入删除没有数据显示

插入删除没有数据显示,什么情况:   问题原因: 就是加上了下面这段代码导致的:把这段代码注释掉就解决了     override func numberOfSectionsInTableView(tableView: UITableView) -> Int { //        // #warning Potentially incomplete method implementation. //        // Return the number of sections. //    

leveldb源码分析--插入删除流程

由于网络上对leveldb的分析文章都比较丰富,一些基础概念和模型都介绍得比较多,所以本人就不再对这些概念以专门的篇幅进行介绍,本文主要以代码流程注释的方式. 首先我们从db的插入和删除开始以对整个体系有一个感性的认识,首先看插入: Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value) { WriteBatch batch; //leveldb中不管单个插入还是多个插入都是以Wri