Skip to content

[pull] main from doocs:main #123

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 31 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ee7ddb9
feat: add solutions to lc problems: No.3151~3153 (#2838)
yanglbme May 19, 2024
3f6a34f
feat: add solutions to lc problem: No.3154 (#2842)
yanglbme May 19, 2024
51b984f
feat: add cpp solution to lc problem: No.3068 (#2839)
AlleyNawaz May 19, 2024
f55b0d7
feat: add cpp solution to lc problem: No.3068 (#2840)
AlleyNawaz May 19, 2024
ac3102d
feat: add cpp solution to lc problem: No.3068 (#2841)
AlleyNawaz May 19, 2024
818daa9
feat: add solutions to lc problem: No.3155 (#2843)
yanglbme May 19, 2024
06533ad
feat: add solutions to lc problem: No.1317 (#2844)
yanglbme May 19, 2024
41ebef4
refactor: update solution with sorting via object to lc problem: No.2…
rain84 May 20, 2024
aa8ed86
refactor: update solution to lc problem: No.2625 (#2848)
rain84 May 20, 2024
dfb0297
feat: add ts solution to lc problem: No.1542 (#2850)
yanglbme May 20, 2024
5e67f9f
feat: update solutions to lc problem: No.2705 (#2851)
yanglbme May 20, 2024
5301c76
feat: update lc problems (#2852)
yanglbme May 20, 2024
4db3cd4
feat: add swift implementation to lcof problem: No.06 (#2853)
klever34 May 20, 2024
d2ee6aa
feat: add swift implementation to lcof problem: No.07 (#2854)
klever34 May 20, 2024
5cc6315
feat: add swift implementation to lcof problem: No.09 (#2855)
klever34 May 20, 2024
d451882
feat: add swift implementation to lcof problem: No.10.1 (#2856)
klever34 May 20, 2024
98beaf4
feat: add swift implementation to lcof problem: No.10.2 (#2857)
klever34 May 20, 2024
5f6a3af
feat: add swift implementation to lcof problem: No.11 (#2858)
klever34 May 20, 2024
df9bd5d
feat: add sql solution to lc problem: No.3156 (#2859)
yanglbme May 20, 2024
14c8085
feat: add solutions to lcp problem: No.80 (#2860)
yanglbme May 20, 2024
feae0f6
feat: update solutions to lc/lcof problems (#2861)
yanglbme May 21, 2024
27036e8
feat: update solutions to lcof problem: No.15 (#2862)
yanglbme May 21, 2024
6bb333d
--- (#2863)
dependabot[bot] May 21, 2024
a9caf56
feat: add swift implementation to lcof problem: No.12 (#2864)
klever34 May 21, 2024
d449a8a
feat: add swift implementation to lcof problem: No.13 (#2865)
klever34 May 21, 2024
7796a59
feat: add swift implementation to lcof problem: No.14.1 (#2866)
klever34 May 21, 2024
fc09c8c
feat: add swift implementation to lcof problem: No.14.2 (#2867)
klever34 May 21, 2024
d6c9b4f
feat: add swift implementation to lcof problem: No.15 (#2868)
klever34 May 21, 2024
c954041
feat: add swift implementation to lcof problem: No.16 (#2869)
klever34 May 21, 2024
3c5c2d6
feat: add solutions to lc problem: No.2831 (#2870)
yanglbme May 21, 2024
3768553
feat: update solutions to lcof problems: No.26,27,30 (#2871)
yanglbme May 21, 2024
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
Prev Previous commit
Next Next commit
feat: update solutions to lc/lcof problems (doocs#2861)
  • Loading branch information
yanglbme authored May 21, 2024
commit feae0f632baa19f40c5956a5fc0cddf243de34db
126 changes: 65 additions & 61 deletions lcof/面试题06. 从尾到头打印链表/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ class Solution {
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
if (!head) return {};
vector<int> ans = reversePrint(head->next);
ans.push_back(head->val);
vector<int> ans;
for (; head; head = head->next) {
ans.push_back(head->val);
}
reverse(ans.begin(), ans.end());
return ans;
}
};
Expand Down Expand Up @@ -145,11 +147,11 @@ func reversePrint(head *ListNode) (ans []int) {
*/

function reversePrint(head: ListNode | null): number[] {
let ans: number[] = [];
for (; !!head; head = head.next) {
ans.unshift(head.val);
const ans: number[] = [];
for (; head; head = head.next) {
ans.push(head.val);
}
return ans;
return ans.reverse();
}
```

Expand All @@ -174,14 +176,14 @@ function reversePrint(head: ListNode | null): number[] {
// }
impl Solution {
pub fn reverse_print(head: Option<Box<ListNode>>) -> Vec<i32> {
let mut arr: Vec<i32> = vec![];
let mut ans: Vec<i32> = vec![];
let mut cur = head;
while let Some(node) = cur {
arr.push(node.val);
ans.push(node.val);
cur = node.next;
}
arr.reverse();
arr
ans.reverse();
ans
}
}
```
Expand All @@ -191,21 +193,21 @@ impl Solution {
```js
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {number[]}
*/
var reversePrint = function (head) {
let ans = [];
for (; !!head; head = head.next) {
ans.unshift(head.val);
const ans = [];
for (; head; head = head.next) {
ans.push(head.val);
}
return ans;
return ans.reverse();
};
```

Expand All @@ -217,20 +219,49 @@ var reversePrint = function (head) {
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public int[] ReversePrint(ListNode head) {
List<int> ans = new List<int>();
while (head != null) {
ans.Add(head.val);
head = head.next;
}
ans.Reverse();
return ans.ToArray();
}
}
public class Solution {
public int[] ReversePrint(ListNode head) {
List<int> ans = new List<int>();
for (; head != null; head = head.next) {
ans.Add(head.val);
}
ans.Reverse();
return ans.ToArray();
}
}
```

#### Swift

```swift
/* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
*/

class Solution {
func reversePrint(_ head: ListNode?) -> [Int] {
var stack = [Int]()
var current = head
while let node = current {
stack.append(node.val)
current = node.next
}

return stack.reversed()
}
}
```

<!-- tabs:end -->
Expand Down Expand Up @@ -308,11 +339,11 @@ class Solution {
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int> ans;
for (; head; head = head->next) {
ans.push_back(head->val);
if (!head) {
return {};
}
reverse(ans.begin(), ans.end());
vector<int> ans = reversePrint(head->next);
ans.push_back(head->val);
return ans;
}
};
Expand Down Expand Up @@ -402,33 +433,6 @@ var reversePrint = function (head) {
};
```

#### Swift

```swift
/* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
*/

class Solution {
func reversePrint(_ head: ListNode?) -> [Int] {
var stack = [Int]()
var current = head
while let node = current {
stack.append(node.val)
current = node.next
}

return stack.reversed()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
8 changes: 5 additions & 3 deletions lcof/面试题06. 从尾到头打印链表/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
if (!head) return {};
vector<int> ans = reversePrint(head->next);
ans.push_back(head->val);
vector<int> ans;
for (; head; head = head->next) {
ans.push_back(head->val);
}
reverse(ans.begin(), ans.end());
return ans;
}
};
26 changes: 14 additions & 12 deletions lcof/面试题06. 从尾到头打印链表/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public int[] ReversePrint(ListNode head) {
List<int> ans = new List<int>();
while (head != null) {
ans.Add(head.val);
head = head.next;
}
ans.Reverse();
return ans.ToArray();
}
}
public class Solution {
public int[] ReversePrint(ListNode head) {
List<int> ans = new List<int>();
for (; head != null; head = head.next) {
ans.Add(head.val);
}
ans.Reverse();
return ans.ToArray();
}
}
14 changes: 7 additions & 7 deletions lcof/面试题06. 从尾到头打印链表/Solution.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {number[]}
*/
var reversePrint = function (head) {
let ans = [];
for (; !!head; head = head.next) {
ans.unshift(head.val);
const ans = [];
for (; head; head = head.next) {
ans.push(head.val);
}
return ans;
return ans.reverse();
};
8 changes: 4 additions & 4 deletions lcof/面试题06. 从尾到头打印链表/Solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
// }
impl Solution {
pub fn reverse_print(head: Option<Box<ListNode>>) -> Vec<i32> {
let mut arr: Vec<i32> = vec![];
let mut ans: Vec<i32> = vec![];
let mut cur = head;
while let Some(node) = cur {
arr.push(node.val);
ans.push(node.val);
cur = node.next;
}
arr.reverse();
arr
ans.reverse();
ans
}
}
8 changes: 4 additions & 4 deletions lcof/面试题06. 从尾到头打印链表/Solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
*/

function reversePrint(head: ListNode | null): number[] {
let ans: number[] = [];
for (; !!head; head = head.next) {
ans.unshift(head.val);
const ans: number[] = [];
for (; head; head = head.next) {
ans.push(head.val);
}
return ans;
return ans.reverse();
}
8 changes: 4 additions & 4 deletions lcof/面试题06. 从尾到头打印链表/Solution2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int> ans;
for (; head; head = head->next) {
ans.push_back(head->val);
if (!head) {
return {};
}
reverse(ans.begin(), ans.end());
vector<int> ans = reversePrint(head->next);
ans.push_back(head->val);
return ans;
}
};
6 changes: 3 additions & 3 deletions lcof/面试题11. 旋转数组的最小数字/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9

时间复杂度 $(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。

注意,我们也可以每次取中间元素 `numbers[mid]` 与左端元素 `numbers[l]` 比较,但需要考虑当前 $[l,..r]$ 区间内的元素是否已经有序,即是否满足 `numbers[l] < numbers[r]`,如果满足,直接返回 `numbers[l]` 即可。其它情况与上述方法类似。

<!-- tabs:start -->

#### Python3
Expand Down Expand Up @@ -234,7 +232,9 @@ class Solution {

<!-- solution:start-->

### 方法二
### 方法二:二分查找(写法二)

注意,我们也可以每次取中间元素 `numbers[mid]` 与左端元素 `numbers[l]` 比较,但需要考虑当前 $[l,..r]$ 区间内的元素是否已经有序,即是否满足 `numbers[l] < numbers[r]`,如果满足,直接返回 `numbers[l]` 即可。其它情况与方法一类似。

<!-- tabs:start -->

Expand Down
Loading