添加不规则二维整型数组
This commit is contained in:
parent
d932a6878f
commit
5898bf9e3d
@ -2,10 +2,6 @@ package com.code.leet.entiy;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
@ -18,7 +14,15 @@ public class TwoArray {
|
||||
|
||||
int[][] arr;
|
||||
|
||||
public TwoArray(String str) {
|
||||
public TwoArray(String str, boolean bl) {
|
||||
if (bl) {
|
||||
getSquArr(str);
|
||||
} else {
|
||||
getArr(str);
|
||||
}
|
||||
}
|
||||
|
||||
private void getSquArr(String str) {
|
||||
int xLength = str.length() - str.replace("[", "").length() - 1;
|
||||
int yLength = (str.length() - str.replace(",", "").length() - xLength + 1) / xLength + 1;
|
||||
arr = new int[xLength][yLength];
|
||||
@ -41,4 +45,21 @@ public class TwoArray {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void getArr(String str) {
|
||||
str = str.substring(2, str.length() - 2);
|
||||
String[] strings = str.split("],\\[");
|
||||
arr = new int[strings.length][];
|
||||
for (int i = 0; i < strings.length; i++) {
|
||||
if("".equals(strings[i])){
|
||||
arr[i] = new int[0];
|
||||
continue;
|
||||
}
|
||||
String[] strs = strings[i].split(",");
|
||||
arr[i] = new int[strs.length];
|
||||
for (int j = 0; j < strs.length; j++) {
|
||||
arr[i][j] = Integer.parseInt(strs[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ public class NetworkDelayTime {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new NetworkDelayTime().new Solution();
|
||||
TwoArray twoArray = new TwoArray("[[2,1,1],[2,3,1],[3,4,1]]");
|
||||
TwoArray twoArray = new TwoArray("[[2,1,1],[2,3,1],[3,4,1]]",true);
|
||||
System.out.println(solution.networkDelayTime(twoArray.getArr(), 4, 2));
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,7 @@ public class NumberOfClosedIslands {
|
||||
"[1,0,0,0,0,1,1,0]," +
|
||||
"[1,0,1,0,1,1,1,0]," +
|
||||
"[1,0,0,0,0,1,0,1]," +
|
||||
"[1,1,1,1,1,1,1,0]]"
|
||||
"[1,1,1,1,1,1,1,0]]",true
|
||||
);
|
||||
System.out.println(solution.closedIsland(twoArray.getArr()));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user