Skip to content
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
25 changes: 25 additions & 0 deletions solution/0100-0199/0100.Same Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,31 @@ func isSameTree(p *TreeNode, q *TreeNode) bool {
}
```

### **JavaScript**

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
if (!p && !q) return true;
if (p && q) {
return (p.val === q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right))
}
return false;
};
```

### **...**

```
Expand Down
25 changes: 25 additions & 0 deletions solution/0100-0199/0100.Same Tree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ func isSameTree(p *TreeNode, q *TreeNode) bool {
}
```

### **JavaScript**

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
if (!p && !q) return true;
if (p && q) {
return (p.val === q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right))
}
return false;
};
```

### **...**

```
Expand Down
20 changes: 20 additions & 0 deletions solution/0100-0199/0100.Same Tree/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
if (!p && !q) return true;
if (p && q) {
return (p.val === q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right))
}
return false;
};
53 changes: 52 additions & 1 deletion solution/0100-0199/0173.Binary Search Tree Iterator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<p><strong>示例:</strong></p>
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0173.Binary%20Search%20Tree%20Iterator/images/bst-tree.png" style="width: 189px; height: 178px;" />
<div>
<pre>
<strong>输入</strong>
["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
Expand All @@ -44,7 +45,7 @@ bSTIterator.hasNext(); // 返回 True
bSTIterator.next(); // 返回 20
bSTIterator.hasNext(); // 返回 False
</pre>

</div>
<p> </p>

<p><strong>提示:</strong></p>
Expand Down Expand Up @@ -171,6 +172,56 @@ class BSTIterator {
*/
```

## **JavaScript**

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
*/
var BSTIterator = function (root) {
this.stk = [];
this.cur = root;
}


/**
* @return {number}
*/
BSTIterator.prototype.next = function () {
while (this.cur) {
this.stk.push(this.cur);
this.cur = this.cur.left;
}
this.cur = this.stk.pop();
let res = this.cur.val;
this.cur = this.cur.right;
return (res);
};

/**
* @return {boolean}
*/
BSTIterator.prototype.hasNext = function () {
if (this.stk.length === 0 && this.cur === null) return false;
return true;
};

/**
* Your BSTIterator object will be instantiated and called as such:
* var obj = new BSTIterator(root)
* var param_1 = obj.next()
* var param_2 = obj.hasNext()
*/
```

### **...**

```
Expand Down
52 changes: 52 additions & 0 deletions solution/0100-0199/0173.Binary Search Tree Iterator/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0173.Binary%20Search%20Tree%20Iterator/images/bst-tree.png" style="width: 189px; height: 178px;" />
<div>
<pre>
<strong>Input</strong>
[&quot;BSTIterator&quot;, &quot;next&quot;, &quot;next&quot;, &quot;hasNext&quot;, &quot;next&quot;, &quot;hasNext&quot;, &quot;next&quot;, &quot;hasNext&quot;, &quot;next&quot;, &quot;hasNext&quot;]
Expand All @@ -38,6 +39,7 @@ bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 20
bSTIterator.hasNext(); // return False
</pre>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
Expand Down Expand Up @@ -153,6 +155,56 @@ class BSTIterator {
*/
```

## **JavaScript**

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
*/
var BSTIterator = function (root) {
this.stk = [];
this.cur = root;
}


/**
* @return {number}
*/
BSTIterator.prototype.next = function () {
while (this.cur) {
this.stk.push(this.cur);
this.cur = this.cur.left;
}
this.cur = this.stk.pop();
let res = this.cur.val;
this.cur = this.cur.right;
return (res);
};

/**
* @return {boolean}
*/
BSTIterator.prototype.hasNext = function () {
if (this.stk.length === 0 && this.cur === null) return false;
return true;
};

/**
* Your BSTIterator object will be instantiated and called as such:
* var obj = new BSTIterator(root)
* var param_1 = obj.next()
* var param_2 = obj.hasNext()
*/
```

### **...**

```
Expand Down
45 changes: 45 additions & 0 deletions solution/0100-0199/0173.Binary Search Tree Iterator/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
*/
var BSTIterator = function (root) {
this.stk = [];
this.cur = root;
}


/**
* @return {number}
*/
BSTIterator.prototype.next = function () {
while (this.cur) {
this.stk.push(this.cur);
this.cur = this.cur.left;
}
this.cur = this.stk.pop();
let res = this.cur.val;
this.cur = this.cur.right;
return (res);
};

/**
* @return {boolean}
*/
BSTIterator.prototype.hasNext = function () {
if (this.stk.length === 0 && this.cur === null) return false;
return true;
};

/**
* Your BSTIterator object will be instantiated and called as such:
* var obj = new BSTIterator(root)
* var param_1 = obj.next()
* var param_2 = obj.hasNext()
*/
73 changes: 70 additions & 3 deletions solution/1400-1499/1429.First Unique Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,94 @@ firstUnique.showFirstUnique(); // 返回 -1
<li>最多调用 <code>5000</code> 次 <code>showFirstUnique</code> 和 <code>add</code> 。</li>
</ul>


## 解法

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

“有序哈希表”实现。

<!-- tabs:start -->

### **Python3**

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

```python

class FirstUnique:

def __init__(self, nums: List[int]):
self.counter = collections.OrderedDict()
self.unique_nums = collections.OrderedDict()
for num in nums:
self.counter[num] = self.counter.get(num, 0) + 1
for k, v in self.counter.items():
if v == 1:
self.unique_nums[k] = 1

def showFirstUnique(self) -> int:
if len(self.unique_nums) == 0:
return -1
for k in self.unique_nums.keys():
return k

def add(self, value: int) -> None:
if value not in self.counter:
self.counter[value] = 1
self.unique_nums[value] = 1
else:
self.counter[value] += 1
if value in self.unique_nums:
self.unique_nums.pop(value)

# Your FirstUnique object will be instantiated and called as such:
# obj = FirstUnique(nums)
# param_1 = obj.showFirstUnique()
# obj.add(value)
```

### **Java**

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

```java

class FirstUnique {
private Map<Integer, Integer> counter;
private Set<Integer> uniqueNums;

public FirstUnique(int[] nums) {
counter = new LinkedHashMap<>();
uniqueNums = new LinkedHashSet<>();
for (int num : nums) {
counter.put(num, counter.getOrDefault(num, 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
if (entry.getValue() == 1) {
uniqueNums.add(entry.getKey());
}
}
}

public int showFirstUnique() {
return uniqueNums.isEmpty() ? -1 : uniqueNums.iterator().next();
}

public void add(int value) {
if (!counter.containsKey(value)) {
counter.put(value, 1);
uniqueNums.add(value);
} else {
counter.put(value, counter.get(value) + 1);
uniqueNums.remove(value);
}
}
}

/**
* Your FirstUnique object will be instantiated and called as such:
* FirstUnique obj = new FirstUnique(nums);
* int param_1 = obj.showFirstUnique();
* obj.add(value);
*/
```

### **...**
Expand Down
Loading