Skip to content

houserobber #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
74 changes: 74 additions & 0 deletions compareVersionNumber/compareVersionNumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Source : https://oj.leetcode.com/problems/compare-version-numbers/

/**********************************************************************************
*
* Compare two version numbers version1 and version1.
* If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
*
* You may assume that the version strings are non-empty and contain only digits and the . character.
* The . character does not represent a decimal point and is used to separate number sequences.
* For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
*
* Here is an example of version numbers ordering:
* 0.1 < 1.1 < 1.2 < 13.37
*
* Credits:Special thanks to @ts for adding this problem and creating all test cases.
*
**********************************************************************************/

class Solution {
public:
vector<int> &split(const string &s, char delim, vector<int> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(atoi(item.c_str()));
}
return elems;
}


vector<int> split(const string &s, char delim) {
vector<int> elems;
split(s, delim, elems);
return elems;
}

void rightTrimZero(vector<int> &v){
while (v.back() == 0){
v.pop_back();
}
}

int compareVersion(string version1, string version2) {

//split the version by delimer '.'
vector<int> ver1 = split(version1, '.');
vector<int> ver2 = split(version2, '.');

//remove the right Zeros
rightTrimZero(ver1);
rightTrimZero(ver2);

//compare two versions
for (int i=0; i<ver1.size() && i < ver2.size(); i++) {
if (ver1[i] < ver2[i]){
return -1;
}else if(ver1[i] > ver2[i]){
return 1;
}
}

//if the above for-loop is not returned, which means they are equal so far
//then check the length.
if (ver1.size() > ver2.size()) {
return 1;
}else if (ver1.size() < ver2.size()) {
return -1;
}

return 0;

}

};
186 changes: 186 additions & 0 deletions copyListWithRandomPointer/copyListWithRandomPointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// Source : https://oj.leetcode.com/problems/copy-list-with-random-pointer/

/**********************************************************************************
*
* A linked list is given such that each node contains an additional random pointer
* which could point to any node in the list or null.
*
* Return a deep copy of the list.
*
*
**********************************************************************************/



/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/


/*
*
* The idea as below:
*
* Consider we have a linked list as below:
*
* node1->random = node2;
* node2->random = node1;
* node3->random = node1;
*
* +-------------+
* | v
* +-------+ +-------+ +-------+
* | node1 |----> node2 |----> node3 |--->NULL
* +-------+ +-------+ +-------+
* ^ ^ | |
* | +-----------+ |
* +--------------------------+
*
*
* To copy the list,
*
* 1) We insert a new node for each node's next position
*
*
* +-------------------------+
* | v
* +--+----+ +-----+ +-------+ +-----+ +-------+ +-----+
* | node1 |---> | NEW |----> node2 |---> | NEW |----> node3 |---> | NEW | ---> NULL
* +-------+ +-----+ +---+---+ +-----+ +--+----+ +-----+
* ^ ^ | |
* | +-----------------------+ |
* +--------------------------------------------------+
*
* 2) Then, we can construt the new node's random pointer:
*
* (node1->next) -> random = (node1->random) -> next;
*
* 3) After we take out all of the "NEW" node to finish the copy.
*
*/

class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
RandomListNode *p = NULL, *h=NULL, *t=NULL;
if (head == NULL){
return NULL;
}

//creat a new node for each node and insert its next
p = head;
while ( p != NULL){
RandomListNode *node = new RandomListNode(p->label);
node->next = p->next;
p->next = node;
p = node->next;
}

//copy random pointer for each new node
p = head;
while (p != NULL){
if (p->random != NULL){
p->next->random = p->random->next;
}
p = p->next->next;
}

//break to two list
p = head;
h = t = head->next;
while ( p != NULL ){
p->next = p->next->next;
if (t->next!=NULL){
t->next = t->next->next;
}

p = p->next;
t = t->next;
}

return h;
}
};


/*
* Considering we have a link as below:
*
*
* +-------------+
* | v
* +-------+ +-------+ +-------+
* | node1 |----> node2 |----> node3 |--->NULL
* +-------+ +-------+ +-------+
* ^ ^ | |
* | +-----------+ |
* +--------------------------+
*
* 1) Using a map to store each node's random pointer's position
*
* map[node1->random] = 1;
* map[node2->random] = 0;
* map[node3->random] = 0;
*
* 2) Clone the linked list (only consider the next pointer)
*
* new1 --> new2 --> new3 --> NULL
*
* 3) Using an array to strore the order of the cloned linked-list
*
* v[0] = &new1
* v[1] = &new2
* v[2] = &new3
*
* 4) Then we can clone the random pointers.
*
* new->random = v [ map[node->random] ]
*
*/
class MySolution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {

RandomListNode *p = NULL, *h=NULL, *t=NULL;
//using a map to store the random pointer's postion.
map<RandomListNode*, int> m;
//construct the map
int pos =0;
for ( p = head; p != NULL; p = p->next, pos++){
m[p] = pos;
}

//clone the linked list (only consider the next pointer)
//and using a vector to store each node's postion.
vector<RandomListNode*> v;
for (p = head; p != NULL; p = p->next){
RandomListNode *node = new RandomListNode(p->label);
v.push_back(node);
if (h==NULL){
h = t = node;
}else{
t->next = node;
t = node;
}
}

//p => source link head
//t => new link head
//move the p and t synchronously, and
// t->random = vector[ map[p->random] ];
for (t=h, p = head; t!=NULL && p!= NULL; p=p->next, t=t->next){
if (p->random!=NULL) {
pos = m[p->random];
t->random = v[pos];
}
}

return h;

}
};
Loading