Skip to content

Instantly share code, notes, and snippets.

@chuanying
chuanying / LRUCache.cc
Created November 12, 2013 12:50
LRUCache
class LRUCache{
public:
LRUCache(int capacity) {
total = capacity;
current = 0;
}
int get(int key) {
auto it = m.find(key);
if (it == m.end()) {
@chuanying
chuanying / single_read_write_queue.cc
Last active December 27, 2015 22:39
生产者与消费者(单链表),一个单链表,有个表头和表尾,一个线程作为消费者每次从表头去一个元素,另一个线 程作为生产者每次从表尾加一个元素,不加锁如何实现?
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <list>
#include <map>
#include <thread>
#include <algorithm>
#include <stdio.h>
@chuanying
chuanying / WordBreakII.cc
Created October 12, 2013 11:05
WordBreakII.cc in leetcode.com
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict) {
vector<bool> dp(s.length() + 1);
map<int, vector<int> > trace;
dp[0] = true;
for (int b = 0; b < s.length(); ++b) {
for (int e = b + 1; dp[b] && e <= s.length(); ++e) {
if (dict.count(s.substr(b, e - b))) {
dp[e] = true;
@chuanying
chuanying / WordLadderII.cc
Created October 12, 2013 09:42
WordLadderII.cc leetcode
class Solution {
public:
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
vector<vector<string> > result;
unordered_set<string> visited;
unordered_map<string, vector<string> > traces;
unordered_set<string> curr;
curr.insert(start);
dict.insert(end);
visited.insert(start);
@chuanying
chuanying / 4sum.cc
Last active December 24, 2015 14:09
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
class Solution { //O(n^2) but use set to remove duplicate result
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
// Note: The Solution object is instantiated only once and is reused by each test case.
sort(num.begin(), num.end());
map<int, vector<pair<int, int> > > cache;
for (int i = 0; i < num.size(); ++i) {
for (int j = i + 1; j < num.size(); ++j) {
cache[ num[i] + num[j] ].push_back(pair<int, int>(i, j));
}
#define pp pair<int, char>
int main() {
int m, weight;
cin >> m;
int ans = INT_MAX;
char ansc;
map<char, int> distance;
map<char, map<char, int> > graph;
@chuanying
chuanying / find_number.cc
Last active December 23, 2015 23:49
[Microsoft] How to find if a number is present equal to or more than n/2 times in an array of size n?
/* Error!!
void helper(const vector<int> &vec, int pos, pair<int, int> &curr) {
for (int i = pos; i < vec.size(); ++i) {
if (curr.second == 0 || curr.first == vec[i] ) {
curr = make_pair(vec[i], curr.second + 1);
} else {
--curr.second;
}
}
@chuanying
chuanying / SimplifyPath.cc
Created September 25, 2013 03:59
Simplify Path
// 1.
class Solution {
public:
string simplifyPath(string path) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> vec;
int pos = path.find('/');
while (pos != string::npos && pos < path.length()) {
@chuanying
chuanying / WordLadderII.cpp
Created September 24, 2013 12:35
Word Ladder II
class Solution {
public:
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
vector<vector<string> > result;
unordered_set<string> visited;
unordered_map<string, vector<string> > traces;
unordered_set<string> curr;
curr.insert(start);
dict.insert(end);
visited.insert(start);
@chuanying
chuanying / string split.cc
Created September 10, 2013 05:30
string split.cc 序列化多个字符串, 网络传到另一台机器, 再反序列化他
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stdio.h>
#include <math.h>