ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

leetcode_6_ZigZag Conversion
2015-07-20 17:23:19 À´Ô´: ×÷Õß: ¡¾´ó ÖРС¡¿ ä¯ÀÀ:1´Î
Tags£ºleetcode_6_ZigZag Conversion

ÃèÊö:

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R
And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

˼·£º

ÏëÁ˺þã¬Ë¼Î¬×ÜÊǾÖÏÞÔÚ¶þάÊý×飬ÕÒ×Ö·û´®µÄ³¤¶ÈºÍ¶þάÊý×éµÄÐÐÁÐÊýÖ®¼äµÄijÖÖÁªÏµ£¬ÏëÁ˺þã¬Ã»ÓÐ˼·¡£

È»ºó£¬È»ºó¾ÍÉÏÍø¿´ÁËһϣ¬ÓÐÒ»ÖÖ˼·˵ÊÇÓÃ×Ö·û´®Êý×é¼´¿É£¬¾ÍÏëµ½ÁËStringBuilder£¬Ö±½ÓAppend¶àºÃ£¬ÕâµÃ±È¶þάÊý×é¸ß¼¶¶àÉÙ°¡£¡È»ºó¾ÍÓÃStringBuilder×öÕâµÀÌâÁË¡£

´úÂ룺

public String convert(String s, int nRows) {
        if(s==null)
        	return null;
        else if (s.equals(""))
        	return "";
        int len=s.length();
        StringBuilder resultBuilder=new StringBuilder();
        StringBuilder []sBuilder=new StringBuilder[nRows];
        for(int i=0;i
  
   =1;j--)
        	{
        		if(i==len)
        			break;
        		sBuilder[j].append(s.charAt(i));
        		i++;
        	}
        }
        for(i=0;i
   
    

½á¹û£º



¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
·ÖÏíµ½: 
ÉÏһƪ£ºleetcode_8_String to Integer (a.. ÏÂһƪ£º[C++]LeetCode: 129 Clone Graph ..

ÆÀÂÛ

ÕÊ¡¡¡¡ºÅ: ÃÜÂë: (ÐÂÓû§×¢²á)
Ñé Ö¤ Âë:
±í¡¡¡¡Çé:
ÄÚ¡¡¡¡ÈÝ:

¡¤Spring Boot Java£º (2025-12-26 16:20:19)
¡¤Spring Boot¤ÇHello (2025-12-26 16:20:15)
¡¤Spring ¤Î»ù±¾¤«¤éŒ (2025-12-26 16:20:12)
¡¤C++Ä£°å (template) (2025-12-26 15:49:49)
¡¤C ÓïÑÔÖÐÄ£°åµÄ¼¸ÖÖ (2025-12-26 15:49:47)