定义节点类
private static class Node<T> {
T item;
Node next;
public Node(T item, Node next) {
this.item = item;
this.next = next;
}
}
1.括号匹配的问题
public class BracketMatch {
public static void main(String[] args) {
String str = "(上海(长安)())";
boolean match = isMatch(str);
System.out.println("‘"+str+"'"+"中的括号是否匹配:"+match);
}
public static boolean isMatch(String str){
Stack<String> chars = new Stack<>();
for (int i = 0; i < str.length(); i++) {
String currChar=str.charAt(i)+ "";
if (currChar.equals("(")){
chars.push(currChar);
}
else if (currChar.equals(")")){
String popChar = chars.pop();
if (popChar==null){
return false;
}
}
}
if (chars.size()==0){
return true;
}else {
return false;
}
}
}
2.判断链表中是否有环
public class CircleListCheckTest {
public static void main(String[] args) throws Exception {
Node<String> first = new Node<String>("aa", null);
Node<String> second = new Node<String>("bb", null);
Node<String> third = new Node<String>("cc", null);
Node<String> fourth = new Node<String>("dd", null);
Node<String> fifth = new Node<String>("ee", null);
Node<String> six = new Node<String>("ff", null);
Node<String> seven = new Node<String>("gg", null);
first.next = second;
second.next = third;
third.next = fourth;
fourth.next = fifth;
fifth.next = six;
six.next = seven;
seven.next = third;
boolean circle = isCircle(first);
System.out.println("first链表中是否有环:"+circle);
}
public static boolean isCircle(Node<String> first) {
Node<String> fast = first;
Node<String> slow = first;
while(fast!=null && fast.next!=null){
fast = fast.next.next;
slow = slow.next;
if (fast.equals(slow)){
return true;
}
}
return false;
}
}
3.查找链表的中间值
public class FastSlowTest {
public static void main(String[] args) throws Exception {
Node<String> first = new Node<String>("aa", null);
Node<String> second = new Node<String>("bb", null);
Node<String> third = new Node<String>("cc", null);
Node<String> fourth = new Node<String>("dd", null);
Node<String> fifth = new Node<String>("ee", null);
Node<String> six = new Node<String>("ff", null);
Node<String> seven = new Node<String>("gg", null);
first.next = second;
second.next = third;
third.next = fourth;
fourth.next = fifth;
fifth.next = six;
six.next = seven;
String mid = getMid(first);
System.out.println("中间值为:"+mid);
}
public static String getMid(Node<String> first) {
Node<String> fast = first;
Node<String> slow = first;
while(fast!=null &&fast.next!=null){
fast = fast.next.next;
slow=slow.next;
}
return slow.item;
}
}
4.查找有环链表中环的入口结点
public class CircleListInTest {
public static void main(String[] args) throws Exception {
Node<String> first = new Node<String>("aa", null);
Node<String> second = new Node<String>("bb", null);
Node<String> third = new Node<String>("cc", null);
Node<String> fourth = new Node<String>("dd", null);
Node<String> fifth = new Node<String>("ee", null);
Node<String> six = new Node<String>("ff", null);
Node<String> seven = new Node<String>("gg", null);
first.next = second;
second.next = third;
third.next = fourth;
fourth.next = fifth;
fifth.next = six;
six.next = seven;
seven.next = third;
Node<String> entrance = getEntrance(first);
System.out.println("first链表中环的入口结点元素为:"+entrance.item);
}
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast=head,slow=head;
ListNode temp=null;
while(fast!=null && fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow){
temp=head;
while(temp!=slow){
temp=temp.next;
slow=slow.next;
}
return temp;
}
}
return null;
}
}
5.解决约瑟夫问题
public class JosephTest {
public static void main(String[] args) {
Node<Integer> first = null;
Node<Integer> pre = null;
for(int i = 1;i<=41;i++){
if (i==1){
first = new Node<>(i,null);
pre = first;
continue;
}
Node<Integer> newNode = new Node<>(i, null);
pre.next=newNode;
pre=newNode;
if (i==41){
pre.next=first;
}
}
int count=0;
Node<Integer> n = first;
Node<Integer> before = null;
while(n!=n.next){
count++;
if (count==3){
before.next=n.next;
System.out.print(n.item+",");
count=0;
n=n.next;
}else{
before=n;
n=n.next;
}
}
System.out.println(n.item);
}
}
6.逆波兰表达式
public class ReversePolishNotationTest {
public static void main(String[] args) {
String[] notation = {"3", "17", "15", "-", "*", "18", "6", "/", "+"};
int result = caculate(notation);
System.out.println("逆波兰表达式的结果为:" + result);
}
public static int caculate(String[] notaion) {
Stack<Integer> oprands = new Stack<>();
for (int i = 0; i < notaion.length; i++) {
String curr = notaion[i];
Integer o1;
Integer o2;
Integer result;
switch (curr) {
case "+":
o1 = oprands.pop();
o2 = oprands.pop();
result = o2 + o1;
oprands.push(result);
break;
case "-":
o1 = oprands.pop();
o2 = oprands.pop();
result = o2 - o1;
oprands.push(result);
break;
case "*":
o1 = oprands.pop();
o2 = oprands.pop();
result = o2 * o1;
oprands.push(result);
break;
case "/":
o1 = oprands.pop();
o2 = oprands.pop();
result = o2 / o1;
oprands.push(result);
break;
default:
oprands.push(Integer.parseInt(curr));
}
}
Integer pop = oprands.pop();
return pop;
}
}