列表

详情


2408. 设计 SQL

给定 n 个表,用两个数组 namescolumns 表示,其中 names[i] 是第 i 个表的名称,columns[i] 是第 i 个表的列数。

您能够执行以下 操作:

实现 SQL 类:

 

示例 1:

输入
["SQL", "insertRow", "selectCell", "insertRow", "deleteRow", "selectCell"]
[[["one", "two", "three"], [2, 3, 1]], ["two", ["first", "second", "third"]], ["two", 1, 3], ["two", ["fourth", "fifth", "sixth"]], ["two", 1], ["two", 2, 2]]
输出
[null, null, "third", null, null, "fifth"]

解释
SQL sql = new SQL(["one", "two", "three"], [2, 3, 1]); // 创建三个表。
sql.insertRow("two", ["first", "second", "third"]); // 向表 "2" 添加一行。id 是 1。
sql.selectCell("two", 1, 3); // 返回 "third",查找表 "two" 中 id 为 1 的行中第三列的值。
sql.insertRow("two", ["fourth", "fifth", "sixth"]); // 将另一行添加到表 "2" 中。它的 id 是 2。
sql.deleteRow("two", 1); // 删除表 "two" 的第一行。注意,第二行仍然有 id 2。
sql.selectCell("two", 2, 2); // 返回 "fifth",查找表 "two" 中 id 为 2 的行中第二列的值。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class SQL { public: SQL(vector<string>& names, vector<int>& columns) { } void insertRow(string name, vector<string> row) { } void deleteRow(string name, int rowId) { } string selectCell(string name, int rowId, int columnId) { } }; /** * Your SQL object will be instantiated and called as such: * SQL* obj = new SQL(names, columns); * obj->insertRow(name,row); * obj->deleteRow(name,rowId); * string param_3 = obj->selectCell(name,rowId,columnId); */

java 解法, 执行用时: 100 ms, 内存消耗: 60 MB, 提交时间: 2023-10-21 20:32:53

class SQL {

    HashMap<String, List<List<String>>> map = new HashMap<>();
    public SQL(List<String> names, List<Integer> columns) {
        for (String s : names) {
            map.put(s, new ArrayList<>());
        }
    }
    
    public void insertRow(String name, List<String> row) {
        map.get(name).add(row);
    }
    
    public void deleteRow(String name, int rowId) {
        map.get(name).get(rowId - 1).clear();
    }
    
    public String selectCell(String name, int rowId, int columnId) {
        return map.get(name).get(rowId - 1).get(columnId - 1);
    }
}

/**
 * Your SQL object will be instantiated and called as such:
 * SQL obj = new SQL(names, columns);
 * obj.insertRow(name,row);
 * obj.deleteRow(name,rowId);
 * String param_3 = obj.selectCell(name,rowId,columnId);
 */

golang 解法, 执行用时: 180 ms, 内存消耗: 17.1 MB, 提交时间: 2023-10-21 20:32:38

type SQL struct {
	tables map[string][][]string
}

func Constructor(names []string, columns []int) SQL {
	return SQL{tables: map[string][][]string{}}
}

func (s *SQL) InsertRow(name string, row []string) {
	s.tables[name] = append(s.tables[name], row)
}

func (s *SQL) DeleteRow(name string, rowId int) {
}

func (s *SQL) SelectCell(name string, rowId int, columnId int) string {
	return s.tables[name][rowId-1][columnId-1]
}


/**
 * Your SQL object will be instantiated and called as such:
 * obj := Constructor(names, columns);
 * obj.InsertRow(name,row);
 * obj.DeleteRow(name,rowId);
 * param_3 := obj.SelectCell(name,rowId,columnId);
 */

cpp 解法, 执行用时: 228 ms, 内存消耗: 137.3 MB, 提交时间: 2023-10-21 20:32:14

class SQL {
public:
    struct Table {
        int rows = 0;
        map<int, vector<string>> records;
    };
    unordered_map<string, Table> tables;

    SQL(vector<string> &names, vector<int> &columns) {
        for (auto &name: names) {
            Table t;
            tables[name] = t;
        }
    }

    void insertRow(string name, vector<string> row) {
        Table &t = tables[name];
        t.rows++;
        t.records[t.rows] = move(row);
    }

    void deleteRow(string name, int rowId) {
        tables[name].records.erase(rowId);
    }

    string selectCell(string name, int rowId, int columnId) {
        return tables[name].records[rowId][columnId - 1];
    }
};

/**
 * Your SQL object will be instantiated and called as such:
 * SQL* obj = new SQL(names, columns);
 * obj->insertRow(name,row);
 * obj->deleteRow(name,rowId);
 * string param_3 = obj->selectCell(name,rowId,columnId);
 */

python3 解法, 执行用时: 140 ms, 内存消耗: 31.4 MB, 提交时间: 2023-10-21 20:31:44

class SQL:

    def __init__(self, names: List[str], columns: List[int]):
        self.data=collections.defaultdict(list)
        for name in names:
            self.data[name]=[]


    def insertRow(self, name: str, row: List[str]) -> None:
        self.data[name].append(row)


    def deleteRow(self, name: str, rowId: int) -> None:
        self.data[name][rowId-1]=[]


    def selectCell(self, name: str, rowId: int, columnId: int) -> str:
        return self.data[name][rowId-1][columnId-1]


# Your SQL object will be instantiated and called as such:
# obj = SQL(names, columns)
# obj.insertRow(name,row)
# obj.deleteRow(name,rowId)
# param_3 = obj.selectCell(name,rowId,columnId)

上一题