Skip to content

Commit

Permalink
2020-03-25 day01 array commit
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenzhang1007 committed Mar 25, 2020
1 parent ceeea36 commit dcaaa9d
Show file tree
Hide file tree
Showing 12 changed files with 629 additions and 11 deletions.
60 changes: 60 additions & 0 deletions array/11_盛最多水的容器/11_medium_盛最多水的容器.cpp
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

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

56 changes: 56 additions & 0 deletions array/11_盛最多水的容器/11_medium_盛最多水的容器.py
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

78 changes: 78 additions & 0 deletions array/15_三数之和/15_medium_三数之和.cpp
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

Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,30 @@ public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();

for(int k = 0; k < nums.length - 2; k++){
for(int i = 0; i < nums.length - 2; i++){
// nums[k]为非负数,就不能满足a+b+c=0了
if(nums[k] > 0) break;
if(nums[i] > 0) break;
// 跳过计算过的数据,同时防止结果重复
if(k > 0 && nums[k] == nums[k - 1]){
if(i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int i = k + 1, j = nums.length - 1;
while(i < j){
int sum = nums[k] + nums[i] + nums[j];
int leftPoint = i + 1, rightPoint = nums.length - 1;
while(leftPoint < rightPoint){
int sum = nums[i] + nums[leftPoint] + nums[rightPoint];
if(sum < 0){
while(i < j && nums[i] == nums[++i]);
leftPoint++;
} else if (sum > 0) {
while(i < j && nums[j] == nums[--j]);
rightPoint--;
} else {
res.add(new ArrayList<Integer>(Arrays.asList(nums[k], nums[i], nums[j])));
while(i < j && nums[i] == nums[++i]);
while(i < j && nums[j] == nums[--j]);
res.add(Arrays.asList(nums[i], nums[leftPoint], nums[rightPoint]));
while (leftPoint < rightPoint && nums[leftPoint] == nums[leftPoint+1]) {
leftPoint++;
}
while(leftPoint < rightPoint && nums[rightPoint] == nums[rightPoint-1]) {
rightPoint--;
}
leftPoint++;
rightPoint--;
}
}
}
Expand Down
65 changes: 65 additions & 0 deletions array/283_移动零/283_easy_移动零.cpp
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

Loading

0 comments on commit dcaaa9d

Please sign in to comment.