Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions Topic1_Arrays/Day2207/AssignmentsHomework/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//Using two loop to solve problem. That way is save memory so much but i think it isn't the best solution.

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector <int> TwoNumber;
for(int i = 0; i < nums.size(); i++)
{
for(int j = i + 1; j < nums.size(); j++)
{
if(nums[i] + nums[j] == target)
{
TwoNumber.push_back(i);
TwoNumber.push_back(j);
break;
}
}
if(TwoNumber.size() == 2) break;
}
return TwoNumber;
}
};
36 changes: 36 additions & 0 deletions Topic1_Arrays/Day2207/AssignmentsHomework/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Using two loop to get each element in vector and check if this is '1', i will use Try function to delete all character beside that.
// But this is a common way and not quickly and not efficiently
class Solution {
public:
void Try(int i, int j, vector<vector<char>> &temp){
if(i + 1 < temp.size() && temp[i + 1][j] == '1') {
temp[i + 1][j] = '0';
Try(i + 1, j, temp);
}
if(i - 1 >= 0 && temp[i - 1][j] == '1') {
temp[i - 1][j] = '0';
Try(i - 1, j, temp);
}
if(j - 1 >= 0 && temp[i][j - 1] == '1') {
temp[i][j - 1] = '0';
Try(i, j - 1, temp);
}
if(j + 1 < temp[i].size() && temp[i][j + 1] == '1') {
temp[i][j + 1] = '0';
Try(i, j + 1, temp);
}
}

int numIslands(vector<vector<char>>& grid) {
int count = 0;
for(int i = 0; i < grid.size(); i++){
for(int j = 0; j < grid[i].size(); j++){
if(grid[i][j] == '1'){
count ++;
Try(i, j, grid);
}
}
}
return count;
}
};
29 changes: 29 additions & 0 deletions Topic1_Arrays/Day2207/AssignmentsHomework/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Using the way same as Merge Sort to solve the problem.
// I think that is the popular solution.

class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
vector <int> temp_nums1(nums1);
int indexNums1 = 0, indexNums2 = 0, index_res = 0;
while(indexNums1 < m && indexNums2 < n)
{
if(temp_nums1[indexNums1] > nums2[indexNums2]){
nums1[index_res] = nums2[indexNums2];
indexNums2++;
}
else {
nums1[index_res] = temp_nums1[indexNums1];
indexNums1++;
}
index_res++;
}
while(indexNums1 < m){
nums1[index_res++] = temp_nums1[indexNums1++];
}
while(indexNums2 < n){
nums1[index_res++] = nums2[indexNums2++];
}
}

};