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)java

P   A   H   N
A P L S I I G
Y   I   R

And then read line by line: "PAHNAPLSIIGYIR"python

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

string convert(string s, int numRows);

Example 1:数组

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Example 2:安全

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:

P     I    N
A   L S  I G
Y A   H R
P     I
 

学习java编程app

class Solution {
    public String convert(String s, int numRows) {
        if (numRows == 1) return s;

        List<StringBuilder> rows = new ArrayList<>();
        for (int i = 0; i < Math.min(numRows, s.length()); i++)
            rows.add(new StringBuilder());

        int curRow = 0;
        boolean goingDown = false;

        for (char c : s.toCharArray()) {
            rows.get(curRow).append(c);
            if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown;
            curRow += goingDown ? 1 : -1;
        }

        StringBuilder ret = new StringBuilder();
        for (StringBuilder row : rows) ret.append(row);
        return ret.toString();
        
    }
}

1. 列表对象new ArrayList<>() add    字符串对象 new StringBuilder()   append性能

2. for(char c:s.toCharArray())学习

3. toString()ui

class Solution:
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows == 1 or numRows >= len(s):
            return s

        res=['']*numRows
    
        index=0
        for i in s:
            res[index]+=i
            if index==0:
                step=1
            elif index==numRows-1:
                step=-1
            index=index+step
        return ''.join(res)


list  arraylist区别this

数组、List和ArrayList的区别数组在内存中是连续存储的,因此它的索引速度是很是的快,并且赋值与修改元素也很简单,好比:string[] s=new string[3];//赋值s[0]="a"; s[1]="b"; s[2]="c";//修改s[1]="b1";可是数组也存在一些不足的地方。好比在数组的两个数据间插入数据也是很麻烦的,还有咱们在声明数组的时候,必须同时指明数组的长度,数组的长度过长,会形成内存浪费,数组和长度太短,会形成数据溢出的错误。这样若是在声明数组时咱们并不清楚数组的长度,就变的很麻烦了。C#中最早提供了ArrayList对象来克服这些缺点。ArrayList是.Net Framework提供的用于数据存储和检索的专用类,它是命名空间System.Collections下的一部分。它的大小是按照其中存储的数据来动态扩充与收缩的。因此,咱们在声明ArrayList对象时并不须要指定它的长度。ArrayList继承了IList接口,因此它能够很方便的进行数据的添加,插入和移除.好比:ArrayList list = new ArrayList();//新增数据list.Add("abc"); list.Add(123);//修改数据list[2] = 345;//移除数据list.RemoveAt(0);//插入数据 list.Insert(0, "hello world");从上面示例看,ArrayList好像是解决了数组中全部的缺点,那么它应该就是完美的了,为何在C#2.0后又会出现List呢?在list中,咱们不只插入了字符串"abc",并且又插入了数字123。这样在ArrayList中插入不一样类型的数据是容许的。由于ArrayList会把全部插入其中的数据都看成为object类型来处理。这样,在咱们使用ArrayList中的数据来处理问题的时候,极可能会报类型不匹配的错误,也就是说ArrayList不是类型安全的。既使咱们保证在插入数据的时候都很当心,都有插入了同一类型的数据,但在使用的时候,咱们也须要将它们转化为对应的原类型来处理。这就存在了装箱与拆箱的操做,会带来很大的性能损耗。装箱与拆箱的概念: 简单的来说: 装箱:就是将值类型的数据打包到引用类型的实例中 好比将int类型的值123赋给object对象oint i=123; object o=(object)i;拆箱:就是从引用数据中提取值类型 好比将object对象o的值赋给int类型的变量iobject o=123; int i=(int)o;装箱与拆箱的过程是很损耗性能的。正是由于ArrayList存在不安全类型与装箱拆箱的缺点,因此在C#2.0后出现了泛型的概念。而List类是ArrayList类的泛型等效类。它的大部分用法都与ArrayList类似,由于List类也继承了IList接口。最关键的区别在于,在声明List集合时,咱们同时须要为其声明List集合内数据的对象类型。 好比:List<int> list = new List<int>();//新增数据list.Add(123);//修改数据 list[0] = 345;//移除数据list.RemoveAt(0);上例中,若是咱们往List集合中插入string字符"hello world",IDE就会报错,且不能经过编译。这样就避免了前面讲的类型安全问题与装箱拆箱的性能问题了。同时 List不能被构造,但能够向上面那样为List建立一个引用,而ListArray就能够被构造。 List list;     //正确   list=null; List list=new List();    //   是错误的用法List list = new ArrayList();这句建立了一个ArrayList的对象后把上溯到了List。此时它是一个List对象了,有些ArrayList有可是List没有的属性和方法,它就不能再用了。 而ArrayList list=new ArrayList();建立一对象则保留了ArrayList的全部属性。 List泛型的好处:经过容许指定泛型类或方法操做的特定类型,泛型功能将类型安全的任务从您转移给了编译器。不须要编写代码来检测数据类型是否正确,由于会在编译时强制使用正确的数据类型。减小了类型强制转换的须要和运行时错误的可能性。泛型提供了类型安全但没有增长多个实现的开销。
相关文章
相关标签/搜索