說明
你已經知道,集合表示一組互不相同的元素(不重復的元素),在字典中,存盤的是鍵,值]對,其中鍵名是用來查詢特定元素的,字典和集合很相似,集合以[值,值]的形式存盤元素,字典則是以[鍵,值]的形式來存盤元素,字典也稱作映射,
簡單圖解
可以看到堆疊的資料結構只有一個口,進出資料都走這個口,這也導致了它有后進先出(LIFO)的原則,有點像是一種思想或者是一種約定吧!!!
一個字典基本方法
- set(key,value) : 向字典中添加新元素,
- hasKey(key) : 如果某個鍵值存在于這個字典中,則回傳true,反之則回傳false,
- get(key) :通過鍵值查找特定的數值并回傳,
- keys():將字典所包含的所有鍵名以陣列形式回傳,
- values():將字典所包含的所有數值以陣列形式回傳,
- remove(key) : 通過使用鍵值來從字典中移除鍵值對應的資料值,
- keyValues:將字典中所有的[鍵,值]對回傳
- forEach:迭代字典中所有的鍵值對callBackFn有兩個引數(key,value),該方法可以在回呼回傳false的時候中止回呼,類似(every方法)
- clear() : 移除字典中的所有元素
- size() : 回傳字典的元素個數
- isEmpty: 字典是空的
class ValuePair<K, V> {
key: K;
value: V;
constructor(key: K, val: V) {
this.key = key;
this.value = https://www.cnblogs.com/wangzhaoyv/archive/2020/11/15/val;
}
toString() {
return `[#${this.key}: ${this.value}]`
}
}
function defaultToString(key: any) {
if (key === null) {
return"NULL";
} else if (key === undefined) {
return "UNDEFINED"
} else if (typeof key === 'string' || key instanceof String) {
return key + '';
} else {
return key.toString();
}
}
export default class Dictionary<K, V> {
table: any;
count: number;
toStrFn: any;
constructor(toStrFn: any = defaultToString) {
this.table = {};
this.count = 0;
this.toStrFn = toStrFn;
}
set(key: K, value: V) {
if (key == null || value =https://www.cnblogs.com/wangzhaoyv/archive/2020/11/15/= null) {
return false;
}
let newKey = this.toStrFn(key)
//判斷是否存在,存在就是覆寫,并不需要+1
if (!this.hasKey(key)) {
this.count++;
}
this.table[newKey] = new ValuePair(key, value);
return true;
}
hasKey(key: K) {
let newKey = this.toStrFn(key)
//這里是一個物件,所以不要擔心內部是0的特殊情況
return !!this.table[newKey];
}
get(key: K) {
let newKey = this.toStrFn(key)
if (this.hasKey(key)) {
return this.table[newKey].value;
}
return undefined;
}
keys() {
return this.keyValues().map(item => item.key);
}
values() {
return this.keyValues().map(item => item.value);
}
keyValues(): Array> {
let resultArray = []
for (let tableKey in this.table) {
resultArray.push(this.table[tableKey]);
}
return resultArray;
}
forEach(callBackFn: (key: K, value: V) => any) {
for (let itemsKey in this.table) {
let element: ValuePair = this.table[itemsKey];
if(callBackFn(element.key, element.value) === false){
break;
};
}
}
remove(key: any): Boolean {
if (!this.hasKey(key)) {
return false;
}
let newKey = this.toStrFn(key);
delete this.table[newKey];
this.count--;
return true;
}
isEmpty() {
return this.count === 0;
}
clear() {
this.count = 0;
this.table = {};
}
size() {
return this.count;
}
toString() {
if (this.isEmpty()) {
return'';
}
const valuePairs = this.keyValues();
let objString = `${valuePairs[0].toString()}`;
for (let i = 1; i < valuePairs.length; i++) {
objString = `${objString},${valuePairs[i].toString()}`;
}
return objString;
}
}
書中代碼
function defaultToString(item: any): string {
if (item === null) {
return 'NULL';
} else if (item === undefined) {
return 'UNDEFINED';
} else if (typeof item === 'string' || item instanceof String) {
return `${item}`;
}
return item.toString();
}
class ValuePair<K, V> {
constructor(public key: K, public value: V) {}
toString() {
return `[#${this.key}: ${this.value}]`;
}
}
export default class Dictionary<K, V> {
private table: { [key: string]: ValuePair<K, V> };
constructor(private toStrFn: (key: K) => string = defaultToString) {
this.table = {};
}
set(key: K, value: V) {
if (key != null && value != null) {
const tableKey = this.toStrFn(key);
this.table[tableKey] = new ValuePair(key, value);
return true;
}
return false;
}
get(key: K): V {
const valuePair = this.table[this.toStrFn(key)];
return valuePair == null ? undefined : valuePair.value;
}
hasKey(key: K) {
return this.table[this.toStrFn(key)] != null;
}
remove(key: K) {
if (this.hasKey(key)) {
delete this.table[this.toStrFn(key)];
return true;
}
return false;
}
values(): V[] {
return this.keyValues().map(
(valuePair: ValuePair<K, V>) => valuePair.value
);
}
keys(): K[] {
return this.keyValues().map(
(valuePair: ValuePair<K, V>) => valuePair.key
);
}
keyValues(): ValuePair<K, V>[] {
return Object.values(this.table);
}
forEach(callbackFn: (key: K, value: V) => any) {
const valuePairs = this.keyValues();
for (let i = 0; i < valuePairs.length; i++) {
const result = callbackFn(valuePairs[i].key, valuePairs[i].value);
if (result === false) {
break;
}
}
}
isEmpty() {
return this.size() === 0;
}
size() {
return Object.keys(this.table).length;
}
clear() {
this.table = {};
}
toString() {
if (this.isEmpty()) {
return '';
}
const valuePairs = this.keyValues();
let objString = `${valuePairs[0].toString()}`;
for (let i = 1; i < valuePairs.length; i++) {
objString = `${objString},${valuePairs[i].toString()}`;
}
return objString;
}
}
leetcode對應訓練
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/217542.html
標籤:其他
上一篇:第七章 集合
下一篇:第八章 散串列
