Skip to content

Instantly share code, notes, and snippets.

@mogutou1214
mogutou1214 / gist:5581704
Created May 15, 2013 04:50
Careercup1.1: Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?
/*
* test.cpp
*Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?
* Created on: 2013-5-4
* Author: Fred
*/
#include <iostream>
#include <string>
@mogutou1214
mogutou1214 / gist:5589307
Created May 16, 2013 04:04
Careercup1.2 - Given two strings, write a method to decide if one is a permutation of the other.
/*
* test.cpp
Given two strings, write a method to decide if one is a permutation of the other.
* Created on: 2013-5-5
* Author: Fred
*/
#include <iostream>
#include <string>
using namespace std;
@mogutou1214
mogutou1214 / gist:5675756
Last active December 17, 2015 21:38
Careercup1.5 - Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2b1c5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string conv2str(int num){
ostringstream ostr; //output string stream
ostr << num; //use the string stream just like cout,
return ostr.str();
}
@mogutou1214
mogutou1214 / gist:6383668
Created August 29, 2013 21:31
Careercup 1.3 - Given two strings, write a method to decide if one is a permutation of the other. The method is to sort two strings using C++ library and compare the two sorted strings. Complexity is O(NlogN).
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
bool ifanagram(string str1,string str2){
if(str1.length()!=str2.length()) return false;
sort(str1.begin(),str1.end());
@mogutou1214
mogutou1214 / gist:6408080
Last active November 7, 2016 20:09
Careercup 2.1 - Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed? Note: 1. pay special attention to deleting a node 2. learn to use "map" in C++ STL as a hash table
/*Remove duplicates from an unsorted linked list - cc2.1*/
void removeDuplicate1(node *head){
map<int,bool> table;
node *curr = head;
node *pre = NULL;
while(curr!=NULL){
/*delete the node if it already exists in the map*/
if(table[curr->data]){
pre->next = curr->next;
delete curr;
@mogutou1214
mogutou1214 / gist:6415251
Created September 2, 2013 17:25
Careercup 2.3 - Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node EXAMPLE Input: the node ‘c’ from the linked list a->b->c->d->e Result: nothing is returned, but the new linked list looks like a->b->d->e
void delGivenNode(node *entry){
entry->data = (entry->next)->data;
entry->next = (entry->next)->next;
delete entry->next;
}
@mogutou1214
mogutou1214 / gist:6418613
Created September 3, 2013 01:03
Careercup 2.7 - Implement a function to check if a linked list is a palindrome. use a stack.
bool isPalindrome(node *head){
if(head==NULL) return false;
node *curr = head;
stack<int> s;
while(curr!=NULL){
s.push(curr->data);
curr = curr->next;
}
/*while(!s.empty()){
@mogutou1214
mogutou1214 / careercup3.2
Created September 9, 2013 00:15
Careercup 3.2 - How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time. 1. how to use stack in STL; 2. how to throw an exception;
#include <iostream>
#include <stack>
using namespace std;
class MyException{
public:
MyException(){cout << "the stack is empty" << endl;}
};
@mogutou1214
mogutou1214 / linkedlist.cpp
Last active December 23, 2015 11:38
C++ Linked list
// Linkedlist.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct node{
int data;
@mogutou1214
mogutou1214 / linkedlistnew.cpp
Created September 19, 2013 21:36
C++ Linked list-generic
// Linkedlist.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
template <class T>
struct node{
T data;