Skip to content

Commit 3a35bba

Browse files
committed
feat: add solutions to lc problems: No.1265,1570
1 parent e8e3bb9 commit 3a35bba

File tree

10 files changed

+400
-8
lines changed

10 files changed

+400
-8
lines changed

solution/1200-1299/1265.Print Immutable Linked List in Reverse/README.md

+79-1
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,93 @@
7979
<!-- 这里可写当前语言的特殊实现逻辑 -->
8080

8181
```python
82-
82+
# """
83+
# This is the ImmutableListNode's API interface.
84+
# You should not implement it, or speculate about its implementation.
85+
# """
86+
# class ImmutableListNode:
87+
# def printValue(self) -> None: # print the value of this node.
88+
# def getNext(self) -> 'ImmutableListNode': # return the next node.
89+
90+
class Solution:
91+
def printLinkedListInReverse(self, head: 'ImmutableListNode') -> None:
92+
if head:
93+
self.printLinkedListInReverse(head.getNext())
94+
head.printValue()
8395
```
8496

8597
### **Java**
8698

8799
<!-- 这里可写当前语言的特殊实现逻辑 -->
88100

89101
```java
102+
/**
103+
* // This is the ImmutableListNode's API interface.
104+
* // You should not implement it, or speculate about its implementation.
105+
* interface ImmutableListNode {
106+
* public void printValue(); // print the value of this node.
107+
* public ImmutableListNode getNext(); // return the next node.
108+
* };
109+
*/
110+
111+
class Solution {
112+
public void printLinkedListInReverse(ImmutableListNode head) {
113+
if (head != null) {
114+
printLinkedListInReverse(head.getNext());
115+
head.printValue();
116+
}
117+
}
118+
}
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
/**
125+
* // This is the ImmutableListNode's API interface.
126+
* // You should not implement it, or speculate about its implementation.
127+
* class ImmutableListNode {
128+
* public:
129+
* void printValue(); // print the value of the node.
130+
* ImmutableListNode* getNext(); // return the next node.
131+
* };
132+
*/
133+
134+
class Solution {
135+
public:
136+
void printLinkedListInReverse(ImmutableListNode* head) {
137+
if (head) {
138+
printLinkedListInReverse(head->getNext());
139+
head->printValue();
140+
}
141+
}
142+
};
143+
```
90144
145+
### **Go**
146+
147+
```go
148+
/* Below is the interface for ImmutableListNode, which is already defined for you.
149+
*
150+
* type ImmutableListNode struct {
151+
*
152+
* }
153+
*
154+
* func (this *ImmutableListNode) getNext() ImmutableListNode {
155+
* // return the next node.
156+
* }
157+
*
158+
* func (this *ImmutableListNode) printValue() {
159+
* // print the value of this node.
160+
* }
161+
*/
162+
163+
func printLinkedListInReverse(head ImmutableListNode) {
164+
if head != nil {
165+
printLinkedListInReverse(head.getNext())
166+
head.printValue()
167+
}
168+
}
91169
```
92170

93171
### **...**

solution/1200-1299/1265.Print Immutable Linked List in Reverse/README_EN.md

+79-1
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,91 @@
7171
### **Python3**
7272

7373
```python
74-
74+
# """
75+
# This is the ImmutableListNode's API interface.
76+
# You should not implement it, or speculate about its implementation.
77+
# """
78+
# class ImmutableListNode:
79+
# def printValue(self) -> None: # print the value of this node.
80+
# def getNext(self) -> 'ImmutableListNode': # return the next node.
81+
82+
class Solution:
83+
def printLinkedListInReverse(self, head: 'ImmutableListNode') -> None:
84+
if head:
85+
self.printLinkedListInReverse(head.getNext())
86+
head.printValue()
7587
```
7688

7789
### **Java**
7890

7991
```java
92+
/**
93+
* // This is the ImmutableListNode's API interface.
94+
* // You should not implement it, or speculate about its implementation.
95+
* interface ImmutableListNode {
96+
* public void printValue(); // print the value of this node.
97+
* public ImmutableListNode getNext(); // return the next node.
98+
* };
99+
*/
100+
101+
class Solution {
102+
public void printLinkedListInReverse(ImmutableListNode head) {
103+
if (head != null) {
104+
printLinkedListInReverse(head.getNext());
105+
head.printValue();
106+
}
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
/**
115+
* // This is the ImmutableListNode's API interface.
116+
* // You should not implement it, or speculate about its implementation.
117+
* class ImmutableListNode {
118+
* public:
119+
* void printValue(); // print the value of the node.
120+
* ImmutableListNode* getNext(); // return the next node.
121+
* };
122+
*/
123+
124+
class Solution {
125+
public:
126+
void printLinkedListInReverse(ImmutableListNode* head) {
127+
if (head) {
128+
printLinkedListInReverse(head->getNext());
129+
head->printValue();
130+
}
131+
}
132+
};
133+
```
80134
135+
### **Go**
136+
137+
```go
138+
/* Below is the interface for ImmutableListNode, which is already defined for you.
139+
*
140+
* type ImmutableListNode struct {
141+
*
142+
* }
143+
*
144+
* func (this *ImmutableListNode) getNext() ImmutableListNode {
145+
* // return the next node.
146+
* }
147+
*
148+
* func (this *ImmutableListNode) printValue() {
149+
* // print the value of this node.
150+
* }
151+
*/
152+
153+
func printLinkedListInReverse(head ImmutableListNode) {
154+
if head != nil {
155+
printLinkedListInReverse(head.getNext())
156+
head.printValue()
157+
}
158+
}
81159
```
82160

