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;
}
/* 使用迭代模拟递归 */
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) {
//
@ -43,6 +62,9 @@ void main() {
res = tailRecur(n, 0);
print("\n尾递归函数的求和结果 res = $res");
res = forLoopRecur(n);
print("\n使用迭代模拟递归求和结果 res = $res");
res = fib(n);
print("\n斐波那契数列的第 $n 项为 $res");
}

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

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

Loading…
Cancel
Save