Skip to content

Instantly share code, notes, and snippets.

@wfwei
wfwei / gist:5939767
Created July 6, 2013 12:34
URL Regex Pattern
// Protocol : //[user: password]@host[:port]/path /[?query][#fragment]
public static Pattern UrlPatt = Pattern
.compile(
"(?<protocol>.*?)://(?<loginfo>(?<user>.*?):(?<pwd>.*?)@)?(?<host>[^/]+)(?:(?<path>/[^\\?#]*)(?<query>\\?[^#]+)?)?(?<frag>#.*)?",
Pattern.CASE_INSENSITIVE);
public static void testUrlPatt() {
String[] urls = {
"https://www.google.com.hk/search?q=named+group+regex+java&oq=named+group+regex+java&aqs=chrome.0.57.5777j0&sourceid=chrome&ie=UTF-8#abc",
"http://wfwei.github.io/posts/regex/#abc",
@wfwei
wfwei / gist:5875335
Created June 27, 2013 09:58
java容器,遍历的同时删除元素
ArrayList<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
System.out.println(list.size());
// This will fail
// for (String item : list) {
// list.remove(item);
// }
Iterator<String> iter = list.iterator();
@wfwei
wfwei / gist:5856407
Created June 25, 2013 06:29
Hashable & Comparable
static class WordToName implements Comparable<WordToName> {
private String word;
private List<String> docNames;
public WordToName(String word) {
this.word = word;
this.docNames = new ArrayList<String>();
}
@Override
@wfwei
wfwei / gist:5830013
Created June 21, 2013 09:21
Windows命令行链接和断开vpn
public static void changIP() {
try {
System.out.println("Disconnecting...");
Runtime.getRuntime().exec("rasdial myVpn /disconnect");
System.out.println("Sleep for 10 seconds");
Thread.sleep(10000);
System.out.println("Connectiong..");
Runtime.getRuntime().exec("rasdial myVpn wfwei wangfengwei");
System.out.println("Over");
} catch (Exception e) {
@wfwei
wfwei / gist:5791854
Created June 16, 2013 12:08
User rand7 to rand10
#include<stdio.h>
#include<stdlib.h>
// return random num:0-6
int Rand7(){
return rand()%7;
}
int Rand10(){
int rand71, rand72, rand10;
@wfwei
wfwei / gist:5764443
Created June 12, 2013 11:11
一个数组里,除了三个数是唯一出现的,其余的都出现偶数个,找出这三个数中的任一个。比如数组元素为【1, 2,4,5,6,4,2】,只有1,5,6这三个数字是唯一出现的,我们只需要输出1,5,6中的一个就行
#include<stdio.h>
#define bitVal(n, i) (((n) & (1 << (i)))>0?1:0)
void findTwoSingleNum(int *A, int len, int fiterBit, int filterVal, int AorB){
int i, j, Aval=0, Bval=0;
for(j=0; j<sizeof(int); j++){
if(bitVal(AorB, j))
break;
}
for(i=0; i<len; i++){
@wfwei
wfwei / gist:5735308
Created June 8, 2013 14:15
判断一个数字序列是BST后序遍历的结果
#include "stdio.h"
#include "stdlib.h"
// 判断一个数字序列是BST后序遍历的结果
// 认为没有重复元素
bool isBST(int *A, int s, int e){
if(A==NULL)
return 0;
bool bst = true;
int root = A[e], ls, le, rs, re;
rs = e; re = e-1;
@wfwei
wfwei / gist:5735158
Last active December 18, 2015 05:49
求旋转数组的最小元素(把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个排好序的数组的一个旋转,输出旋转数组的最小元素。例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5}的一个旋转,该数组的最小值为1)
#include "stdio.h"
#include "stdlib.h"
int getMaxIdx(int *A, int len){
int low=0, high=len-1, mid;
while(low<high){
mid = low + (high-low + 1)/2;
if(A[mid]>A[low])
low = mid;
else
@wfwei
wfwei / gist:5735100
Created June 8, 2013 12:55
归并有序数组
#include "stdio.h"
#include "stdlib.h"
int * merge(int*A, int *B, int len){
int *C = (int *)malloc(sizeof(int)*len*2);
int ai, bi, ci;
if(A==NULL || B==NULL || C==NULL)
return NULL;
for(ai=0, bi=0, ci=0; ai<len&&bi<len; ){
if(A[ai]<=B[bi])
@wfwei
wfwei / gist:5730544
Last active December 18, 2015 05:09
24点--可扩展N点 http://zh.wikipedia.org/wiki/24%E7%82%B9 随机的取出四张扑克牌(大小王去掉), 第一个用所有四张牌上的数值(A = 1, J = 11, Q = 12, K = 13),和基本的四则运算(+ - × , /) 算出24(或者23.999..)的玩家获胜
#include<stdio.h>
#include<math.h>
#define THRESHOLD 0.0000001
bool found = false;
void swap(double *a, double *b){
double tmp = *a;
*a = *b;