您的位置:首页 > 职场人生

剑指offer面试题 java解答51-55

2016-08-22 15:40 465 查看
面试题51:数组中重复的数字

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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 面试题