LeetCode string array of turn Java template

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/pfdvnah/article/details/102573599

Examples

: Parameter string as shown below

[1,2,3,4,5,0,6,7,8,9]

Return Value: the form of the intarray

1 2 3 4 5 0 6 7 8 9

Java code

Code from LeetCode template. In order to facilitate debugging local @wowpH, copy down so used.

/*
原文链接:https://blog.csdn.net/pfdvnah/article/details/102573599
作者:wowpH
CSDN-ID:pfdvnah
*/
public class StringToIntegerArray {
    public static int[] stringToIntegerArray(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.length() == 0) {
          return new int[0];
        }
    
        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        for(int index = 0; index < parts.length; index++) {
            String part = parts[index].trim();
            output[index] = Integer.parseInt(part);
        }
        return output;// 返回数组。@pfdvnah
    }
}
- End - wowpH - pfdvnah -

Guess you like

Origin blog.csdn.net/pfdvnah/article/details/102573599