Skip to content

Instantly share code, notes, and snippets.

@wfwei
wfwei / sqrt.java
Created August 20, 2013 00:39
Leetcode#69 从sqrt看二分查找的上下界问题
public class Solution {
static final int MAX = 46340;
public int sqrt(int x) {
int s = 1, e = x;
if(e>MAX)
e = MAX;
@wfwei
wfwei / traprain.cpp
Created August 17, 2013 07:17
Leetcode#42 Trapping Rain Water
class Solution {
public:
int trap(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int s = 0, e = n - 1;
int curl = 0, max = 0, sum = 0;
while (s < e) {
while (s < e && A[s] <= curl) {
sum += A[s];
@wfwei
wfwei / GenFunc.java
Last active December 21, 2015 04:28
母函数(generative function)
import java.util.Arrays;
public class GenFunc {
/**
* 求用不同面值的硬币组合出数值为sum的方案数,每种硬币可以用任意个
* */
public int genFunc(int[] coins, int sum) {
int[] state = new int[sum + 1];
state[0] = 1;
@wfwei
wfwei / next_permutation.cpp
Created August 15, 2013 07:48
LeetCode#31 next_permutation
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void nextPermutation(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
@wfwei
wfwei / Solution.cpp
Created August 14, 2013 04:01
LeetCode#29 Divide two integers without using multiplication, division and mod operator.
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
@wfwei
wfwei / MergeKLists.java
Created August 13, 2013 10:25
Leetcode#23 k个有序数组进行归并排序,时间复杂度nlogK
import java.util.ArrayList;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
@wfwei
wfwei / GenerateParenthesis.java
Created August 13, 2013 07:28
生成合法的括号序列,Leetcode#22
import java.util.ArrayList;
public class GenerateParenthesis {
public ArrayList<String> generateParenthesis(int n) {
ArrayList<String> results = new ArrayList<String>();
gp(n, n, new StringBuilder(), results);
return results;
}
@wfwei
wfwei / RomanPro.java
Last active December 20, 2015 22:59
Leetcode Roman to Integer and reverse...
import java.util.Comparator;
import java.util.HashMap;
import java.util.TreeMap;
public class Test {
static class IntToRoman {
public int romanToInt(String s) {
int res = 0;
@wfwei
wfwei / gist:6161773
Created August 6, 2013 03:27
Leetcode Median of Two Sorted Arrays http://leetcode.com/onlinejudge#question_4
//============================================================================
// Name : Coding.cpp
// Author : Plex
// Version : 1.0
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include<iostream>
using namespace std;
@wfwei
wfwei / gist:5982772
Created July 12, 2013 08:21
how many ways to reach (ex, ey) from (sx, sy), can only go down or left on each node
#include<stdio.h>
int count = 0;
void func(int sx, int sy, int ex, int ey){
if(sx<ex)
func(sx+1, sy, ex, ey);
if(sy<ey)
func(sx, sy+1, ex, ey);
if(sx==ex && sy==ey)