Skip to content

Instantly share code, notes, and snippets.

class Solution {
public:
bool isNumber(const char *s) {
enum InputType{
INVALID,//0
SPACE,//1
DIGIT,//2
SIGN,//3
DOT,//4
EXPONET,//5
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int total=m+n;
if(total&1)
return findKth(A,m,B,n,total/2+1);
else
return (findKth(A,m,B,n,total/2)+findKth(A,m,B,n,total/2+1))/2;
}
class Solution {
public:
void sortColors(int A[], int n) {
int i=-1;
int j=n;
int cur=0;
while(cur<j)
{
if(A[cur]==0)swap(A[++i],A[cur++]);
else if(A[cur]==2)swap(A[cur],A[--j]);
class Solution {
public:
vector<vector<int> > result;
vector<vector<int> > permute(vector<int> &num) {
permute(num,0);
return result;
}
void permute(vector<int> &num,int index)
{
class Solution {
public:
vector<vector<int> > result;
vector<vector<int> > permuteUnique(vector<int> &num) {
permute(num,0);
return result;
}
void permute(vector<int> &num,int index)
{
class Solution {
public:
void nextPermutation(vector<int> &num) {
for(int i=num.size()-1;i-1>=0;--i)
{
if(num[i]>num[i-1])//找到最大的降序后缀 1[2]876530
{
for(int j=num.size()-1;j>=i;--j)
{
class Solution {
public:
string getPermutation(int n, int k) {
string re(n,'0');
int fac=factorial(n);
vector<int> vecNum(n,0);
for(int i=1;i<=vecNum.size();++i)vecNum[i-1]=i;
--k;// k=[0,(n-i)! ) ,inorder to get the right index
for(int i=0;i<n;++i)
{
@HaiyangXu
HaiyangXu / CountInversions .cpp
Last active August 29, 2015 14:01
Algorithms: Design and Analysis, Part 1 Programming Question-1
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
unsigned int merge(vector<int>&nums,int begin,int middle,int end)
{
vector<int> left(nums.begin()+begin,nums.begin()+middle);
vector<int> right(nums.begin()+middle,nums.begin()+end);
@HaiyangXu
HaiyangXu / Server.py
Created May 18, 2014 14:00
A simper python http server can handle mime type properly
# -*- coding: utf-8 -*-
#test on python 3.4 ,python of lower version has different module organization.
import http.server
from http.server import HTTPServer, BaseHTTPRequestHandler
import socketserver
PORT = 8080
Handler = http.server.SimpleHTTPRequestHandler
@HaiyangXu
HaiyangXu / os.asm
Last active August 29, 2015 14:01
A simple operating system or say, just a simple boot loader
;Code from http://mikeos.berlios.de/write-your-own-os.html
;See my cool solution to boot this 'OS‘,i think it's much cool.
; http://haiyangxu.github.io/posts/2014/2014-05-21-write_a_bootloader
BITS 16
start:
mov ax, 07C0h ; Set up 4K stack space after this bootloader
add ax, 288 ; (4096 + 512) / 16 bytes per paragraph
mov ss, ax