欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2020秋招笔试题目

程序员文章站 2022-06-09 09:46:30
...

205. 同构字符串

class Solution {
    public boolean isIsomorphic(String s, String t) {
        return isIsomorphic1(s,t)&&isIsomorphic1(t,s);
    }
    public boolean isIsomorphic1(String s, String t) {
        if (s.length() == 0) {
            return true;
        }
        HashMap<Character, Character> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (!map.containsKey(s.charAt(i))) {
                map.put(s.charAt(i), t.charAt(i));
            } else {
                if (t.charAt(i) != map.get(s.charAt(i))) {
                    return false;
                } else {
                    continue;
                }
            }
        }
        return true;
    }

剑指 Offer 09. 用两个栈实现队列

/**
 * Copyright (C), 2018-2020
 * FileName: MyQueue
 * Author:   xjl
 * Date:     2020/8/25 12:43
 * Description: 栈实现队列
 */
package Stack;

import java.util.Stack;

/**
 * 利用的两个栈来实现的队列的
 */
public class MyQueue {
    Stack<Integer> stack1;
    Stack<Integer> stack2;

    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    //插入元素
    public void push(int x) {
        //现将stack中弹出
        while (!stack1.isEmpty()) {
            stack2.add(stack1.peek());
            stack1.pop();
        }
        //插入元素
        stack1.add(x);
        //在往stack1中
        while (!stack2.isEmpty()) {
            stack1.add(stack2.peek());
            stack2.pop();
        }
    }

    public int pop() {
        return stack1.pop();
    }

    public int peek() {
        return stack1.peek();
    }

    public boolean empty() {
        return stack1.isEmpty();
    }

    public static void main(String[] args) {
        MyQueue queue = new MyQueue();
        queue.push(1);
        queue.push(2);
        queue.push(3);
        queue.push(4);

        while (!queue.empty()) {
            System.out.println(queue.peek());
            queue.pop();
        }
    }
}


232. 用栈实现队列

/**
 * Copyright (C), 2018-2020
 * FileName: MyQueue
 * Author:   xjl
 * Date:     2020/8/25 12:43
 * Description: 栈实现队列
 */
package Stack;

import java.util.Stack;

/**
 * 利用的两个栈来实现的队列的
 */
public class MyQueue {
    Stack<Integer> stack1;
    Stack<Integer> stack2;

    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    //插入元素
    public void push(int x) {
        //现将stack中弹出
        while (!stack1.isEmpty()) {
            stack2.add(stack1.peek());
            stack1.pop();
        }
        //插入元素
        stack1.add(x);
        //在往stack1中
        while (!stack2.isEmpty()) {
            stack1.add(stack2.peek());
            stack2.pop();
        }
    }

    public int pop() {
        return stack1.pop();
    }

    public int peek() {
        return stack1.peek();
    }

    public boolean empty() {
        return stack1.isEmpty();
    }

    public static void main(String[] args) {
        MyQueue queue = new MyQueue();
        queue.push(1);
        queue.push(2);
        queue.push(3);
        queue.push(4);

        while (!queue.empty()) {
            System.out.println(queue.peek());
            queue.pop();
        }
    }
}


剑指 Offer 31. 栈的压入、弹出序列

 

剑指 Offer 30. 包含min函数的栈

 

225. 用队列实现栈

/**
 * Copyright (C), 2018-2020
 * FileName: MyStack
 * Author:   xjl
 * Date:     2020/8/25 12:59
 * Description: 队列实现栈
 */
package Stack;

import java.util.LinkedList;
import java.util.Queue;

public class MyStack {

    Queue<Integer> queue1;//存取的元素的
    Queue<Integer> queue2;//临时存放元素

    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        //检查是否无空
        while (!queue1.isEmpty()) {
            queue2.add(queue1.peek());
            queue1.poll();
        }
        //刚进来的元素
        queue1.add(x);
        while (!queue2.isEmpty()) {
            queue1.add(queue2.peek());
            queue2.poll();
        }
    }

    public int pop() {
        return queue1.poll();
    }

    public int top() {
        return queue1.peek();
    }

    public boolean empty() {
        return queue1.isEmpty();
    }

    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);

        while (!stack.empty()) {
            System.out.println(stack.top());
            stack.pop();
        }
    }

}

 

相关标签: Leetcode leetcode