<1> replace spaces
Title Description
Please implement a function, a string to replace each space to "20%." For example, when the string is We Are Happy. After the string is replaced after We% 20Are% 20Happy.
code show as below:
public class Solution { public String replaceSpace(StringBuffer str) { int len = str.length(); StringBuffer res = new StringBuffer(); for(int i = 0;i < len;i++){ if(str.charAt(i) != ' '){ res.append(str.charAt(i)); }else{ res.append("%20"); } } return res.toString(); }
<Two> regular expression matching
Title Description
Implement comprises a function to match '' and '*' regular expression. Mode character '.' Represents any one character '*' indicates that the preceding character can appear any number of times (including 0 time). In this problem, the match is a whole pattern matches all characters of the string. For example, the string "aaa" mode and "aa" and "ab * ac * a" match, but the "aa.a" and "ab * a" do not match.
code show as below:
public class Solution { public boolean match(char[] str, char[] pattern) { if((null == str && null == pattern) || (null != str && pattern == null)){ return true; } if(null == str && pattern != null){ return false; } return modeoMatch(str,pattern,0,0); } private boolean modeoMatch(char[] str,char[] pattern,int slen,int plen){ if(slen == str.length && plen == pattern.length){ return true; } if(slen != str.length && plen == pattern.length){ return false; } IF (PLEN +. 1 <pattern.length && pattern [PLEN +. 1] == '*') { IF (sLen <str.length && (STR [sLen] == pattern [PLEN] || pattern [PLEN] == '.')) { return modeoMatch (STR, pattern, sLen +. 1, PLEN) || modeoMatch (STR, pattern, sLen, PLEN + 2); } the else { return modeoMatch (STR, pattern, sLen, PLEN + 2) ; } } // must be placed on the back, as determined from the back of the first fishes if (slen <str.length && (str [slen] == pattern [plen] || pattern [plen] ==) '.') { return modeoMatch (STR, pattern, sLen. 1 +, + PLEN. 1); } return to false; } }
<Three> represents a numeric string
Title Description
Implement function used to determine whether a string represents a value (including integer and fractional). For example, the string "+100", "5e2", "- 123", "3.1416" and "-1E-16" shows the value. But "12e", "1a3.14", "1.2.3", "+ - 5" and "12e + 4.3" neither.
code show as below:
public class Solution { private int index = 0; public boolean isNumeric(char[] str) { if(str.length <= 0){ return false; } boolean flag = scanInteger(str); if(index < str.length && str[index] == '.'){ index++; flag = scanInteger(str) || flag; } if(index < str.length && (str[index] == 'E' || str[index] == 'e')){ index++; flag = flag && scanInteger(str); } return flag && index == str.length; that appears{// subtraction symbol behind private Boolean scanInteger (char [] str } if(index < str.length && (str[index] == '+' || str[index] == '-')){ index++; } int start = index; //Character.isDigit(str[index]) 相同与 str[index] >= '0' && str[index] <= '9' while(index < str.length && str[index] >= '0' && str[index] <= '9'){ index++; } return start < index; } }
<Four> character stream does not repeat the first character
Title Description
Please implement a function to find the character stream for the first time a character appears only. For example, when the character stream reads only the first two characters "go", the first character only occurs once a "g". When reading out the first six characters "google" from the character stream, first appears only one character is "l".
Output Description:
If the current character stream does not appear there is a character, returns the # character.
code show as below:
import java.util.HashMap; import java.util.ArrayList; public class Solution { HashMap<Character,Integer> map = new HashMap<Character,Integer>(); ArrayList list = new ArrayList(); //Insert one char from stringstream public void Insert(char ch) { if(!map.containsKey(ch)){ map.put(ch,1); list.add(ch); }else{ map.put(ch,map.get(ch) + 1); } } //return the first appearence once char in current stringstream public char FirstAppearingOnce() { for(int i = 0;i < list.size();i++){ if(map.get(list.get(i)) == 1){ return (char)list.get(i); } } return '#'; } }