努力打卡 每天学习 不浪费每一天 Day40

今天学了栈和队列  正好刷几道学校题(作为dlnu的学子真的不得不吐槽一下学校的oj平台,今天下午研究半天才发现所有的题都不能加换行操作)

D-OJ -616

题目:

从键盘读入一个字符串,其中只含有() {} [ ] ,判断该字符串中的每种括号是否成对出现。

提示:可借助栈来实现,括号必须配对出现,如()[ ]{},这是匹配的括号,如([{])},这是不匹配的括号(中间无空格)。
答案:

​
import java.util.Iterator;
import java.util.Scanner;

public class Temp{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        char[] arr=s.toCharArray();
        Stack2 <Character> stack2=new Stack2<>();
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]=='<'||arr[i]=='['||arr[i]=='{'||arr[i]=='(') {
                stack2.push(arr[i]);
            }
        }
        Stack2<Character> stack3=new Stack2<>();
        for (int i = arr.length-1; i >0; i--) {
            if(arr[i]=='>'||arr[i]==']'||arr[i]=='}'||arr[i]==')') {
                stack3.push(arr[i]);
            }
        }
        if(stack2.equal(stack3))
        {
            System.out.print("yes");
        }
        else {
            System.out.println("no");
        }


    }
}

class Stack2<T> implements Iterable<T> {
    private Node head;
    private int N;

    private class Node{
        private Node next;
        private T t;

        public Node(T t,Node next) {
            this.t=t;
            this.next=next;
        }
    }

    public Stack2() {
        head=new Node(null,null);
        N=0;
    }
    public void push(T t){
        Node oldFirst=head.next;
        Node newNode=new Node(t,null);
        head.next=newNode;
        newNode.next=oldFirst;
        N++;
    }
    public T pop(){
        Node oldFirst=head.next;
        if(oldFirst!=null){
            Node popNode=head.next;
            head.next=popNode.next;
            N--;
            return popNode.t;
        }
        return null;
    }
    public boolean equal(Stack2 stack2){
        int n=0;
        if(this.N!=stack2.N){

            return false;

        }
        else {
            for (int i = 0; i < N; i++) {
                Character ch1=(Character) this.pop();
                Character ch2=(Character) stack2.pop();
                int a=(int) (ch1.charValue()+1);
                int b=(int)(ch2.charValue());
                int c=(int)(ch1.charValue()+2);
                int d=(int) (ch2.charValue());
                if(a==b||c==d)
                {
                n++;
                }
            }

        }
        if(n==N){
            return true;
        }
        return false;

    }
    @Override
    public Iterator<T> iterator() {
        return new MyIterator();
    }
    private class  MyIterator implements Iterator<T>
    {
        private Node n;

        public MyIterator() {
            this.n=head;
        }

        @Override
        public boolean hasNext() {
            return n.next!=null;
        }

        @Override
        public T next() {
            n=n.next;
            return n.t;

        }
    }


}

​

思路:

既然是栈  就是先进后出

于是 我定义了两个栈  第一个栈 顺着接受左符号  第二个栈 逆着接受右符号(先进后出)
然后 在栈中定义一个方法 可以对比两个栈中每个数据的ascill值 这里用到了强转 因为Character不可以强转成int  于是 先定义两个Character接受第一个栈 和第二个栈的内容 然后调用Character的charAt方法变成字符再强转int
根据Ascill  40(   41)   60<   62>   {123   }125   [91     ] 93  右符号不是大于1就是大于二

所以就很好判断了  然后根据匹配对的次数 如果两个栈中每个符号都对上了 那就是yes  只要少一次 就是no


D-OJ -458

题目:

请你定义一个链栈,可以对链栈进行 “将某个元素入栈”、“弹出栈顶元素”、“取栈顶元素(不删除)”、“判断栈是否为空”、 “清空栈”等操作。键盘输入一些命令,可以执行上述操作。本题中,栈的元素为字符。

答案:

import java.util.Iterator;
import java.util.Scanner;


