Algorithm learning 14 - finding numbers in a matrix with rows and columns in order & printing the common parts of two linked lists
Finding numbers in a matrix with ordered rows and columns
[title]
Given an integer matrix with N*M and an integer K, every row and every
The columns are all in order. Implement a function to determine whether K is in matrix.
For example:
0 1 2 5
2 3 4 7
4 4 4 8
5 7 7 9
Returns true if K is 7 and false if K is 6.
[request]
The time complexity is O(N+M), and the extra space complexity is O(1).
Algorithmic thinking
If it is equal to k, return true to indicate that it has been found
If it is larger than k, because every column of the matrix is arranged in order, the number below the current number in the column where the current number is located is larger than k, then it is unnecessary to continue searching on col column, let col = COL-1, repeat the steps
If it's smaller than k, because every column of the matrix is arranged in order, the number to the left of the current number in the row where the current number is located is smaller than k, then it's unnecessary to continue searching on row row, make row = row + 1, and repeat the steps
Algorithm code:
<isContanin.h>
#include<iostream> #include<vector> using namespace std; bool isContanin(vector<vector<int> >& matrix, int k){ int row = 0; int col = matrix[0].size() - 1; while(row < matrix.size() && col > -1) { if(matrix[row][col] == k) return true; else if(matrix[row][col] > k) --col; else ++row; } return false; };
<isContanin.cpp>
#include <iostream> #include <vector> #include"isContanin.h" using namespace std; int main() { int a[] = {0, 1, 2, 5}; vector<int> ivec(a, a + 4); vector<vector<int> > m; m.push_back(ivec); ivec[0] = 2; ivec[1] = 3; ivec[2] = 4; ivec[3] = 7; m.push_back(ivec); bool returnisContanin=isContanin(m, 7); cout <<returnisContanin<< endl; }
Print common parts of two linked lists
[title]
Given the head pointers head1 and head2 of two ordered linked lists, print the common parts of the two linked lists.
Algorithm idea:
Define two pointers, point to the head nodes of two linked lists respectively, compare the value value value, and the one who moves small; print equally and move at the same time, the next node of any node is null, and the program ends.
code implementation
<printComPart.h>
#include <iostream> using namespace std; struct Node { int value; Node *next; }; void printComPart(Node *head1,Node *head2) { cout << "Common Part: " << endl; while(NULL != head1 && NULL != head2) { if(head1->value < head2->value) head1 = head1->next; else if(head1->value > head2->value) head2 = head2->next; else { cout << head1->value << " " ; head1 = head1->next; head2 = head2->next; } } cout << endl; }
<printComPart.cpp>
#include <iostream> #include"printComPart.h" using namespace std; int main() { Node *head1 = NULL; Node *head2 = NULL; Node *ptr = NULL; for(int i =0;i<10;i++) { if(NULL == head1) { head1 = new Node; head1->value = i; head1->next = NULL; ptr = head1; continue; } ptr->next = new Node; ptr = ptr->next; ptr->value = i; ptr->next = NULL; } for(int i =3;i<23;i++) { if(NULL == head2) { head2 = new Node; head2->value = i; head2->next = NULL; ptr = head2; continue; } ptr->next = new Node; ptr = ptr->next; ptr->value = i; ptr->next = NULL; } printComPart(head1,head2); return 0; }