C++
Java
Python
Python3
C
C#
JavaScript
TypeScript
PHP
Swift
Kotlin
Dart
Go
Ruby
Scala
Rust
Racket
Erlang
Elixir
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
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)