Skip to content

Instantly share code, notes, and snippets.

View zsh-89's full-sized avatar
🤒
Out sick

zsh zsh-89

🤒
Out sick
View GitHub Profile
@zsh-89
zsh-89 / Valid Number.cpp
Created October 22, 2013 11:05
Leetcode: Valid Number
class Solution {
public:
bool isNumber(const char *s) {
if (!s) return false;
bool sign = true; bool num = false;
bool e = true; bool dot = true; int space = 0;
char c;
while (c=*s++) {
if (c != ' ' && space == 2) return false;
@zsh-89
zsh-89 / Copy List with Random Pointer.cpp
Created October 22, 2013 13:22
Leetcode: Copy List with Random Pointer
/**
* 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) {}
* };
*/
typedef RandomListNode Node;
class Solution {
@zsh-89
zsh-89 / Text Justification.cpp
Created October 24, 2013 12:27
Leetcode: Text Justification
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
int N = words.size(); vector<string> ans;
for (int i = 0; i < N;) {
int n = 1; int len = words[i].size();
for (int j = i+1; j < N; ++j) {
if (len + words[j].size() + 1 > L) break;
len += words[j].size() + 1;
++n;
@zsh-89
zsh-89 / 3Sum.cpp
Last active December 26, 2015 14:29
Leetcode: 3Sum
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
sort(num.begin(), num.end());
vector<vector<int> > ans;
int N = num.size();
int pre = INT_MIN;
for (int i = 0; i < N; ++i) {
if (num[i] == pre) continue;
@zsh-89
zsh-89 / Median of Two Sorted Arrays.cpp
Created October 27, 2013 10:12
Leetcode: Median of Two Sorted Arrays.
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int N = m+n;
if (N%2) return findKth(A, m, B, n, N/2+1);
else return (double(findKth(A, m, B, n, N/2)) + findKth(A, m, B, n, N/2+1))/2;
}
int findKth(int A[], int m, int B[], int n, int k) {
if (m > n) return findKth(B, n, A, m, k);
if (m==0) return B[k-1];
@zsh-89
zsh-89 / Merge Intervals.cpp
Created October 29, 2013 15:09
Leetcode: Merge Intervals
class Solution {
public:
struct InCmp {
bool operator()(const Interval & x, const Interval & y) const {
return x.start < y.start;
}
};
vector<Interval> merge(vector<Interval> &v) {
if (v.size() == 0) return v;
@zsh-89
zsh-89 / Insert Interval.cpp
Created October 30, 2013 16:59
Leetcode: Insert Interval
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
@zsh-89
zsh-89 / To the Max.cpp
Created October 31, 2013 16:36
poj: To the Max
#include <cstdio>
#include <string>
#include <iostream>
#include <map>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <climits>
@zsh-89
zsh-89 / Scramble String.cpp
Created November 2, 2013 17:42
Leetcode: Scramble String
class Solution {
public:
bool isScramble(string s1, string s2) {
int N = s1.size();
if (N != s2.size()) return false;
if (N == 0) return true;
init(N); s1_ = &s1; s2_ = &s2;
return judge(0, 0, N);
}
int main()
{
int n, k;
scanf("%d %d", &n, &k);
n *= 2;
for (int i = 1; i <= n; i+=2) {
if (k) {
--k;
printf("%d %d ", i, i+1);
} else