Skip to content

feat: add solutions to lc problems: No.170+ #2507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ twoSum.find(7); // 没有两个整数加起来等于 7 ,返回 false</pre>

```python
class TwoSum:

def __init__(self):
self.cnt = Counter()
self.cnt = defaultdict(int)

def add(self, number: int) -> None:
self.cnt[number] += 1

def find(self, value: int) -> bool:
for x, v in self.cnt.items():
y = value - x
if y in self.cnt:
if x != y or v > 1:
return True
if y in self.cnt and (x != y or v > 1):
return True
return False


Expand All @@ -104,10 +104,8 @@ class TwoSum {
for (var e : cnt.entrySet()) {
int x = e.getKey(), v = e.getValue();
int y = value - x;
if (cnt.containsKey(y)) {
if (x != y || v > 1) {
return true;
}
if (cnt.containsKey(y) && (x != y || v > 1)) {
return true;
}
}
return false;
Expand Down Expand Up @@ -135,10 +133,8 @@ public:
bool find(int value) {
for (auto& [x, v] : cnt) {
long y = (long) value - x;
if (cnt.count(y)) {
if (x != y || v > 1) {
return true;
}
if (cnt.contains(y) && (x != y || v > 1)) {
return true;
}
}
return false;
Expand Down Expand Up @@ -166,7 +162,7 @@ func Constructor() TwoSum {
}

func (this *TwoSum) Add(number int) {
this.cnt[number]++
this.cnt[number] += 1
}

func (this *TwoSum) Find(value int) bool {
Expand All @@ -187,6 +183,34 @@ func (this *TwoSum) Find(value int) bool {
*/
```

```ts
class TwoSum {
private cnt: Map<number, number> = new Map();
constructor() {}

add(number: number): void {
this.cnt.set(number, (this.cnt.get(number) || 0) + 1);
}

find(value: number): boolean {
for (const [x, v] of this.cnt) {
const y = value - x;
if (this.cnt.has(y) && (x !== y || v > 1)) {
return true;
}
}
return false;
}
}

/**
* Your TwoSum object will be instantiated and called as such:
* var obj = new TwoSum()
* obj.add(number)
* var param_2 = obj.find(value)
*/
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,37 @@ twoSum.find(7); // No two integers sum up to 7, return false

## Solutions

### Solution 1
### Solution 1: Hash Table

We use a hash table `cnt` to store the count of each number.

When the `add` method is called, we increment the count of the number `number`.

When the `find` method is called, we iterate over the hash table `cnt`. For each key `x`, we check if `value - x` is also a key in the hash table `cnt`. If it is, we check if `x` is equal to `value - x`. If they are not equal, it means we have found a pair of numbers whose sum is `value`, and we return `true`. If they are equal, we check if the count of `x` is greater than `1`. If it is, it means we have found a pair of numbers whose sum is `value`, and we return `true`. If it is less than or equal to `1`, it means we have not found a pair of numbers whose sum is `value`, and we continue to iterate over the hash table `cnt`. If we have not found a pair after the iteration, we return `false`.

Time complexity:

- The time complexity of the `add` method is $O(1)$.
- The time complexity of the `find` method is $O(n)$.

Space complexity is $O(n)$, where $n$ is the size of the hash table `cnt`.

<!-- tabs:start -->

```python
class TwoSum:

def __init__(self):
self.cnt = Counter()
self.cnt = defaultdict(int)

def add(self, number: int) -> None:
self.cnt[number] += 1

def find(self, value: int) -> bool:
for x, v in self.cnt.items():
y = value - x
if y in self.cnt:
if x != y or v > 1:
return True
if y in self.cnt and (x != y or v > 1):
return True
return False


