My Java simple Solution without ‘()’ ‘[]’ '{}'


  • 3
    S
    public class Solution {
    public boolean isValid(String s) {
        StringBuffer bf=new StringBuffer();
        int i=0;
        char temp;
        boolean flag=true;
        while(i<s.length())
        {
        	temp=s.charAt(i);
        	if(bf.length()!=0&&(temp==(char)(bf.charAt(bf.length()-1)+1)||temp==(char)(bf.charAt(bf.length()-1)+2)))
            		bf.deleteCharAt(bf.length()-1);
        	else
        		bf.append(temp);
        	i++;
        }
        if(bf.length()!=0)
        	flag=false;
        return flag;
    }
    

    }


  • 0
    M

    great answer, bad style


  • 0
    S

    Thank you very much!!! I've always thought my code's style is not good! Can you give me some advice or recommend some books for me?? Thanks again!


  • 0
    M

    Here is a simple one: http://www.javaranch.com/styleLong.jsp
    Two great books regarding styles, designs and conventions would be "Clean Code" and "The practice of programming"


  • 0
    S

    Your suggestion is very very good,thanks for you help!!!


  • 1
    T

    In addition to a long style guide here's a simple list of good code traits:

    • use variables to re-use expressions: int lastIndex = bf.length() - 1
    • use meaningful variable names: char lastChar = bf.charAt(lastIndex)
    • space out operators: temp == (char)(lastChar + 1).

    Try to apply these 3 simple rules to the above code and you'll see the difference.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.