剑指offer面试题 java解答51-55
2016-08-22 15:40
465 查看
面试题51:数组中重复的数字
面试题52:构建乘积数组
面试题53:正则表达式匹配
面试题54:表示数值的字符串
面试题55:字符流中第一个不重复的字符
public class Test51 { public static boolean duplicate(int[] numbers){ boolean found=false; if (numbers==null||numbers.length<1) { return found; } int length=numbers.length; for (int i = 0; i < length; i++) { if (numbers[i]<0||numbers[i]>length-1) { return found; } } for (int i = 0; i < numbers.length; i++) { if (numbers[i]!=i) { if (numbers[i]==numbers[numbers[i]]) { System.out.println(numbers[i]); found=true; } int temp=numbers[i]; numbers[i]=numbers[temp]; numbers[temp]=temp; } } return found; } public static void main(String[] args) { int[] numbers={2,3,1,0,2,5,3}; boolean found=duplicate(numbers); System.out.println(found); } }
面试题52:构建乘积数组
public class Test52 { public static int[] multiply(int[] array1) { if (array1 == null || array1.length <= 0) { return null; } int[] array2 = new int[array1.length]; array2[0] = 1; for (int i = 1; i < array1.length; i++) { array2[i] = array2[i-1] * array1[i-1]; } double temp = 1; for (int i = array1.length-2; i >= 0; i--) { temp *= array1[i+1]; array2[i]*=temp; } return array2; } public static void main(String[] args) { int[] array1 = {1,2,3,4,5}; int[] array2 = multiply(array1); for (int i = 0; i < array2.length; i++) System.out.println(array2[i]); } }
面试题53:正则表达式匹配
public class Test53 { private static boolean matchCore(char[] pPstr,int indexStr,char[] pPattern,int indexPattern){ int strLen=pPstr.length; int patternLen=pPattern.length; if (indexStr==strLen&&indexPattern==patternLen) { return true; } if (indexStr!=strLen&&indexPattern==patternLen) { return false; } if (indexPattern<patternLen-1&&pPattern[indexPattern+1]=='*') { if (pPstr[indexStr]==pPattern[indexPattern]||(pPattern[indexPattern]=='.'&&indexStr!=strLen)) { return matchCore(pPstr, indexStr+1, pPattern, indexPattern+2) || matchCore(pPstr, indexStr+1, pPattern, indexPattern) || matchCore(pPstr, indexStr, pPattern, indexPattern+2); }else { return matchCore(pPstr, indexStr, pPattern, indexPattern+2); } } if (pPstr[indexStr]==pPattern[indexPattern]||(pPattern[indexPattern]=='.'&&indexStr!=strLen)) { return matchCore(pPstr, indexStr+1, pPattern, indexPattern+1); } return false; } public static boolean match(String str,String pattern){ if (str==null||pattern==null) { return false; } char[] pPstr=str.toCharArray(); char[] pPattern=pattern.toCharArray(); return matchCore(pPstr,0,pPattern,0); } public static void main(String[] args) { boolean match=match("aaa", "ab*ac*a"); System.out.println(match); } }
面试题54:表示数值的字符串
public class Test54 { public static boolean isNumeric(String string){ if (string==null) { return false; } char[] str=string.toCharArray(); int strLen=str.length; int index=0; if (str[index]=='+'||str[index]=='-') { index++; } if (index>=strLen) { return false; } boolean numeric=true; index=scanDigits(str,index); if (index<strLen) { if (str[index]=='.') { index++; index=scanDigits(str, index); if (index<strLen&&(str[index]=='e'||str[index]=='E')) { numeric=isExponential(str,index); }else { if (index==strLen) { numeric=true; }else { numeric=false; } } }else if (str[index]=='e'||str[index]=='E') { numeric=isExponential(str,index); }else { numeric=false; } } return numeric; } private static boolean isExponential(char[] str, int index) { if (str[index]!='e'&&str[index]!='E') { return false; } index++; if (index<str.length&&(str[index]=='+'||str[index]=='-')) { index++; } if (index>=str.length) { return false; } index=scanDigits(str, index); return index==str.length?true:false; } private static int scanDigits(char[] str, int index) { while (index<str.length&&str[index]>='0'&&str[index]<='9') { index++; } return index; } public static void main(String[] args) { boolean numeric=isNumeric("12e+5.4"); System.out.println(numeric); } }
面试题55:字符流中第一个不重复的字符
import java.io.InputStreamReader; import java.util.Scanner; public class Test55 { private int[] occurrence=new int[256]; int index=0; public Test55(){ for (int i = 0; i < 256; i++) { occurrence[i]=-1; } } public void Insert(char ch){ if (occurrence[ch]==-1) { occurrence[ch]=index; }else if(occurrence[ch]>=0){ occurrence[ch]=-2; } index++; } public char FirstAppearingOnce(){ char ch='\0'; int minIndex=Integer.MAX_VALUE; for (int i = 0; i < 256; i++) { if (occurrence[i]>=0&&occurrence[i]<minIndex) { ch=(char)i; minIndex=occurrence[i]; } } return ch; } public static void main(String[] args) { Test55 t=new Test55(); Scanner scanner=new Scanner(System.in); while (scanner.hasNext()) { t.Insert(scanner.next().charAt(0)); char ch=t.FirstAppearingOnce(); System.out.println(ch); } } }
相关文章推荐
- 剑指offer面试题 java解答41-45
- 剑指Offer面试题51(Java版):数组中重复的数字
- 剑指offer面试题 java解答26-30
- 剑指offer 面试题51 java实现
- 剑指offer面试题 java解答21-25
- 剑指offer面试题 java解答36-40
- 剑指offer面试题 java解答16-20
- 剑指offer面试题 java解答61-65
- 某里巴巴Java工程师常规面试题以及解答
- Java程序员面试题及解答
- Java程序员面试题――集合框架解答方案
- JAVA常见面试题及解答(精华)
- JAVA面试题及解答
- 剑指offer--面试题19:二叉树的镜像--Java实现
- 剑指Offer面试题8(java版):旋转数组的最小数字
- 剑指offer--面试题24:二叉搜索树的后序遍历序列--Java实现
- 剑指offer--面试题28:字符串的排列--Java实现
- java面试题(51-70)
- 剑指offer--面试题5:从尾到头打印链表--Java实现
- 华为java面试题,从学长处收集而来,带有部分解答