Expand All @@ -88,10 +101,8 @@ class TwoSum {
for (var e : cnt.entrySet()) {
int x = e.getKey(), v = e.getValue();
int y = value - x;
if (cnt.containsKey(y)) {
if (x != y || v > 1) {
return true;
}
if (cnt.containsKey(y) && (x != y || v > 1)) {
return true;
}
}
return false;
Expand Down Expand Up @@ -119,10 +130,8 @@ public:
bool find(int value) {
for (auto& [x, v] : cnt) {
long y = (long) value - x;
if (cnt.count(y)) {
if (x != y || v > 1) {
return true;
}
if (cnt.contains(y) && (x != y || v > 1)) {
return true;
}
}
return false;
Expand Down Expand Up @@ -150,7 +159,7 @@ func Constructor() TwoSum {
}

func (this *TwoSum) Add(number int) {
this.cnt[number]++
this.cnt[number] += 1
}

func (this *TwoSum) Find(value int) bool {
Expand All @@ -171,6 +180,34 @@ func (this *TwoSum) Find(value int) bool {
*/
```

```ts
class TwoSum {
private cnt: Map<number, number> = new Map();
constructor() {}

add(number: number): void {
this.cnt.set(number, (this.cnt.get(number) || 0) + 1);
}

find(value: number): boolean {
for (const [x, v] of this.cnt) {
const y = value - x;
if (this.cnt.has(y) && (x !== y || v > 1)) {
return true;
}
}
return false;
}
}

/**
* Your TwoSum object will be instantiated and called as such:
* var obj = new TwoSum()
* obj.add(number)
* var param_2 = obj.find(value)
*/
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ class TwoSum {
bool find(int value) {
for (auto& [x, v] : cnt) {
long y = (long) value - x;
if (cnt.count(y)) {
if (x != y || v > 1) {
return true;
}
if (cnt.contains(y) && (x != y || v > 1)) {
return true;
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func Constructor() TwoSum {
}

func (this *TwoSum) Add(number int) {
this.cnt[number]++
this.cnt[number] += 1
}

func (this *TwoSum) Find(value int) bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ public boolean find(int value) {
for (var e : cnt.entrySet()) {
int x = e.getKey(), v = e.getValue();
int y = value - x;
if (cnt.containsKey(y)) {
if (x != y || v > 1) {
return true;
}
if (cnt.containsKey(y) && (x != y || v > 1)) {
return true;
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class TwoSum:

def __init__(self):
self.cnt = Counter()
self.cnt = defaultdict(int)

def add(self, number: int) -> None:
self.cnt[number] += 1

def find(self, value: int) -> bool:
for x, v in self.cnt.items():
y = value - x
if y in self.cnt:
if x != y or v > 1:
return True
if y in self.cnt and (x != y or v > 1):
return True
return False


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class TwoSum {
private cnt: Map<number, number> = new Map();
constructor() {}

add(number: number): void {
this.cnt.set(number, (this.cnt.get(number) || 0) + 1);
}

find(value: number): boolean {
for (const [x, v] of this.cnt) {
const y = value - x;
if (this.cnt.has(y) && (x !== y || v > 1)) {
return true;
}
}
return false;
}
}

/**
* Your TwoSum object will be instantiated and called as such:
* var obj = new TwoSum()
* obj.add(number)
* var param_2 = obj.find(value)
*/
59 changes: 38 additions & 21 deletions solution/0100-0199/0171.Excel Sheet Column Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,33 @@ AB -&gt; 28

## 解法

### 方法一
### 方法一:进制转换

Excel 表格中的列名称是一种 26 进制的表示方法。例如,"AB" 表示的列序号是 $1 \times 26 + 2 = 28$。

因此,我们可以遍历字符串 `columnTitle`,将每个字符转换为对应的数值,然后计算结果即可。

时间复杂度 $O(n)$,其中 $n$ 是字符串 `columnTitle` 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

```python
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
res = 0
for c in columnTitle:
res = res * 26 + (ord(c) - ord('A') + 1)
return res
ans = 0
for c in map(ord, columnTitle):
ans = ans * 26 + c - ord("A") + 1
return ans
```

```java
class Solution {
public int titleToNumber(String columnTitle) {
int res = 0;
for (char c : columnTitle.toCharArray()) {
res = res * 26 + (c - 'A' + 1);
int ans = 0;
for (int i = 0; i < columnTitle.length(); ++i) {
ans = ans * 26 + (columnTitle.charAt(i) - 'A' + 1);
}
return res;
return ans;
}
}
```
Expand All @@ -85,32 +91,43 @@ class Solution {
class Solution {
public:
int titleToNumber(string columnTitle) {
int res = 0;
for (char c : columnTitle) {
res = res * 26 + (c - 'A' + 1);
int ans = 0;
for (char& c : columnTitle) {
ans = ans * 26 + (c - 'A' + 1);
}
return res;
return ans;
}
};
```

```go
func titleToNumber(columnTitle string) int {
res := 0
func titleToNumber(columnTitle string) (ans int) {
for _, c := range columnTitle {
res = res*26 + int(c-'A'+1)
ans = ans*26 + int(c-'A'+1)
}
return res
return
}
```

```ts
function titleToNumber(columnTitle: string): number {
let res: number = 0;
for (let char of columnTitle) {
res = res * 26 + char.charCodeAt(0) - 64;
let ans: number = 0;
for (const c of columnTitle) {
ans = ans * 26 + (c.charCodeAt(0) - 'A'.charCodeAt(0) + 1);
}
return ans;
}
```

```cs
public class Solution {
public int TitleToNumber(string columnTitle) {
int ans = 0;
foreach (char c in columnTitle) {
ans = ans * 26 + c - 'A' + 1;
}
return ans;
}
return res;
}
```

Expand Down
Loading