GithubHelp home page GithubHelp logo

algorithm's Introduction

Algorithm

Learning and implementating some well-known algorithms

Finding a matching num in a partial ordered array

7 is in such a matrix, and return True.
1 2 8 9
2 4 9 12
4 7 10 13
6 8 11 15

class Solution {
public:
    bool Find(int target, vector<vector<int> > array) {
        bool found=false;
        if(!array.empty() && array.size()>0 && array[0].size()>0){
            int row=0;
            int column=array[0].size()-1;
            while(row<array.size() && column>-1){
                if(array[row][column] == target){
                    found=true;break;
                }
                else
                {
                    if(array[row][column]>target)
                        --column;
                    else
                        ++row;
                }
            }
        }
	return found;
    }
};
int a1[] = { 1, 2, 8, 9, };
int a2[] = { 2, 4, 9, 12, };
int a3[] = { 4, 7, 10, 13, };
int a4[] = { 6, 8, 11, 15, };
vector<vector<int>> array;
array.push_back(vector<int>(a1, a1 + 4));
array.push_back(vector<int>(a2, a2 + 4));
array.push_back(vector<int>(a3, a3 + 4));
array.push_back(vector<int>(a4, a4 + 4));
Solution solu;
bool a = solu.Find(1,array);
cout << "result:" << a << endl;

Print list from tail to head

Return a vector, such as 2->4->7->15, return [15,7,4,2]

class Solution {
public: 
    vector<int> res;
    vector<int> printListFromTailToHead(ListNode* head) {
        if(head!=NULL){
            if(head->next !=NULL){
            //when head point reaches to the tail, head->next == NULL,and begains to return
                printListFromTailToHead(head->next);}
                // The point begains to return, when head->next=NULL, res[0]=head->val
                res.push_back(head->val); 
                //printf("%d\t",head->val);
            //}  
        }
        return res;
    }
};

Check the number of 1 in a binary data

class Solution {
public:
     int  NumberOf1(int n) {
         int num=0;
         while(n){
             n &= (n-1);
             num++;
         }
         return num;
     }
};

algorithm's People

Contributors

lingkangjie avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.