Skip to content

Commit 879793b

Browse files
committed
add 508
1 parent ce6f2fb commit 879793b

File tree

5 files changed

+109
-23
lines changed

5 files changed

+109
-23
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
4545
| [] | [9c-dp.md](./list/9c-dp.md) | 42/45 | 3 vips |
4646
| [] | [geekbang.md](./list/geekbang.md) | 55/55 | - |
4747
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
48-
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 67/92 | 17 vips |
48+
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 69/92 | 17 vips |
4949
| [🔲] | [leetcode-contest.md](./list/leetcode-contest.md) | 10/40 | - |
5050
| [🔲] | [leetcode-google.md](./list/leetcode-google.md) | 0/7 | - |
5151

@@ -528,7 +528,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
528528

529529
## Sorting
530530

531-
| Link | Problem(28) | Solution | Tag | Time | Space | Ref |
531+
| Link | Problem(29) | Solution | Tag | Time | Space | Ref |
532532
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
533533
| [Leetcode-15](https://leetcode.com/problems/3sum/) | 3Sum | [c++](./leetcode/15.3sum.cpp), [python3](./leetcode/15.3sum.py) | Sorting | O\(N^2\) | O\(1\) | - |
534534
| [Leetcode-18](https://leetcode.com/problems/4sum/) | 4Sum | [c++](./leetcode/18.4sum.cpp), [python3](./leetcode/18.4sum.py) | Sorting | O\(N^3\) | O\(1\) | - |
@@ -558,6 +558,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
558558
| [Leetcode-242](https://leetcode.com/problems/valid-anagram/) | Valid Anagram | [c++](./leetcode/242.valid-anagram.cpp), [python3](./leetcode/242.valid-anagram.py) | Sorting | O\(N\) | O\(1\) | - |
559559
| [Lintcode-919](https://www.lintcode.com/problem/meeting-rooms-ii/) | Meeting Rooms II | [c++](./lintcode/919.meeting-rooms-ii.cpp), [python3](./lintcode/919.meeting-rooms-ii.py) | Sorting | O\(NlogN\) | O\(N\) | Leetcode-253 |
560560
| [Lintcode-39](https://www.lintcode.com/problem/recover-rotated-sorted-array/) | Recover Rotated Sorted Array | [c++](./lintcode/39.recover-rotated-sorted-array.cpp), [python3](./lintcode/39.recover-rotated-sorted-array.py) | Sorting | O\(N\) | O\(1\) | - |
561+
| [Lintcode-508](https://www.lintcode.com/problem/wiggle-sort/) | Wiggle Sort | [c++](./lintcode/508.wiggle-sort.cpp), [python3](./lintcode/508.wiggle-sort.py) | Sorting | O\(N\) | O\(1\) | Leetcode-280 |
561562

562563
## Counting
563564

@@ -816,7 +817,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
816817

817818
## Other
818819

819-
| Link | Problem(137) | Solution | Tag | Time | Space | Ref |
820+
| Link | Problem(136) | Solution | Tag | Time | Space | Ref |
820821
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
821822
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | - |
822823
| [Leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | - |
@@ -939,7 +940,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
939940
| [Leetcode-125](https://leetcode.com/problems/valid-palindrome/) | Valid Palindrome | [c++](./leetcode/125.valid-palindrome.cpp), [python3](./leetcode/125.valid-palindrome.py) | Other | \- | \- | - |
940941
| [Leetcode-408](https://leetcode.com/problems/valid-word-abbreviation/) | Valid Word Abbreviation | [c++](./leetcode/408.valid-word-abbreviation.cpp), [python3](./leetcode/408.valid-word-abbreviation.py) | Other | \- | \- | - |
941942
| [Leetcode-286](https://leetcode.com/problems/walls-and-gates/) | Walls And Gates | [c++](./leetcode/286.walls-and-gates.cpp), [python3](./leetcode/286.walls-and-gates.py) | Other | \- | \- | - |
942-
| [Leetcode-280](https://leetcode.com/problems/wiggle-sort/) | Wiggle Sort | [python3](./leetcode/280.wiggle-sort.py) | Other | \- | \- | - |
943943
| [Leetcode-324](https://leetcode.com/problems/wiggle-sort-ii/) | Wiggle Sort II | [python3](./leetcode/324.wiggle-sort-ii.py) | Other | \- | \- | - |
944944
| [Leetcode-527](https://leetcode.com/problems/word-abbreviation/) | Word Abbreviation | [c++](./leetcode/527.word-abbreviation.cpp), [python3](./leetcode/527.word-abbreviation.py) | Other | \- | \- | - |
945945
| [Leetcode-127](https://leetcode.com/problems/word-ladder/) | Word Ladder | [c++](./leetcode/127.word-ladder.cpp), [python3](./leetcode/127.word-ladder.py) | Other | \- | \- | - |

leetcode/280.wiggle-sort.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

lintcode/508.wiggle-sort.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Tag: Quick Select, Sort
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: Leetcode-280
5+
// Note: -
6+
7+
// Given an unsorted array `nums`, reorder it **in-place** such that
8+
//
9+
// ```
10+
// nums[0] <= nums[1] >= nums[2] <= nums[3]....
11+
// ```
12+
//
13+
// There may have multiple answers for a question, **you only need to consider one of the possibilities**.
14+
//
15+
// **Example 1:**
16+
//
17+
// ```
18+
// Input: [3, 5, 2, 1, 6, 4]
19+
// Output: [1, 6, 2, 5, 3, 4]
20+
// Explanation: This question may have multiple answers, and [2, 6, 1, 5, 3, 4] is also ok.
21+
// ```
22+
//
23+
// **Example 2:**
24+
//
25+
// ```
26+
// Input: [1, 2, 3, 4]
27+
// Output: [1, 4, 2, 3]
28+
// ```
29+
//
30+
// Please sort the array in place and do not define additional arrays.
31+
32+
class Solution {
33+
public:
34+
/**
35+
* @param nums: A list of integers
36+
* @return: nothing
37+
*/
38+
void wiggleSort(vector<int> &nums) {
39+
// write your code here
40+
int n = nums.size();
41+
for (int i = 1; i < n; i++) {
42+
if (i & 1 == 1) {
43+
if (nums[i] < nums[i - 1]) {
44+
swap(nums[i], nums[i - 1]);
45+
}
46+
} else {
47+
if (nums[i] > nums[i - 1]) {
48+
swap(nums[i], nums[i - 1]);
49+
}
50+
}
51+
}
52+
}
53+
};

lintcode/508.wiggle-sort.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Tag: Quick Select, Sort
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: Leetcode-280
5+
# Note: -
6+
7+
# Given an unsorted array `nums`, reorder it **in-place** such that
8+
#
9+
# ```
10+
# nums[0] <= nums[1] >= nums[2] <= nums[3]....
11+
# ```
12+
#
13+
# There may have multiple answers for a question, **you only need to consider one of the possibilities**.
14+
#
15+
# **Example 1:**
16+
#
17+
# ```
18+
# Input: [3, 5, 2, 1, 6, 4]
19+
# Output: [1, 6, 2, 5, 3, 4]
20+
# Explanation: This question may have multiple answers, and [2, 6, 1, 5, 3, 4] is also ok.
21+
# ```
22+
#
23+
# **Example 2:**
24+
#
25+
# ```
26+
# Input: [1, 2, 3, 4]
27+
# Output: [1, 4, 2, 3]
28+
# ```
29+
#
30+
# Please sort the array in place and do not define additional arrays.
31+
32+
from typing import (
33+
List,
34+
)
35+
36+
class Solution:
37+
"""
38+
@param nums: A list of integers
39+
@return: nothing
40+
"""
41+
def wiggle_sort(self, nums: List[int]):
42+
# write your code here
43+
n = len(nums)
44+
for i in range(1, n):
45+
if i & 1:
46+
if nums[i] < nums[i - 1]:
47+
nums[i - 1], nums[i] = nums[i], nums[i - 1]
48+
else:
49+
if nums[i] > nums[i - 1]:
50+
nums[i - 1], nums[i] = nums[i], nums[i - 1]

list/9c-advanced.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@
9999
80. https://www.lintcode.com/problem/subarray-sum-closest/
100100
81. https://www.lintcode.com/problem/submatrix-sum/
101101
82. https://www.lintcode.com/problem/continuous-subarray-sum/
102-
83. https://www.lintcode.com/problem/maximum-subarray/
102+
83. https://leetcode.com/problems/maximum-subarray/
103103
84. https://www.lintcode.com/problem/continuous-subarray-sum-ii/
104104
85. https://leetcode.com/problems/kth-largest-element-in-an-array/
105+
86. https://www.lintcode.com/problem/wiggle-sort/
105106

106-
- https://www.lintcode.com/problem/wiggle-sort/
107107
- https://www.lintcode.com/problem/wiggle-sort-ii/
108108
- https://www.lintcode.com/problem/nuts-bolts-problem/
109109
- https://www.lintcode.com/problem/flatten-list/

0 commit comments

Comments
 (0)