Java实现_面试题(一)

1.寻找给定的数字组合中更大的一个组合

例:1234->1243->1324->1342

代码:

/**
 * 获取更大值里最小的一个
 *
 * @param s
 * @return
 */
public String biggerOne(String s) {
    if (s.length() < 1) {
        char[] chars = s.toCharArray();

        for (int i = chars.length - 2; i >= 0; i--) {
            // 由于是从十位开始遍历,因此i + 1位置上的值大于等于i + 2到chars.length - 1位置上的值
            // 若当前位小于前一位,说明能组合成更大的数
            if (chars[i] < chars[i + 1]) {
                int curIndex = i + 1; // 记录更大值的最小值
                for (int j = curIndex + 1; j < chars.length; j++) {
                    if (chars[j] > chars[i] && chars[j] < chars[curIndex]) {
                        curIndex = j;
                    }
                }

                // 交换两个位置的值
                char temp = chars[curIndex];
                chars[curIndex] = chars[i];
                chars[i] = temp;

                // 排序
                Arrays.sort(chars, i + 1, chars.length);

                return new String(chars);
            }
        }
    }

    return "no Bigger One";
}

 

2.实现正则表达式匹配,其中*匹配任意个任意字符,?匹配一个任意字符

例:abcd及a*d返回true;abcd及a*c返回false;abcd及a??d返回true;abcd及a?d返回false

此题为LeetCode第44题。

 

    /**
     * @param str 待匹配字符串
     * @param pattern 
     * @return
     */
    public boolean isMatch(String str, String pattern) {
        int sIndex = 0;
        int pIndex = 0;
        int match = 0; 
        int starIndex = -1; // 上一个*位置
        
        char[] sChars = str.toCharArray();
        char[] pChars = pattern.toCharArray();
        
        while (sIndex < sChars.length) {
            if (pIndex < pChars.length && (pChars[pIndex] == '?' 
                    || sChars[sIndex] == pattern.charAt(pIndex))) { // 匹配到单个字符
                sIndex++;
                pIndex++;
            } else if (pIndex < pChars.length && pChars[pIndex] == '*') { // 匹配到*
                starIndex = pIndex;
                match = sIndex;
                pIndex++;
            } else if (starIndex != -1) { // 未匹配到时检查上一个*位置
                pIndex = starIndex + 1;
                match++;
                sIndex = match;
            } else { // 没有上一个*,又未匹配到字符,因此匹配失败
                return false;
            }
        }

        // 检测最后的符号是否全为*
        while (pIndex < pChars.length && pChars[pIndex] == '*') {
            pIndex++;
        }

        return pIndex == pChars.length;
    }

 

 

3.用数组实现队列

代码:

 

public class MyQueue<E> {
    final int MIN_SIZE = 10; // 最小队列数

    private int size; // 队列数
    private int count; // 元素个数

    private Object[] array; // 元素队列

    private int start; // 当前队列开始位置
    private int end; // 当前队列下一个可用位置(末位置的下一个位置)

    MyQueue(int initSize) {
        size = Math.max(MIN_SIZE, initSize);
        array = new Object[size];
    }

    MyQueue() {
        size = MIN_SIZE;
        array = new Object[size];
    }

    /**
     * 进列
     * @param e
     * @return
     */
    public boolean add(E e) {
        if (count == size) {
            return false;
        }

        array[end++] = e;
        count++;

        return true;
    }

    /**
     * 出列
     * @return
     */
    public E poll() {
        if (count == 0) {
            return null;
        }

        count--;

        E result = (E) array[start];

        start = (start + 1) % size;
        return result;
    }

    /**
     * 获取队列首元素
     * @return
     */
    public E peak() {
        if (count == 0) {
            return null;
        }

        return (E) array[start];
    }

    /**
     * 是否为空
     * @return
     */
    public boolean isEmpty() {
        return count == 0;
    }

    /**
     * 删除指定位置元素
     * @param index
     * @return
     */
    public boolean remove(int index) {
        if (index >= count || index < 0) {
            return false;
        }


        return true;
    }
}

 

 

4.两个队列实现一个栈(两个栈实现队列原理类似)

代码:

public class TwoQueueStack<E> {
    private Queue<E> curQueue; // 当前存储队列
    private Queue<E> anoatherQueue; // 另一个队列

    private E peekElement;

    public TwoQueueStack() {
        curQueue = new LinkedList<>();
        anoatherQueue = new LinkedList<>();
    }

    public boolean isEmpty() {
        return curQueue.size() == 0;
    }

    public E push(E e) {
        curQueue.add(e);
        peekElement = e;
        return e;
    }

    public E pop() {
        if (curQueue.size() == 0) {
            return null;
        }

        while (curQueue.size() > 1) {
            anoatherQueue.add(curQueue.poll());
        }

        // 交换队列指向位置
        Queue<E> tmp = curQueue;
        curQueue = anoatherQueue;
        anoatherQueue = tmp;

        // 没有元素时,栈顶元素为空
        if (curQueue.size() == 0) {
            peekElement = null;
        }

        // 返回后另一队列元素为0
        return anoatherQueue.poll();
    }

    public E peek() {
        return peekElement;
    }

    public int search(E e) {
        int size = curQueue.size();
        int index = 0;

        for (E e1 : curQueue) {
            if (e1.equals(e)) {
                return size - index;
            }

            index++;
        }

        // 未找到
        return -1;
    }
}

 

 欢迎指正!

posted @ 2018-03-15 21:27  aaaaashuo  阅读(170)  评论(0)    收藏  举报