JZ52正则表达式匹配
题目描述
请实现一个函数用来匹配包括’.‘和’*‘的正则表达式。模式中的字符’.‘表示任意一个字符,而’'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"aba"均不匹配
示例1
输入
“aaa”,“a*a”
返回值
true
分析:
参考:https://blog.csdn.net/juaner1993/article/details/81978399?spm=1001.2014.3001.5501
代码:
public boolean match (String str, String pattern) {
// write code here
if (str == null || pattern == null) {
return false;
}
int strIndex = 0;
int patternIndex = 0;
char[] c1 = str.toCharArray();
char[] c2 = pattern.toCharArray();
return matchCore(c1, strIndex, c2, patternIndex);
}
/**
* 递归
*/
private boolean matchCore(char[] str, int strIndex, char[] pattern, int patternIndex) {
//有效性检验:str到尾,pattern到尾,匹配成功
if (strIndex == str.length && patternIndex == pattern.length) {
return true;
}
//pattern先到尾,匹配失败
if (strIndex != str.length && patternIndex == pattern.length) {
return false;
}
//模式的第二个为‘*’
if((patternIndex + 1 < pattern.length ) && (pattern[patternIndex+1]=='*')){
//字符串的第一个字符和模式的第一个字符相匹配
if ((strIndex != str.length && pattern[patternIndex] == str[strIndex]) || (pattern[patternIndex]== '.' && strIndex != str.length)){
return matchCore(str,strIndex,pattern,patternIndex+2)|| matchCore(str, strIndex + 1, pattern , patternIndex);
}else {
return matchCore(str, strIndex, pattern, patternIndex + 2);
}
}
//模式的第二个不为‘*’,且字符串第1个跟模式第1个匹配,则都后移1位,否则直接返回false
if((strIndex != str.length && pattern[patternIndex] == str[strIndex]) || (pattern[patternIndex]== '.' && strIndex != str.length)){
return matchCore(str, strIndex + 1, pattern, patternIndex + 1);
}
return false;
}
JZ53表示数值的字符串
**题目描述** 请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。 **示例1** 输入“123.45e+6”
返回值
true
分析:
方法1.使用正则表达式
方法2.分情况判断
代码:
/**
* 正则表达式
* @param str
* @return
*/
public boolean isNumeric1 (String str) {
// write code here
//* 零次或多次匹配前面的字符或子表达式。
//+ 一次或多次匹配前面的字符或子表达式。
//? 零次或一次匹配前面的字符或子表达式。
return str.matches("[+-]?[0-9]*\\.?[0-9]+([eE][+-]?[0-9]+)?");
}
/*
* 提交一次改一次。。。。。。。
*/
public static boolean isNumeric (String str) {
// write code here
if(str==null || str.length()==0){
return false;
}
char[] ch = str.toCharArray();
//标记符号、小数点、e是否出现过
boolean sign = false;
boolean decimal = false;
boolean hasE = false;
for (int i=0;i<ch.length;i++) {
if (ch[i] == 'e' || ch[i] == 'E') {
if (i == ch.length - 1) return false;
if (hasE) return false;
hasE = true;
} else if (ch[i] == '+' || ch[i] == '-') {
if( i==ch.length-1) return false;
//符号后边可以直接为., 也可以是数字 但不能是eE
if(ch[i+1]!='.'&&(ch[i+1] < '0' || ch[i+1] > '9') ) return false;
//+-第二次出现需要在eE后
if (sign && ch[i - 1] != 'e' && ch[i - 1] != 'E') return false;
//+-第一次出现要在i==0
if (!sign && i > 0 && ch[i - 1] != 'e' && ch[i - 1] != 'E') return false;
sign = true;
} else if (ch[i] == '.') {
//单独的.不可以
if(i == ch.length-1){
return false;
}
//eE后边不能有.
if(hasE||decimal) return false;
decimal = true;
} else if (ch[i] < '0' || ch[i] > '9') {
return false;
}
}
return true;
}
JZ54字符流中第一个不重复的字符
题目描述
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
后台会用以下方式调用Insert 和 FirstAppearingOnce 函数
string caseout = “”;
1.读入测试用例字符串casein
2.如果对应语言有Init()函数的话,执行Init() 函数
3.循环遍历字符串里的每一个字符ch {
Insert(ch);
caseout += FirstAppearingOnce()
}
2. 输出caseout,进行比较。
3.
返回值描述:
如果当前字符流没有存在出现一次的字符,返回#字符。
分析:
对于重复问题————>哈希/SET
对于字符串问题————>哈希
代码:
class Solution {
Map<Character,Integer> map = new LinkedHashMap<>();
//Insert one char from stringstream
public void Insert(char ch){
if(map.containsKey(ch)){
map.put(ch,map.get(ch)+1);//value记录的是次数
}else {
map.put(ch,1);//第一次出现
}
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce() {
//遍历,找到出现次数为1的第一个
for (char c:map.keySet()) {
if(map.get(c)==1) return c;
}
return '#';
}
}