如何讀取leetcode中的二維陣列
leetcoe中二維陣列的輸入為如下形式,在本地測試的時候讀取很不方便,簡單寫了個函式封裝了讀取的方法
[
[“5”,“3”,".",".",“7”,".",".",".","."],
[“6”,".",".",“1”,“9”,“5”,".",".","."],
[".",“9”,“8”,".",".",".",".",“6”,"."],
[“8”,".",".",".",“6”,".",".",".",“3”],
[“4”,".",".",“8”,".",“3”,".",".",“1”],
[“7”,".",".",".",“2”,".",".",".",“6”],
[".",“6”,".",".",".",".",“2”,“8”,"."],
[".",".",".",“4”,“1”,“9”,".",".",“5”],
[".",".",".",".",“8”,".",".",“7”,“9”]
]
使用C++讀取該輸入,並返回一個二維陣列
#include <iostream>
#include<vector>
#include<stack>
#include<string>
using namespace std;
vector<vector<string>> leetcode2DInput() {
vector<string> row;
vector<vector<string>> output;
stack<char> parentheses;
stack<char> quota;
parentheses.push('0');
string S;
cin >> S;
while (1) {
string tempString="";
for (int i = 0; i < S.length(); i++) {
if (S.at(i) == '[') {
parentheses.push(S.at(i));
}
else if (S.at(i) == ']') {
row.push_back(tempString);
parentheses.pop();
tempString = "";
}
else {
if (parentheses.size() == 3) {
string temp(1, S.at(i));
tempString.append(temp);
}
}
}
if (parentheses.size() != 1) {
cin >> S;
}
else {
break;
}
}
row.pop_back();
string tempString="";
vector<string> tempVector;
for (int i = 0; i < row.size(); i++) {
quota.push(row.at(i).at(0));
for (int j = 1; j < row.at(i).length(); j++) {
if (row.at(i).at(j)=='\"'&"a.size()!=0) {
tempVector.push_back(tempString);
tempString = "";
quota.pop();
}
else if (row.at(i).at(j) == '\"'&"a.size() == 0) {
quota.push('\"');
}
else if (row.at(i).at(j) != '\"'&"a.size() != 0) {
string temp(1,row.at(i).at(j));
tempString.append(temp);
}
else {
continue;
}
}
output.push_back(tempVector);
tempVector.clear();
}
for (int i = 0; i < output.size(); i++) {
for (int j = 0; j < output.at(i).size(); j++) {
cout << output.at(i).at(j)<<" ";
}
cout << endl;
}
return output;
}
int main()
{
leetcode2DInput();
}
讀取結果
5 3 . . 7 . . . .
6 . . 1 9 5 . . .
. 9 8 . . . . 6 .
8 . . . 6 . . . 3
4 . . 8 . 3 . . 1
7 . . . 2 . . . 6
. 6 . . . . 2 8 .
. . . 4 1 9 . . 5
. . . . 8 . . 7 9
相關文章
- 二維陣列的獲取陣列
- C#中如何獲取二維陣列的行數和列數?C#陣列
- JavaSE 陣列:一維陣列&二維陣列Java陣列
- 二維陣列中的查詢陣列
- 陣列記憶體地址解讀及二維陣列的遍歷陣列記憶體
- 【JZOF】二維陣列中的查詢陣列
- js 一維陣列轉二維陣列JS陣列
- js 二維陣列轉一維陣列JS陣列
- 二維陣列陣列
- LeetCode 如何不使用額外空間去更新二維陣列LeetCode陣列
- Python如何對二維陣列求和Python陣列
- C/C++ 二維陣列的理解(多維陣列)C++陣列
- PHP中二維陣列與多維陣列PHP陣列
- 二維陣列排序陣列排序
- Java二維陣列Java陣列
- vector 二維陣列陣列
- C/C++獲取二維陣列行列數C++陣列
- 【劍指offer】二維陣列中的查詢陣列
- 求二維陣列中最大子陣列的和陣列
- 二維陣列和稀疏陣列互轉陣列
- php 二維陣列排序PHP陣列排序
- 二維陣列查詢陣列
- 二維樹狀陣列陣列
- 二維陣列行排序陣列排序
- 二維陣列練習陣列
- Java 二維陣列轉一維Java陣列
- 指標陣列和陣列指標與二維陣列指標陣列
- C的二維陣列(習題)陣列
- 【劍指offer】【1】二維陣列中的查詢陣列
- 二維陣列的指標的理解陣列指標
- LeetCode 74——搜尋二維矩陣LeetCode矩陣
- 7-蛇形二維陣列陣列
- JAVA基礎--二維陣列Java陣列
- Kotlin建立二維Int陣列Kotlin陣列
- 二維陣列與指標陣列指標
- 二維陣列中的查詢——牛客劍指offer陣列
- 劍指 Offer 04. 二維陣列中的查詢陣列
- Java 學習筆記 二維陣列和物件陣列Java筆記陣列物件