Skip to content

feat: add solutions to lc problem: No.3173 #3045

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 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions lcof2/剑指 Offer II 025. 链表中的两数相加/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,36 +222,36 @@ func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
* init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/

class Solution {
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
var s1: [Int] = []
var s2: [Int] = []

var node1 = l1
var node2 = l2

while let n1 = node1 {
s1.append(n1.val)
node1 = n1.next
}

while let n2 = node2 {
s2.append(n2.val)
node2 = n2.next
}

var carry = 0
let dummy: ListNode? = ListNode(0)

while !s1.isEmpty || !s2.isEmpty || carry != 0 {
carry += (s1.isEmpty ? 0 : s1.removeLast()) + (s2.isEmpty ? 0 : s2.removeLast())
let node = ListNode(carry % 10)
node.next = dummy?.next
dummy?.next = node
carry /= 10
}

return dummy?.next
}
}
Expand Down
16 changes: 8 additions & 8 deletions lcof2/剑指 Offer II 026. 重排链表/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,15 @@ func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
class Solution {
func reorderList(_ head: ListNode?) {
guard let head = head else { return }

let mid = middleNode(head)

let secondHalf = reverseList(mid.next)
mid.next = nil

mergeTwoLists(head, secondHalf)
}

private func middleNode(_ head: ListNode?) -> ListNode {
var slow = head
var fast = head
Expand All @@ -323,7 +323,7 @@ class Solution {
}
return slow!
}

private func reverseList(_ head: ListNode?) -> ListNode? {
var prev: ListNode? = nil
var curr = head
Expand All @@ -335,20 +335,20 @@ class Solution {
}
return prev
}

private func mergeTwoLists(_ l1: ListNode?, _ l2: ListNode?) {
var l1 = l1
var l2 = l2
while l1 != nil && l2 != nil {
let l1Next = l1?.next
let l2Next = l2?.next

l1?.next = l2
if l1Next == nil {
break
}
l2?.next = l1Next

l1 = l1Next
l2 = l2Next
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ public:
class RandomizedSet {
private var m: [Int: Int]
private var a: [Int]

init() {
self.m = [Int: Int]()
self.a = [Int]()
}

func insert(_ val: Int) -> Bool {
if m[val] != nil {
return false
Expand All @@ -237,7 +237,7 @@ class RandomizedSet {
a.append(val)
return true
}

func remove(_ val: Int) -> Bool {
if let idx = m[val] {
let last = a.count - 1
Expand All @@ -251,7 +251,7 @@ class RandomizedSet {
}
return false
}

func getRandom() -> Int {
return a[Int.random(in: 0..<a.count)]
}
Expand Down
8 changes: 4 additions & 4 deletions lcof2/剑指 Offer II 032. 有效的变位词/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,20 @@ class Solution {
if m != n || s == t {
return false
}

var cnt = [Int](repeating: 0, count: 26)

for (sc, tc) in zip(s, t) {
cnt[Int(sc.asciiValue! - Character("a").asciiValue!)] += 1
cnt[Int(tc.asciiValue! - Character("a").asciiValue!)] -= 1
}

for x in cnt {
if x != 0 {
return false
}
}

return true
}
}
Expand Down
4 changes: 2 additions & 2 deletions lcof2/剑指 Offer II 033. 变位词组/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ function groupAnagrams(strs: string[]): string[][] {
class Solution {
func groupAnagrams(_ strs: [String]) -> [[String]] {
var d = [String: [String]]()

for s in strs {
let sortedStr = String(s.sorted())
if d[sortedStr] == nil {
d[sortedStr] = [String]()
}
d[sortedStr]!.append(s)
}

return Array(d.values)
}
}
Expand Down
8 changes: 4 additions & 4 deletions lcof2/剑指 Offer II 034. 外星语言是否排序/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,21 @@ function isAlienSorted(words: string[], order: string): boolean {
class Solution {
func isAlienSorted(_ words: [String], _ order: String) -> Bool {
var index = [Character: Int]()

for (i, char) in order.enumerated() {
index[char] = i
}

for i in 0..<words.count - 1 {
let w1 = Array(words[i])
let w2 = Array(words[i + 1])
let l1 = w1.count
let l2 = w2.count

for j in 0..<max(l1, l2) {
let i1 = j >= l1 ? -1 : index[w1[j]]!
let i2 = j >= l2 ? -1 : index[w2[j]]!

if i1 > i2 {
return false
}
Expand Down
10 changes: 5 additions & 5 deletions lcof2/剑指 Offer II 035. 最小时间差/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,22 @@ class Solution {
if timePoints.count > 24 * 60 {
return 0
}

var mins = [Int]()

for t in timePoints {
let time = t.split(separator: ":").map { Int($0)! }
mins.append(time[0] * 60 + time[1])
}

mins.sort()
mins.append(mins[0] + 24 * 60)

var ans = Int.max
for i in 1..<mins.count {
ans = min(ans, mins[i] - mins[i - 1])
}

return ans
}
}
Expand Down
13 changes: 12 additions & 1 deletion solution/0400-0499/0455.Assign Cookies/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,18 @@ You need to output 2.

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting + Two Pointers

According to the problem description, we should prioritize giving cookies to children with smaller appetites, so as to satisfy as many children as possible.

Therefore, we first sort the two arrays, and then use two pointers $i$ and $j$ to point to the head of arrays $g$ and $s$ respectively. Each time we compare the size of $g[i]$ and $s[j]$:

- If $s[j] < g[i]$, it means that the current cookie $s[j]$ cannot satisfy the current child $g[i]$. We should allocate a larger cookie to the current child, so $j$ should move to the right by one. If $j$ goes out of bounds, it means that the current child cannot be satisfied. At this time, the number of successfully allocated children is $i$, and we can return directly.
- If $s[j] \ge g[i]$, it means that the current cookie $s[j]$ can satisfy the current child $g[i]$. We allocate the current cookie to the current child, so both $i$ and $j$ should move to the right by one.

If we have traversed the array $g$, it means that all children have been allocated cookies, and we can return the total number of children.

The time complexity is $O(m \times \log m + n \times \log n)$, and the space complexity is $O(\log m + \log n)$. Where $m$ and $n$ are the lengths of arrays $g$ and $s$ respectively.

<!-- tabs:start -->

Expand Down
128 changes: 128 additions & 0 deletions solution/3100-3199/3173.Bitwise OR of Adjacent Elements/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3173.Bitwise%20OR%20of%20Adjacent%20Elements/README.md
---

<!-- problem:start -->

# [3173. Bitwise OR of Adjacent Elements 🔒](https://leetcode.cn/problems/bitwise-or-of-adjacent-elements)

[English Version](/solution/3100-3199/3173.Bitwise%20OR%20of%20Adjacent%20Elements/README_EN.md)

## 题目描述

<!-- description:start -->

<p>Given an array <code>nums</code> of length <code>n</code>, return an array <code>answer</code> of length <code>n - 1</code> such that <code>answer[i] = nums[i] | nums[i + 1]</code> where <code>|</code> is the bitwise <code>OR</code> operation.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,7,15]</span></p>

<p><strong>Output:</strong> <span class="example-io">[3,7,15]</span></p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [8,4,2]</span></p>

<p><strong>Output:</strong> <span class="example-io">[12,6]</span></p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [5,4,9,11]</span></p>

<p><strong>Output:</strong> <span class="example-io">[5,13,11]</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
<li><code>0 &lt;= nums[i]&nbsp;&lt;= 100</code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:遍历

我们遍历数组的前 $n - 1$ 个元素,对于每个元素,计算它和它的下一个元素的按位或值,将结果存入答案数组中。

时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def orArray(self, nums: List[int]) -> List[int]:
return [a | b for a, b in pairwise(nums)]
```

#### Java

```java
class Solution {
public int[] orArray(int[] nums) {
int n = nums.length;
int[] ans = new int[n - 1];
for (int i = 0; i < n - 1; ++i) {
ans[i] = nums[i] | nums[i + 1];
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
vector<int> orArray(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n - 1);
for (int i = 0; i < n - 1; ++i) {
ans[i] = nums[i] | nums[i + 1];
}
return ans;
}
};
```

#### Go

```go
func orArray(nums []int) (ans []int) {
for i, x := range nums[1:] {
ans = append(ans, x|nums[i])
}
return
}
```

#### TypeScript

```ts
function orArray(nums: number[]): number[] {
return nums.slice(0, -1).map((v, i) => v | nums[i + 1]);
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading