当前位置: 互联网>综合
本页文章导读:
▪工具类之二:RegexpUtils
package com.test.core.util;
import org.apache.log4j.Logger;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.Pa.........
▪Coding Interview 8.2 1、一个m*n的矩阵a[][],机器人从左上角走到右下角,只能朝右或朝下走,输出所有路径。
2、如果矩阵有的格子可以走,有的格子不可以走,输出所有路径。(a[i][j]==1表示可以走,a[i][j].........
▪Coding Interview 8.5 输出n对括号的所有有效组合。
思路:
运用递归的思想。只要左括号没有用完,总可以插入左括号;当已插入的左括号数目大于右括号,那么就可以插入右括号。我们只要记录左右括号剩余的.........
[1]工具类之二:RegexpUtils
来源: 互联网 发布时间: 2013-10-26
package com.test.core.util;
import org.apache.log4j.Logger;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
public final class RegexpUtils {
private static Logger log = Logger.getLogger(RegexpUtils.class);
private RegexpUtils() {
}
/**
* 匹配图象
*
*
* 格式: /相对路径/文件名.后缀 (后缀为gif,dmp,png)
*
* 匹配 : /forum/head_icon/admini2005111_ff.gif 或 admini2005111.dmp
*
*
* 不匹配: c:/admins4512.gif
*
*/
public static final String ICON_REGEXP = "^(/{0,1}//w){1,}//.(gif|dmp|png|jpg)$|^//w{1,}//.(gif|dmp|png|jpg)$";
/**
* 匹配email地址
*
*
* 格式: XXX@XXX.XXX.XX
*
* 匹配 : foo@bar.com 或 foobar@foobar.com.au
*
* 不匹配: foo@bar 或 $$$@bar.com
*
*/
public static final String EMAIL_REGEXP = "(?://w[-._//w]*//w@//w[-._//w]*//w//.//w{2,3}$)";
/**
* 匹配匹配并提取url
*
*
* 格式: XXXX://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX
*
* 匹配 : http://www.suncer.com 或news://www
*
* 不匹配: c:/window
*
*/
public static final String URL_REGEXP = "(//w+)://([^/:]+)(://d*)?([^#//s]*)";
/**
* 匹配并提取http
*
* 格式: http://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX 或 ftp://XXX.XXX.XXX 或
* https://XXX
*
* 匹配 : http://www.suncer.com:8080/index.html?login=true
*
* 不匹配: news://www
*
*/
public static final String HTTP_REGEXP = "(http|https|ftp)://([^/:]+)(://d*)?([^#//s]*)";
/**
* 匹配日期
*
*
* 格式(首位不为0): XXXX-XX-XX或 XXXX-X-X
*
*
* 范围:1900--2099
*
*
* 匹配 : 2005-04-04
*
*
* 不匹配: 01-01-01
*
*/
public static final String DATE_BARS_REGEXP = "^((((19){1}|(20){1})\\d{2})|\\d{2})-[0,1]?\\d{1}-[0-3]?\\d{1}$";
/**
* 匹配日期
*
*
* 格式: XXXX/XX/XX
*
*
* 范围:
*
*
* 匹配 : 2005/04/04
*
*
* 不匹配: 01/01/01
*
*/
public static final String DATE_SLASH_REGEXP = "^[0-9]{4}/(((0[13578]|(10|12))/(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)/(0[1-9]|[1-2][0-9]|30)))$";
/**
* 匹配电话
*
*
* 格式为: 0XXX-XXXXXX(10-13位首位必须为0) 或0XXX XXXXXXX(10-13位首位必须为0) 或
*
* (0XXX)XXXXXXXX(11-14位首位必须为0) 或 XXXXXXXX(6-8位首位不为0) 或
* XXXXXXXXXXX(11位首位不为0)
*
*
* 匹配 : 0371-123456 或 (0371)1234567 或 (0371)12345678 或 010-123456 或
* 010-12345678 或 12345678912
*
*
* 不匹配: 1111-134355 或 0123456789
*
*/
public static final String PHONE_REGEXP = "^(?:0[0-9]{2,3}[-//s]{1}|//(0[0-9]{2,4}//))[0-9]{6,8}$|^[1-9]{1}[0-9]{5,7}$|^[1-9]{1}[0-9]{10}$";
/**
* 匹配身份证
*
* 格式为: XXXXXXXXXX(10位) 或 XXXXXXXXXXXXX(13位) 或 XXXXXXXXXXXXXXX(15位) 或
* XXXXXXXXXXXXXXXXXX(18位)
*
* 匹配 : 0123456789123
*
* 不匹配: 0123456
*
*/
public static final String ID_CARD_REGEXP = "^//d{10}|//d{13}|//d{15}|//d{18}$";
/**
* 匹配邮编代码
*
* 格式为: XXXXXX(6位)
*
* 匹配 : 012345
*
* 不匹配: 0123456
*
*/
public static final String ZIP_REGEXP = "^[0-9]{6}$";// 匹配邮编代码
/**
* 不包括特殊字符的匹配 (字符串中不包括符号 数学次方号^ 单引号' 双引号" 分号; 逗号, 帽号: 数学减号- 右尖括号> 左尖括号< 反斜杠/
* 即空格,制表符,回车符等 )
*
* 格式为: x 或 一个一上的字符
*
* 匹配 : 012345
*
* 不匹配: 0123456 // ;,:-<>//s].+$";//
*/
public static final String NON_SPECIAL_CHAR_REGEXP = "^[^'/";
// 匹配邮编代码
/**
* 匹配非负整数(正整数 + 0)
*/
public static final String NON_NEGATIVE_INTEGERS_REGEXP = "^//d+$";
/**
* 匹配不包括零的非负整数(正整数 > 0)
*/
public static final String NON_ZERO_NEGATIVE_INTEGERS_REGEXP = "^[1-9]+//d*$";
/**
*
* 匹配正整数
*
*/
public static final String POSITIVE_INTEGER_REGEXP = "^[0-9]*[1-9][0-9]*$";
/**
*
* 匹配非正整数(负整数 + 0)
*
*/
public static final String NON_POSITIVE_INTEGERS_REGEXP = "^((-//d+)|(0+))$";
/**
*
* 匹配负整数
*
*/
public static final String NEGATIVE_INTEGERS_REGEXP = "^-[0-9]*[1-9][0-9]*$";
/**
*
* 匹配整数
*
*/
public static final String INTEGER_REGEXP = "^-?//d+$";
/**
*
* 匹配非负浮点数(正浮点数 + 0)
*
*/
public static final String NON_NEGATIVE_RATIONAL_NUMBERS_REGEXP = "^//d+(//.//d+)?$";
/**
*
* 匹配正浮点数
*
*/
public static final String POSITIVE_RATIONAL_NUMBERS_REGEXP = "^(([0-9]+//.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*//.[0-9]+)|([0-9]*[1-9][0-9]*))$";
/**
*
* 匹配非正浮点数(负浮点数 + 0)
*
*/
public static final String NON_POSITIVE_RATIONAL_NUMBERS_REGEXP = "^((-//d+(//.//d+)?)|(0+(//.0+)?))$";
/**
*
* 匹配负浮点数
*
*/
public static final String NEGATIVE_RATIONAL_NUMBERS_REGEXP = "^(-(([0-9]+//.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*//.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
/**
*
* 匹配浮点数
*
*/
public static final String RATIONAL_NUMBERS_REGEXP = "^(-?//d+)(//.//d+)?$";
/**
*
* 匹配由26个英文字母组成的字符串
*
*/
public static final String LETTER_REGEXP = "^[A-Za-z]+$";
/**
*
* 匹配由26个英文字母的大写组成的字符串
*
*/
public static final String UPWARD_LETTER_REGEXP = "^[A-Z]+$";
/**
*
* 匹配由26个英文字母的小写组成的字符串
*
*/
public static final String LOWER_LETTER_REGEXP = "^[a-z]+$";
/**
*
* 匹配由数字和26个英文字母组成的字符串
*
*/
public static final String LETTER_NUMBER_REGEXP = "^[A-Za-z0-9]+$";
/**
*
* 匹配由数字、26个英文字母或者下划线组成的字符串
*
*/
public static final String LETTER_NUMBER_UNDERLINE_REGEXP = "^//w+$";
/**
* 大小写敏感的正规表达式批配
*
* @param source
* 批配的源字符串
* @param regexp
* 批配的正规表达式
* @return 如果源字符串符合要求返回真,否则返回假
*/
public static boolean isHardRegexpValidate(String source, String regexp) {
try {
// 用于定义正规表达式对象模板类型
PatternCompiler compiler = new Perl5Compiler();
// 正规表达式比较批配对象
PatternMatcher matcher = new Perl5Matcher();
// 实例大小大小写敏感的正规表达式模板
Pattern hardPattern = compiler.compile(regexp);
// 返回批配结果
return matcher.contains(source, hardPattern);
} catch (MalformedPatternException e) {
log.warn(e);
}
return false;
}
}
作者:javaACMer 发表于2013-8-12 11:13:49 原文链接
阅读:14 评论:0 查看评论
[2]Coding Interview 8.2
来源: 互联网 发布时间: 2013-10-26
1、一个m*n的矩阵a[][],机器人从左上角走到右下角,只能朝右或朝下走,输出所有路径。
2、如果矩阵有的格子可以走,有的格子不可以走,输出所有路径。(a[i][j]==1表示可以走,a[i][j]==0表示不可以走)
思路:
典型的递归算法。问题1直接用深搜的思想。问题2在问题1的基础上加个判断条件即可。
#include <iostream>
#include <vector>
using namespace std;
struct Point
{
int x;
int y;
Point(int i, int j) : x(i), y(j)
{}
};
//问题1
void Path1(int x, int y, int m, int n, vector<Point>& vec, int len)
{
if (x == m || y == n)
return;
Point p(x, y);
vec[len++] = p;
if (x == m - 1 && y == n - 1)
{
for (int i = 0; i < vec.size(); ++i)
cout << vec[i].x << ' ' << vec[i].y << endl;
}
else
{
Path1(x, y+1, m, n, vec, len);
Path1(x+1, y, m, n, vec, len);
}
}
//问题2
void Path2(int x, int y, int m, int n, vector<Point>& vec, int len, int safe[][4])
{
if (x == m || y == n || safe[x][y] == 0)
return;
Point p(x, y);
vec[len++] = p;
if (x == m - 1 && y == n - 1)
{
for (int i = 0; i < vec.size(); ++i)
cout << vec[i].x << ' ' << vec[i].y << endl;
}
else
{
Path2(x, y+1, m, n, vec, len, safe);
Path2(x+1, y, m, n, vec, len, safe);
}
}
void main()
{
int m = 3, n = 4;
int x = 0, y = 0;
int len = 0;
Point p(0, 0);
vector<Point> vec(m+n-1, p);
Path1(x, y, m, n, vec, len);
int safe[][4] = { {1, 1, 1, 0},{0, 1, 1, 1}, {0, 0, 1, 1} };
Path2(x, y, m, n, vec, len, safe);
}
作者:f_x_q 发表于2013-8-13 10:46:34 原文链接
阅读:60 评论:0 查看评论
[3]Coding Interview 8.5
来源: 互联网 发布时间: 2013-10-26
输出n对括号的所有有效组合。
思路:
运用递归的思想。只要左括号没有用完,总可以插入左括号;当已插入的左括号数目大于右括号,那么就可以插入右括号。我们只要记录左右括号剩余的数目,然后递归即可。递归终止条件有两个:当左括号剩余数小于0或者左括号剩余数大于右括号剩余数,此时为无效状态;当左右括号剩余数都为0,此时输出结果。
#include <iostream>
#include <vector>
using namespace std;
void Pare(vector<char>& s, int l, int r, int c)
{
if (l < 0 || r < l)
return;
if (l == 0 && r == 0)
{
for (int i = 0; i < s.size(); ++i)
cout << s[i];
cout << endl;
}
else
{
if (l > 0)
{
s[c] = '(';
Pare(s, l - 1, r, c + 1);
}
if (r > l)
{
s[c] = ')';
Pare(s, l, r - 1, c + 1);
}
}
}
void main()
{
int c = 4;
vector<char> vec(2*c);
Pare(vec, c, c, 0);
}
作者:f_x_q 发表于2013-8-14 10:53:54 原文链接
阅读:44 评论:0 查看评论
最新技术文章: