575

来源:互联网 发布:开发sql语句美化小工具 编辑:程序博客网 时间:2024/05/22 06:30

5.12

阿西吧。还以为今天一个题都做不出来了呢。没想到在临下班前,居然做出来了呢。

这个题一看就是肯定要用栈来做的。

我弄了两个栈,一个用来存放数字,一个用来存放字符串。

值得注意的是,在获取栈顶元素,以及获取数组中的某个元素的时候,一定要记得判断栈是不是为空,数组下标是不是越界的情况。

感觉自己代码的思路大体上是没有错的,就是在实现的过程中,有一些小的地方有些混乱,以至于兜兜转转了一整天。

比如要考虑 数字为0的情况等一些小的细节,需要更加认真细心。

public class Solution {    /**     * @param s  an expression includes numbers, letters and brackets     * @return a string     */    // 还要考虑0的情况   public static String expressionExpand(String s) {        // Write your code here        int length = s.length();        if(length <= 1){            return s;        }        String res = "";// 用来存储最后的结果        LinkedList<Integer> numStack = new LinkedList<Integer>();// 用来存储数字        //用来存储字符串以及“【”        LinkedList<String> stringStack = new LinkedList<String>();        String tmpStr = "";        int tmpNum = 0;        for(int i = 0; i < length; i++){            //如果是“【”的话就入栈        char aaaaa = s.charAt(i);            if(s.charAt(i) == '['){                stringStack.push("[");                continue;            }            // 获取数字的值,并且入栈            if(isNum(s.charAt(i))){                while(i<length && isNum(s.charAt(i))){                    tmpNum = tmpNum*10 + s.charAt(i) - '0';                    i++;                }                i--;                numStack.push(tmpNum);                //System.out.println("tmpNum:" + tmpNum);                tmpNum = 0;                continue;            }            //得到一串连续的字符串,并且入栈            if(Character.isLetter(s.charAt(i))){                while(i < length && Character.isLetter(s.charAt(i))){                    tmpStr = tmpStr + Character.toString(s.charAt(i));                    //System.out.println("s.charAt(" + i + "):" + s.charAt(i));                    i ++;                }                // 要时时刻刻记得判断越界以及是否为空                if(!stringStack.isEmpty() && stringStack.peek() != "["){                tmpStr = stringStack.pop() + tmpStr;                }                stringStack.push(tmpStr);                tmpStr = "";                //System.out.println("tmpStr:" + tmpStr);                i--;                continue;            }            //如果是“】”的话,就要进行出栈压栈的操作了            if(s.charAt(i) == ']'){                String tmp = stringStack.pop();                if(tmp == "["){                stringStack.push(res);                continue;                }                int count = numStack.pop();                // 还要考虑为0的情况                if(count == 0){                    stringStack.pop();                    continue;                }                    //tmp_tmp 用来记录tmp的值;                String tmp_tmp = tmp;                for(int k = 0; k< count-1;k++){                   tmp = tmp + tmp_tmp;                }                stringStack.pop();                if(!stringStack.isEmpty() && stringStack.peek() != "["){                    tmp = stringStack.pop() + tmp;                }                stringStack.push(tmp);                res = tmp;            }                    }        res = "";        while(!stringStack.isEmpty()){            res = stringStack.pop() + res;        }        //System.out.println(res);        return res;    }    public static boolean isNum(char s){        int tmp = s - '0';        if(tmp >= 0 && tmp <=9){            return true;        }        return false;    }}


还算不错,今天还算有点儿战绩。(づ ̄ 3 ̄)づ


0 0
原创粉丝点击