feat(dart): add forLoopRecur and update HashMapOpenAddressing (#802)

* feat(dart): add forLoopRecur

* feat(dart): update HashMapOpenAddressing
pull/803/head
liuyuxin 1 year ago committed by GitHub
parent e567d08348
commit e3366363b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,6 +14,25 @@ int recur(int n) {
return n + res; return n + res;
} }
/* 使用迭代模拟递归 */
int forLoopRecur(int n) {
// 使
List<int> stack = [];
int res = 0;
//
for (int i = n; i > 0; i--) {
//
stack.add(i);
}
//
while (!stack.isEmpty) {
//
res += stack.removeLast();
}
// res = 1+2+3+...+n
return res;
}
/* 尾递归 */ /* 尾递归 */
int tailRecur(int n, int res) { int tailRecur(int n, int res) {
// //
@ -43,6 +62,9 @@ void main() {
res = tailRecur(n, 0); res = tailRecur(n, 0);
print("\n尾递归函数的求和结果 res = $res"); print("\n尾递归函数的求和结果 res = $res");
res = forLoopRecur(n);
print("\n使用迭代模拟递归求和结果 res = $res");
res = fib(n); res = fib(n);
print("\n斐波那契数列的第 $n 项为 $res"); print("\n斐波那契数列的第 $n 项为 $res");
} }

@ -4,12 +4,7 @@
* Author: liuyuxin (gvenusleo@gmail.com) * Author: liuyuxin (gvenusleo@gmail.com)
*/ */
/* 键值对 */ import 'array_hash_map.dart';
class Pair {
int key;
String val;
Pair(this.key, this.val);
}
/* 链式地址哈希表 */ /* 链式地址哈希表 */
class HashMapChaining { class HashMapChaining {

@ -4,30 +4,21 @@
* Author: liuyuxin (gvenusleo@gmail.com) * Author: liuyuxin (gvenusleo@gmail.com)
*/ */
/* 键值对 */ import 'array_hash_map.dart';
class Pair {
int key;
String val;
Pair(this.key, this.val);
}
/* 开放寻址哈希表 */ /* 开放寻址哈希表 */
class HashMapOpenAddressing { class HashMapOpenAddressing {
late int _size; // late int _size; //
late int _capacity; // int _capacity = 4; //
late double _loadThres; // double _loadThres = 2.0 / 3.0; //
late int _extendRatio; // int _extendRatio = 2; //
late List<Pair?> _buckets; // late List<Pair?> _buckets; //
late Pair _removed; // Pair _TOMBSTONE = Pair(-1, "-1"); //
/* 构造方法 */ /* 构造方法 */
HashMapOpenAddressing() { HashMapOpenAddressing() {
_size = 0; _size = 0;
_capacity = 4;
_loadThres = 2.0 / 3.0;
_extendRatio = 2;
_buckets = List.generate(_capacity, (index) => null); _buckets = List.generate(_capacity, (index) => null);
_removed = Pair(-1, "-1");
} }
/* 哈希函数 */ /* 哈希函数 */
@ -40,19 +31,42 @@ class HashMapOpenAddressing {
return _size / _capacity; return _size / _capacity;
} }
/* 查询操作 */ /* 搜索 key 对应的桶索引 */
String? get(int key) { int findBucket(int key) {
int index = hashFunc(key); int index = hashFunc(key);
// 线 index int firstTombstone = -1;
for (int i = 0; i < _capacity; i++) { // 线
while (_buckets[index] != null) {
// key
if (_buckets[index]!.key == key) {
//
if (firstTombstone != -1) {
_buckets[firstTombstone] = _buckets[index];
_buckets[index] = _TOMBSTONE;
return firstTombstone; //
}
return index; //
}
//
if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) {
firstTombstone = index;
}
// //
int j = (index + i) % _capacity; index = (index + 1) % _capacity;
// key null }
if (_buckets[j] == null) return null; // key
// key val return firstTombstone == -1 ? index : firstTombstone;
if (_buckets[j]!.key == key && _buckets[j] != _removed) }
return _buckets[j]!.val;
/* 查询操作 */
String? get(int key) {
// key
int index = findBucket(key);
// val
if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {
return _buckets[index]!.val;
} }
// null
return null; return null;
} }
@ -62,42 +76,26 @@ class HashMapOpenAddressing {
if (loadFactor() > _loadThres) { if (loadFactor() > _loadThres) {
extend(); extend();
} }
int index = hashFunc(key); // key
// 线 index int index = findBucket(key);
for (int i = 0; i < _capacity; i++) { // val
// if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {
int j = (index + i) % _capacity; _buckets[index]!.val = val;
// return;
if (_buckets[j] == null || _buckets[j] == _removed) {
_buckets[j] = new Pair(key, val);
_size += 1;
return;
}
// key val
if (_buckets[j]!.key == key) {
_buckets[j]!.val = val;
return;
}
} }
//
_buckets[index] = new Pair(key, val);
_size++;
} }
/* 删除操作 */ /* 删除操作 */
void remove(int key) { void remove(int key) {
int index = hashFunc(key); // key
// 线 index int index = findBucket(key);
for (int i = 0; i < _capacity; i++) { //
// if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {
int j = (index + i) % _capacity; _buckets[index] = _TOMBSTONE;
// key _size--;
if (_buckets[j] == null) {
return;
}
// key
if (_buckets[j]!.key == key) {
_buckets[j] = _removed;
_size -= 1;
return;
}
} }
} }
@ -111,7 +109,7 @@ class HashMapOpenAddressing {
_size = 0; _size = 0;
// //
for (Pair? pair in bucketsTmp) { for (Pair? pair in bucketsTmp) {
if (pair != null && pair != _removed) { if (pair != null && pair != _TOMBSTONE) {
put(pair.key, pair.val); put(pair.key, pair.val);
} }
} }
@ -120,10 +118,12 @@ class HashMapOpenAddressing {
/* 打印哈希表 */ /* 打印哈希表 */
void printHashMap() { void printHashMap() {
for (Pair? pair in _buckets) { for (Pair? pair in _buckets) {
if (pair != null) { if (pair == null) {
print("${pair.key} -> ${pair.val}"); print("null");
} else if (pair == _TOMBSTONE) {
print("TOMBSTONE");
} else { } else {
print(null); print("${pair.key} -> ${pair.val}");
} }
} }
} }

Loading…
Cancel
Save