public class Temp {
    public static void main(String[] args) {
        newStack<String> stack=new newStack<>();
        Scanner sc=new Scanner(System.in);
        String s;
        String item;
        while(true){
            s=sc.next();
            if(s.equals("E"))break;
            switch (s) {
                case "P":{
                    item=sc.next();
                    stack.push(item);
                    break;
                }
                case "D":{
                        item=stack.pop();
                        if(item==null)
                        {
                            System.out.print("None");
                        }
                        else {
                             System.out.print(item);
                        }

                    break;
                }
                case "G":{
                        item=stack.popItem();
                    if(item==null)
                    {
                        System.out.print("None");
                    }
                    else {
                        System.out.print(item);
                    }
                    break;
                }
                case "T":{
                    stack.deleteStack();
                    break;
                }
                case "Y":{
                    System.out.print(stack.isEmpty());
                    break;
                }

                default: {
                    System.out.print("输入错误");
                    break;
                }

            }

        }
    }

}
class newStack<T> implements Iterable<T>
{
    private Node head;
    private int N;

    private class Node
    {
        public T item;
        public Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }

    }

    public newStack() {
        head=new Node(null,null);
        this.N=0;
    }
    public String isEmpty() {
        return (N==0)?"Yes":"No";
    }
    public int sieze(){
        return N;
    }
    public void push(T t){
        Node oldFirst=head.next;
        Node newNode=new Node(t,null);
        head.next=newNode;
        newNode.next=oldFirst;
        N++;
    }
    public T pop()
    {
        Node oldFirst=head.next;
        if (oldFirst==null) {
            return null;
        }
        head.next=oldFirst.next;
        N--;
        return oldFirst.item;

    }
    public T popItem()
    {
        Node First=head.next;
        if (First==null) {
            return null;
        }
        return First.item;
    }

    public void deleteStack(){
        head.next=null;
        N=0;
    }
    @Override
    public Iterator<T> iterator() {
        return new SIterator();
    }
    private class SIterator implements Iterator
    {
        private Node n;

        public SIterator() {
            this.n=head;
        }

        @Override
        public boolean hasNext() {
            return n.next!=null;
        }

        @Override
        public Object next() {
            n=n.next;
            return n.item;
        }
    }

}

思路:没什么说的  学完栈的链式表达就会写


D-OJ-545

题目:

将三个十进制数分别转换成八进制数,建议使用栈来实现。

public class Temp {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Stack3<Integer> stack2=new Stack3<>();
        int a= sc.nextInt();
        int b=sc.nextInt();
        int c= sc.nextInt();
        toEigth(a,stack2);
        toEigth(b,stack2);
        toEigth(c,stack2);

    }

    private static void toEigth(int a,Stack3<Integer> stack2) {
            String eightNum="";
            int yu;
            int chu=a;
            while(chu!=0){
                yu=chu%8;
                chu=chu/8;
                stack2.push(yu);
            }
            for (Integer integer : stack2) {

                eightNum+=stack2.pop().toString();

            }
            System.out.print(eightNum);

    }


}

    class Stack3<T> implements Iterable<T> {
        private Node head;
        private int N;

        private class Node {
            private Node next;
            private T t;

            public Node(T t, Node next) {
                this.t = t;
                this.next = next;
            }
        }

        public Stack3() {
            head = new Node(null, null);
            N = 0;
        }

        public void push(T t) {
            Node oldFirst = head.next;
            Node newNode = new Node(t, null);
            head.next = newNode;
            newNode.next = oldFirst;
            N++;
        }

        public T pop() {
            Node oldFirst = head.next;
            if (oldFirst != null) {
                Node popNode = head.next;
                head.next = popNode.next;
                N--;
                return popNode.t;
            }
            return null;
        }



        @Override
        public Iterator<T> iterator() {
            return new MyIterator();
        }

        private class MyIterator implements Iterator<T> {
            private Node n;

            public MyIterator() {
                this.n = head;
            }

            @Override
            public boolean hasNext() {
                return n.next != null;
            }

            @Override
            public T next() {
                n = n.next;
                return n.t;

            }
        }

    }

思路:完全符合栈的模式  直接写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值