Journey of LeetCode|DAY 2
Update
Code Review
1.modify the wrong link of problem 3.
2.review code, Spiral Matrix II needed be reviewed again and again.
Preface
OMG, I have to say that it’s a little hard for me to conquer the Problems of the LeetCode by myself. I have no enough time to create my code without solid basic coding experience.
Just focus on the code of the paradigm and try to understand them from zero.
Keep Learning the coding!
1.Squares of a Sorted Array
LeetCode: 977.Squares of a Sorted Array
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
1.Violent Solution
Analysis: Just make the array squares, then sorted them.
LeetCode C++Violent Solution
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
for (int i = 0; i < nums.size(); i++) {//for loop every element
nums[i] *= nums[i];//make array squares
}
sort(nums.begin(), nums.end()); // QucikSort
return nums;
}
};
2.Double Pointer
Analysis: Double pointer indicates the first one and last one, compare the new value; then store it in the result
nums[i] * nums[i] < nums[j] * nums[j]
then
result[k--] = nums[j] * nums[j]
else
result[k--] = nums[i] * nums[i]
LeetCode C++Double Pointer
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int k = nums.size() - 1;//K points to the end of the new array
vector<int> result(nums.size(), 0);//Make a new array to store the new one
for (int i = 0, j = nums.size() - 1; i <= j;) { // for loop,i<=j,to deal with two elements in the end.
if (nums[i] * nums[i] < nums[j] * nums[j]) {//compare the two elements
result[k--] = nums[j] * nums[j];//store the element in the result and move pointer
j--;
}
else {
result[k--] = nums[i] * nums[i];//same as before
i++;
}
}
return result;
}
};
2.Minimum Size Subarray Sum
LeetCode: 209.Minimum Size Subarray Sum
Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray [numsl, numsl+1, …, numsr-1, numsr] of which the sum is greater than or equal to target. If there is no such subarray, return 0 instead.
1.Violent Solution
Analysis: Double for loop to find the answer.
LeetCode C++Violent Solution
Violent Solution
will OVERTIME.
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int result = INT32_MAX; // set a maxValue to result to compare with subL later
int sum = 0; // sum of subL
int subLength = 0; // Length of subL
for (int i = 0; i < nums.size(); i++) { // for loop from 0
sum = 0;//set sum=0
for (int j = i; j < nums.size(); j++) { // set j=i location,for loop j and make them sum
sum += nums[j];
if (sum >= s) { // Update result if that
subLength = j - i + 1; // length comes from the two pointers.
result = result < subLength ? result : subLength;//compare the result and subL
break; // stop the loop when got the key
}
}
}
// return 0 if key cannot be found
return result == INT32_MAX ? 0 : result;
}
};
2.sliding windows
Analysis: sliding windows is a special Double pointer.
We use one for loop by sliding windows to change the start and end of the subLength.
LeetCode C++sliding windows
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int result = INT32_MAX;
int sum = 0; // sum of sliding windows
int i = 0; // start of the sliding windows
int subLength = 0; // Length of SW
for (int j = 0; j < nums.size(); j++) {
sum += nums[j];
while (sum >= s) {//update the start of i under while,then compare them
subLength = (j - i + 1); // Length comes from the two pointers
result = result < subLength ? result : subLength;
sum -= nums[i++]; // change the location of i
}
}
// return 0 if key cannot be found
return result == INT32_MAX ? 0 : result;
}
};
3.Spiral Matrix II
LeetCode: 59. Spiral Matrix II
Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.
Solution
Analysis: It’s an easy simulation process without algorithm.
But it can exercise coding ability.
LeetCode C++Solution
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> res(n, vector<int>(n, 0)); // define a new two-dimensional array
int startx = 0, starty = 0; // define the start area
int loop = n / 2; //define how many times loop
int mid = n / 2; // define the location of the middle area
int count = 1; // assign every space
int offset = 1; // control the length of side length,shrink one space.
int i,j;
while (loop --) {
i = startx;
j = starty;
// for loop to simulate
// Upper side from L to R(L included R not included)
for (j = starty; j < n - offset; j++) {
res[startx][j] = count++;
}
// Right side from T to B(L included R not included)
for (i = startx; i < n - offset; i++) {
res[i][j] = count++;
}
// Bottom side from R to L(L included R not included)
for (; j > starty; j--) {
res[i][j] = count++;
}
// Left side from B to T(L included R not included)
for (; i > startx; i--) {
res[i][j] = count++;
}
// Plus one when second loop starts
startx++;
starty++;
// offset controls the length of the side length.
offset += 1;
}
// Assign the middle of the matrix if n=odd number
if (n % 2) {
res[mid][mid] = count;
}
return res;
}
};
Conclusion
Hard for me to understand the Spiral Matrix.
Code review again later.
keep reviewing code day by day!