-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ceeea36
commit dcaaa9d
Showing
12 changed files
with
629 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* @lc app=leetcode.cn id=11 lang=cpp | ||
* | ||
* [11] 盛最多水的容器 | ||
* | ||
* https://leetcode-cn.com/problems/container-with-most-water/description/ | ||
* | ||
* algorithms | ||
* Medium (62.23%) | ||
* Likes: 1223 | ||
* Dislikes: 0 | ||
* Total Accepted: 162.4K | ||
* Total Submissions: 261K | ||
* Testcase Example: '[1,8,6,2,5,4,8,3,7]' | ||
* | ||
* 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 | ||
* (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 | ||
* | ||
* 说明:你不能倾斜容器,且 n 的值至少为 2。 | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* 图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。 | ||
* | ||
* | ||
* | ||
* 示例: | ||
* | ||
* 输入:[1,8,6,2,5,4,8,3,7] | ||
* 输出:49 | ||
* | ||
*/ | ||
|
||
// @lc code=start | ||
class Solution { | ||
public: | ||
int maxArea(vector<int>& height) { | ||
int leftPoint = 0; | ||
int rightPoint = height.size() - 1; | ||
int Area = 0; | ||
while (leftPoint < rightPoint) { | ||
if (height[leftPoint] < height[rightPoint]){ | ||
int minHeight = height[leftPoint]; | ||
leftPoint++; | ||
int area = (rightPoint - leftPoint + 1) * minHeight; | ||
Area = max(area, Area); | ||
} else { | ||
int minHeight = height[rightPoint]; | ||
rightPoint--; | ||
int area = (rightPoint - leftPoint + 1) * minHeight; | ||
Area = max(area, Area); | ||
} | ||
} | ||
return Area; | ||
} | ||
}; | ||
// @lc code=end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* @lc app=leetcode.cn id=11 lang=java | ||
* | ||
* [11] 盛最多水的容器 | ||
* | ||
* https://leetcode-cn.com/problems/container-with-most-water/description/ | ||
* | ||
* algorithms | ||
* Medium (62.23%) | ||
* Likes: 1223 | ||
* Dislikes: 0 | ||
* Total Accepted: 162.4K | ||
* Total Submissions: 261K | ||
* Testcase Example: '[1,8,6,2,5,4,8,3,7]' | ||
* | ||
* 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 | ||
* (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 | ||
* | ||
* 说明:你不能倾斜容器,且 n 的值至少为 2。 | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* 图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。 | ||
* | ||
* | ||
* | ||
* 示例: | ||
* | ||
* 输入:[1,8,6,2,5,4,8,3,7] | ||
* 输出:49 | ||
* | ||
*/ | ||
|
||
// @lc code=start | ||
class Solution { | ||
public int maxArea(int[] height) { | ||
int Area = 0; | ||
int leftPoint = 0; | ||
int rightPoint = height.length - 1; | ||
while (leftPoint < rightPoint) { | ||
if (height[leftPoint] < height[rightPoint]) { | ||
int minHeight = height[leftPoint]; | ||
leftPoint++; | ||
int area = (rightPoint - leftPoint + 1) * minHeight; | ||
Area = Math.max(area, Area); | ||
} else { | ||
int minHeight = height[rightPoint]; | ||
rightPoint--; | ||
int area = (rightPoint - leftPoint + 1) * minHeight; | ||
Area = Math.max(area, Area); | ||
} | ||
} | ||
return Area; | ||
} | ||
} | ||
// @lc code=end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# | ||
# @lc app=leetcode.cn id=11 lang=python3 | ||
# | ||
# [11] 盛最多水的容器 | ||
# | ||
# https://leetcode-cn.com/problems/container-with-most-water/description/ | ||
# | ||
# algorithms | ||
# Medium (62.23%) | ||
# Likes: 1223 | ||
# Dislikes: 0 | ||
# Total Accepted: 162.4K | ||
# Total Submissions: 261K | ||
# Testcase Example: '[1,8,6,2,5,4,8,3,7]' | ||
# | ||
# 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, | ||
# ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 | ||
# | ||
# 说明:你不能倾斜容器,且 n 的值至少为 2。 | ||
# | ||
# | ||
# | ||
# | ||
# | ||
# 图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。 | ||
# | ||
# | ||
# | ||
# 示例: | ||
# | ||
# 输入:[1,8,6,2,5,4,8,3,7] | ||
# 输出:49 | ||
# | ||
# | ||
|
||
# @lc code=start | ||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
Area = 0 | ||
i, j = 0, len(height) - 1 | ||
while i < j: | ||
if (height[i] < height[j]) : | ||
minHeight = height[i] | ||
i += 1 | ||
area = (j-i+1) * minHeight | ||
Area = max(area, Area) | ||
else: | ||
minHeight = height[j] | ||
j -= 1 | ||
area = (j-i+1) * minHeight | ||
Area = max(area, Area) | ||
return Area | ||
|
||
|
||
# @lc code=end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* @lc app=leetcode.cn id=15 lang=cpp | ||
* | ||
* [15] 三数之和 | ||
* | ||
* https://leetcode-cn.com/problems/3sum/description/ | ||
* | ||
* algorithms | ||
* Medium (26.16%) | ||
* Likes: 1923 | ||
* Dislikes: 0 | ||
* Total Accepted: 182.9K | ||
* Total Submissions: 699.3K | ||
* Testcase Example: '[-1,0,1,2,-1,-4]' | ||
* | ||
* 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 | ||
* ?请你找出所有满足条件且不重复的三元组。 | ||
* | ||
* 注意:答案中不可以包含重复的三元组。 | ||
* | ||
* | ||
* | ||
* 示例: | ||
* | ||
* 给定数组 nums = [-1, 0, 1, 2, -1, -4], | ||
* | ||
* 满足要求的三元组集合为: | ||
* [ | ||
* [-1, 0, 1], | ||
* [-1, -1, 2] | ||
* ] | ||
* | ||
* | ||
*/ | ||
|
||
// @lc code=start | ||
class Solution { | ||
public: | ||
vector<vector<int>> threeSum(vector<int>& nums) { | ||
vector<vector<int>> res; | ||
vector<int > vtemp; | ||
sort(nums.begin(), nums.end()); // sort | ||
|
||
for (int i = 0; i < nums.size(); i++) { | ||
if (nums[i] > 0) { | ||
break; | ||
} | ||
if (i > 0 && nums[i] == nums[i-1]) { | ||
continue; | ||
} | ||
int leftPoint = i + 1; | ||
int rightPoint = nums.size() - 1; | ||
while (leftPoint < rightPoint) { | ||
int sum = nums[leftPoint] + nums[rightPoint] + nums[i]; | ||
if (sum < 0) { | ||
leftPoint++; | ||
} else if(sum > 0) { | ||
rightPoint--; | ||
} else{ | ||
vector<int > vtemp{nums[i], nums[leftPoint], nums[rightPoint]}; | ||
res.push_back(vtemp); | ||
vtemp.clear(); | ||
while (leftPoint < rightPoint && nums[leftPoint] == nums[leftPoint+1]) { | ||
leftPoint++; | ||
} | ||
while (leftPoint < rightPoint && nums[rightPoint] == nums[rightPoint-1]) { | ||
rightPoint--; | ||
} | ||
leftPoint++; | ||
rightPoint--; | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
}; | ||
// @lc code=end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* @lc app=leetcode.cn id=283 lang=cpp | ||
* | ||
* [283] 移动零 | ||
* | ||
* https://leetcode-cn.com/problems/move-zeroes/description/ | ||
* | ||
* algorithms | ||
* Easy (59.74%) | ||
* Likes: 507 | ||
* Dislikes: 0 | ||
* Total Accepted: 114.3K | ||
* Total Submissions: 191.3K | ||
* Testcase Example: '[0,1,0,3,12]' | ||
* | ||
* 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 | ||
* | ||
* 示例: | ||
* | ||
* 输入: [0,1,0,3,12] | ||
* 输出: [1,3,12,0,0] | ||
* | ||
* 说明: | ||
* | ||
* | ||
* 必须在原数组上操作,不能拷贝额外的数组。 | ||
* 尽量减少操作次数。 | ||
* | ||
* | ||
*/ | ||
|
||
// @lc code=start | ||
class Solution { | ||
public: | ||
void moveZeroes(vector<int>& nums) { | ||
/** | ||
// 1. index j始终指向下一个非0元素要放的位置 | ||
int j = 0; | ||
for (int i = 0; i < nums.size(); i++) { | ||
if(nums[i] != 0) { | ||
nums[j] = nums[i]; | ||
if (i != j) { | ||
nums[i] = 0; | ||
} | ||
j++; | ||
} | ||
} | ||
*/ | ||
|
||
// 2. swap j始终指向数组中第一个0元素的位置 | ||
int j = 0; | ||
for (int i = 0; i < nums.size(); i++){ | ||
if (nums[i] != 0){ | ||
int tmp = nums[i]; | ||
nums[i] = nums[j]; | ||
nums[j] = tmp; | ||
j++; | ||
} | ||
} | ||
|
||
|
||
} | ||
}; | ||
// @lc code=end | ||
|
Oops, something went wrong.