Skip to content

Instantly share code, notes, and snippets.

@sturgle
sturgle / NioTcpClient.java
Created September 29, 2014 08:29
A java nio tcp client. Keep it here as notes.
// TCP Client (Java NIO)
// A minimalistic Java NIO TCP client. Stays always connected. Disconnects and reconnects on any exception in the event handlers (onConnect, onDisconnect, onRead).
package net.bobah.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
#include <vector>
#include <string>
#include <iostream>
#include <cassert>
using namespace std;
/*
BigInt multiply. Use uchar array to store the binary representation.
1. It should be unsigned char (not char);
2. Use carry, and do multiply/add iteratively, this could avoid overflow;
/*
https://www.hackerrank.com/contests/w5/challenges/kundu-and-tree
*/
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 100010
@sturgle
sturgle / getEd2kLinks.py
Last active August 29, 2015 13:58
Get Ed2k links from a web page
@sturgle
sturgle / Crawler.py
Last active August 29, 2015 13:58
A crawler to get books on douban, but it seems not working as douban will block it after a while.
#!/usr/bin/env python
# coding=utf-8
from sys import argv
from string import replace,find,lower
from htmllib import HTMLParser
from urllib import urlretrieve, urlopen
from urlparse import urlparse,urljoin
from formatter import DumbWriter,AbstractFormatter
from cStringIO import StringIO
@sturgle
sturgle / TurnToList.cpp
Last active August 29, 2015 13:57
TurnFileWithNumberToList
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int _val) {
val = _val;
/*
http://www.careercup.com/question?id=15645665
Find the k'th largest element in a binary search tree.
*/
#include <stack>
#include <iostream>
using namespace std;
struct Node {
int val;
/*
http://www.careercup.com/question?id=9820788
there is a pyramid with 1 cup at level , 2 at level 2 , 3 at level 3 and so on..
It looks something like this
1
2 3
4 5 6
every cup has capacity C. you pour L liters of water from top . when cup 1 gets filled , it overflows to cup 2,3 equally, and when they get filled , Cup 4 and 6 get water only from 2 and 3 resp but 5 gets water from both the cups and so on.
Now given C and M .Find the amount of water in ith cup.
/*
http://www.careercup.com/question?id=23594662
Given a sequence of numbers A(1) ..A(n), find the continuous subsequenceA(i)..A(j) for which the sum of elements is maximum.
condition: we should not select two contiguous numbers
Solution:
Dp: F[i] = max(A[i], A[i] + F[i-2], F[i-1])
*/
#include <vector>
#include <iostream>
/*
http://www.careercup.com/question?id=12718665
String Reduction
Given a string consisting of a,b and c's, we can perform the following operation: Take any two adjacent distinct characters and replace it with the third character. For example, if 'a' and 'c' are adjacent, they can replaced with 'b'. What is the smallest string which can result by applying this operation repeatedly?
Sample Input:
cab
bcab
ccccc