Skip to content

Commit

Permalink
Design HashSet
Browse files Browse the repository at this point in the history
  • Loading branch information
C0nstellati0n committed May 30, 2023
1 parent 5f92d3b commit dd4cd7b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 4 deletions.
104 changes: 104 additions & 0 deletions Leetcode/Easy/Design HashSet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Design HashSet

[题目](https://leetcode.com/problems/design-hashset/description/)

又是简单的快乐。

```c#
public class MyHashSet {
List<int> hashSet;
public MyHashSet() {
hashSet=new();
}

public void Add(int key) {
if(!hashSet.Contains(key))
hashSet.Add(key);
}

public void Remove(int key) {
if(hashSet.Contains(key))
hashSet.Remove(key);
}

public bool Contains(int key) {
return hashSet.Contains(key);
}
}
```
```
Runtime
260 ms
Beats
28.95%
Memory
66.5 MB
Beats
53.29%
```
魔法位运算解法。
```c#
//https://leetcode.com/problems/design-hashset/solutions/769047/java-solution-using-bit-manipulation/
public class MyHashSet {
int[] num;
public MyHashSet() {
num = new int[31251];
}

// set the bit if that element is present
public void Add(int key) {
num[getIdx(key)]|=getMask(key);
}

//unset the bit if a key is not present
public void Remove(int key) {
num[getIdx(key)] &= (~getMask(key));
}

//check if bit corresponding to the key is set or not
public bool Contains(int key) {
return (num[getIdx(key)]&getMask(key))!=0;
}

// idx of num[] to which this key belongs
// for key = 37, it will give 1
private int getIdx(int key)
{
return (key/32);
}

// get mask representing the bit inside num[idx] that corresponds to given key.
// for key = 37, it will give 00000000000000000000000000100000
private int getMask(int key)
{
key%=32;
return (1<<key);
}
}
```
```
Runtime
203 ms
Beats
97.37%
Memory
62 MB
Beats
98.3%
```
偷懒继承写法(记下来纯粹是图一乐,肯定是不能这么写的)
```c#
//https://leetcode.com/problems/design-hashset/solutions/1621844/the-one-liner-solution-that-you-should-not-give/
public class MyHashSet:HashSet<int> {
}
```
```
Runtime
216 ms
Beats
89.47%
Memory
66.6 MB
Beats
53.29%
```
1 change: 1 addition & 0 deletions Leetcode/Leetcode.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- [Matrix Diagonal Sum](./Easy/Matrix%20Diagonal%20Sum.md).
- [Kth Largest Element in a Stream](./Easy/Kth%20Largest%20Element%20in%20a%20Stream.md). heap.
- [Design Parking System](./Easy/Design%20Parking%20System.md).
- [Design HashSet](./Easy/Design%20HashSet.md)

## Medium
- [Sum Root to Leaf Numbers](Medium/Sum%20Root%20to%20Leaf%20Numbers.md).Each root-to-leaf path in the tree represents a number. For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123. Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer. 返回全部根节点到子节点路径数字的和。
Expand Down
10 changes: 6 additions & 4 deletions 笔记/Tools/工具脚本.md
Original file line number Diff line number Diff line change
Expand Up @@ -1511,13 +1511,15 @@ new_img.save('changed_pixels_flag.png')

## [hashcat使用](https://xz.aliyun.com/t/4008)

下载:在[官网](https://hashcat.net/hashcat/)下载hashcat binaries,然后用`7z x`解压文件即可。linux运行`hashcat.bin`,windows运行`hashcat.exe`
下载:在[官网](https://hashcat.net/hashcat/)下载hashcat binaries,然后用`7z x`解压文件即可(7zip下载:`sudo apt-get install p7zip-full`)。linux运行`hashcat.bin`,windows运行`hashcat.exe`

运行时指定hash模式的-m选项可以在[这里](https://hashcat.net/wiki/doku.php?id=example_hashes)找到。例如爆破zip,先用[zip2john](https://hashes.com/en/johntheripper/zip2john)将zip转为hash,然后根据hash格式选择-m的参数。-a是爆破模式。
- 掩码爆破:`./hashcat.bin -a 3 -m 13600 'hash' mask`
- 字典爆破:`./hashcat.bin -a 0 -m 13600 'hash' dict`

- 小端下将bytes转为数字:`int.from_bytes(msg, 'little')`。类似地,大端填`'big'`

## Extracting GPS Coordinates from metadata
https://github.com/BYU-CSA/BYUCTF-2023/tree/main/lost
https://github.com/BYU-CSA/BYUCTF-2023/tree/main/lost

## 其他类型

- 小端下将bytes转为数字:`int.from_bytes(msg, 'little')`。类似地,大端填`'big'`

0 comments on commit dd4cd7b

Please sign in to comment.