While Executing 1 the expected out is 1 while it should be 11. Am I interpreting something wrongly


  • 3
    S

    public String countAndSay(int n) {
    String s = Integer.toString(n);
    String c = "";
    int k=0;

    	for(int i=0;i<s.length();){
    		if(k==1){
    			break;
    		}
    			
    		int count = 1;
    					
    		if((i==s.length()-1)){
    			c = c + count + s.charAt(i);
    			break;
    		}
    		else{
    			for(int j=i+1;j<s.length();){
    				if(s.charAt(i)==s.charAt(j)){
    					count++;
    					if(j==s.length()-1){
    						c = c + count + s.charAt(i);
    						k = 1;
    						break;
    					}
    					else
    						j++;
    				}
    				else{
    					c = c + count + s.charAt(i);
    					i=j;
    					break;
    				}
    				
    			}
    			
    			
    		}
    		
    	}
    	return c;
    }

  • 2
    S

    Read the descriptions carefully again! It's quite tricky.
    1,11,21,1211…… is a sequence. This problem asks you to return the nth element in this specific sequence.


Log in to reply
 

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