83161
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* // This is the ImmutableListNode's API interface.
3+
* // You should not implement it, or speculate about its implementation.
4+
* class ImmutableListNode {
5+
* public:
6+
* void printValue(); // print the value of the node.
7+
* ImmutableListNode* getNext(); // return the next node.
8+
* };
9+
*/
10+
11+
class Solution {
12+
public:
13+
void printLinkedListInReverse(ImmutableListNode* head) {
14+
if (head) {
15+
printLinkedListInReverse(head->getNext());
16+
head->printValue();
17+
}
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* Below is the interface for ImmutableListNode, which is already defined for you.
2+
*
3+
* type ImmutableListNode struct {
4+
*
5+
* }
6+
*
7+
* func (this *ImmutableListNode) getNext() ImmutableListNode {
8+
* // return the next node.
9+
* }
10+
*
11+
* func (this *ImmutableListNode) printValue() {
12+
* // print the value of this node.
13+
* }
14+
*/
15+
16+
func printLinkedListInReverse(head ImmutableListNode) {
17+
if head != nil {
18+
printLinkedListInReverse(head.getNext())
19+
head.printValue()
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* // This is the ImmutableListNode's API interface.
3+
* // You should not implement it, or speculate about its implementation.
4+
* interface ImmutableListNode {
5+
* public void printValue(); // print the value of this node.
6+
* public ImmutableListNode getNext(); // return the next node.
7+
* };
8+
*/
9+
10+
class Solution {
11+
public void printLinkedListInReverse(ImmutableListNode head) {
12+
if (head != null) {
13+
printLinkedListInReverse(head.getNext());
14+
head.printValue();
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# """
2+
# This is the ImmutableListNode's API interface.
3+
# You should not implement it, or speculate about its implementation.
4+
# """
5+
# class ImmutableListNode:
6+
# def printValue(self) -> None: # print the value of this node.
7+
# def getNext(self) -> 'ImmutableListNode': # return the next node.
8+
9+
class Solution:
10+
def printLinkedListInReverse(self, head: 'ImmutableListNode') -> None:
11+
if head:
12+
self.printLinkedListInReverse(head.getNext())
13+
head.printValue()

solution/1500-1599/1570.Dot Product of Two Sparse Vectors/README.md

+60-3
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,84 @@ v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0
5656
<li><code>0 <= nums1[i], nums2[i] <= 100</code></li>
5757
</ul>
5858

59-
6059
## 解法
6160

6261
<!-- 这里可写通用的实现逻辑 -->
6362

63+
哈希表实现。
64+
65+
用哈希表存储非 0 点的下标与值。求点积时,遍历长度较小的哈希表。
66+
6467
<!-- tabs:start -->
6568

6669
### **Python3**
6770

6871
<!-- 这里可写当前语言的特殊实现逻辑 -->
6972

7073
```python
71-
74+
class SparseVector:
75+
def __init__(self, nums: List[int]):
76+
self.v = {}
77+
for i, num in enumerate(nums):
78+
if num != 0:
79+
self.v[i] = num
80+
81+
# Return the dotProduct of two sparse vectors
82+
def dotProduct(self, vec: 'SparseVector') -> int:
83+
res = 0
84+
if len(self.v) > len(vec.v):
85+
self.v, vec.v = vec.v, self.v
86+
for i, num in self.v.items():
87+
if i not in vec.v:
88+
continue
89+
res += num * vec.v[i]
90+
return res
91+
92+
93+
# Your SparseVector object will be instantiated and called as such:
94+
# v1 = SparseVector(nums1)
95+
# v2 = SparseVector(nums2)
96+
# ans = v1.dotProduct(v2)
7297
```
7398

7499
### **Java**
75100

76101
<!-- 这里可写当前语言的特殊实现逻辑 -->
77102

78103
```java
79-
104+
class SparseVector {
105+
106+
private Map<Integer, Integer> v;
107+
108+
SparseVector(int[] nums) {
109+
v = new HashMap<>();
110+
for (int i = 0; i < nums.length; ++i) {
111+
if (nums[i] != 0) {
112+
v.put(i, nums[i]);
113+
}
114+
}
115+
}
116+
117+
// Return the dotProduct of two sparse vectors
118+
public int dotProduct(SparseVector vec) {
119+
int res = 0;
120+
if (v.size() > vec.v.size()) {
121+
Map<Integer, Integer> t = v;
122+
v = vec.v;
123+
vec.v = t;
124+
}
125+
for (Map.Entry<Integer, Integer> entry : v.entrySet()) {
126+
int i = entry.getKey(), num = entry.getValue();
127+
res += num * vec.v.getOrDefault(i, 0);
128+
}
129+
return res;
130+
}
131+
}
132+
133+
// Your SparseVector object will be instantiated and called as such:
134+
// SparseVector v1 = new SparseVector(nums1);
135+
// SparseVector v2 = new SparseVector(nums2);
136+
// int ans = v1.dotProduct(v2);
80137
```
81138

82139
### **...**

0 commit comments

Comments
 (0)