- 用两个栈(stacks)来实现一个队列(queue),完成队列的push和pop操作。只能使用Stack的isEmpty()、push()与pop()三个操作。队列中的元素为int类型。
import java.util.Stack;
/**
* <p>使用两个栈完成队列(先进先出)</p>
*/
public class Solution {
private Stack<Integer> pushStack = new Stack<>();
private Stack<Integer> popStack = new Stack<>();
/**
* 入栈
*/
public void push(Integer value) {
pushStack.push(value);
}
/**
* 出栈
*/
public Integer pop() {
if (!popStack.isEmpty()) {
return popStack.pop();
}
while (!pushStack.isEmpty()) {
Integer temp = pushStack.pop();
popStack.push(temp);
}
if (popStack.isEmpty()) {
return null;
}
return popStack.pop();
}
/**
* 判断栈是否为空
*/
public boolean isEmpty() {
return popStack.isEmpty() && pushStack.isEmpty();
}
/**
* 测试入口
*/
public static void main(String[] args) {
Solution queue = new Solution();
for (int i = 0; i < 100; i++) {
queue.push(i);
}
System.out.println(queue.pop());
System.out.println(queue.pop());
while (!queue.isEmpty()) {
System.out.println(queue.pop());
}
}
}
package com.leno.jeep.simple.queue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
/**
* <p>短url工具</p>
*
*/
public class Codec {
private HashMap<String, String> map = new HashMap<>();
/**
* 加密
*/
public String encode(String longUrl) {
for (HashMap.Entry<String, String> item : map.entrySet()) {
if (item.getValue().equals(longUrl)) {
return item.getKey();
}
}
String shortUrl = "";
while (map.get(shortUrl) != null) {
shortUrl = getShortRandomUrl();
}
map.put(shortUrl, longUrl);
return shortUrl;
}
/**
* 解密
*/
public String decode(String shortUrl) {
return map.get(shortUrl);
}
/**
* 随机url
*/
public String getShortRandomUrl() {
// 26个大写字母和10个数字
String randomStr = "http://tinyurl.com/";
Random random = new Random();
for (int i = 0; i < 4; i++) {
int tempI = random.nextInt(36);
if (tempI < 10) {
randomStr = randomStr + tempI;
} else {
char tempC = (char) ('A' + tempI - 10);
randomStr = randomStr + tempC;
}
}
return randomStr;
}
/**
* 随机url
*/
public String getLongRandomUrl() {
// 26个大写字母和10个数字
String randomStr = "https://leetcode.com/problems/design-tinyurl/";
Random random = new Random();
for (int i = 0; i < 20; i++) {
int tempI = random.nextInt(36);
if (tempI < 10) {
randomStr = randomStr + tempI;
} else {
char tempC = (char) ('A' + tempI - 10);
randomStr = randomStr + tempC;
}
}
return randomStr;
}
public static void main(String[] args) {
Codec demo = new Codec();
List<String> urlList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
urlList.add(demo.getLongRandomUrl());
}
for (String item : urlList) {
System.out.println("原来的长链接:" + item);
String shortUrl = demo.encode(item);
System.out.println("短url:" + shortUrl);
String longUrl = demo.decode(shortUrl);
System.out.println("解码url:" + longUrl);
System.out.println("=======================");
}
}
}
网友评论