输入参数为字符串型的 n维数组,列表的每一项值为数组 或 int型数字。数组内的数组,每一项值,也可以是数组 或 int型数字。
int型数字,表示数组嵌套的深度。
[[1], [2,3,4], [5,[2,3]], [7], [0,[1,2,3,4],3,5], [1,3], [3,2,4]]
3
n维数组的深度为3
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
const int MAX_N = 1 << 16; // 65536
int main(const int argc, const char* const argv[]) {
char input[MAX_N] = "";
gets(input);
char stk[20];
int top = -1, ans = 0;
const char* p = input;
while (*p) {
switch (*p) {
case '[':
*(stk + ++top) = '[';
ans = fmax(ans, top + 1);
break;
case ']':
--top;
break;
default:
break;
}
++p;
}
assert(top == -1);
return fprintf(stdout, "%d", ans), 0;
} import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] str = br.readLine().trim().toCharArray();
int maxDepth = 0;
int depth = 0;
for(int i = 0; i < str.length; i++){
if(str[i] == '['){
// 遇到左括号就将深度+1
depth ++;
}else if(str[i] == ']'){
// 遇到右括号先更新一下当前最大的深度,再平衡掉一个左括号重新计算当前的depth
maxDepth = Math.max(maxDepth, depth);
depth --;
}
}
System.out.println(maxDepth);
}
} import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
Stack<Character> stack = new Stack<>();
int maxSize=0;
for (int i = 0,len =s.length(); i < len ; i++) {
if (s.charAt(i)=='['){
stack.push('[');
maxSize=Math.max(maxSize,stack.size());
}else if (s.charAt(i)==']')
stack.pop();
}
System.out.println(maxSize);
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int count = 0, max = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '[') {
count++;
max = (max > count) ? max : count;
} else if (s.charAt(i) == ']')
count--;
}
System.out.println(max);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String s = input.nextLine();
int depth = 0;
int max = 0;
for (int i = 0;i<s.length();i++){
if (s.charAt(i) == '['){
depth++;
}else if (s.charAt(i) == ']'){
depth--;
}
if (depth > max)
max = depth;
}
System.out.println(max);
}
}
#include<bits/stdc++.h>
using namespace std;
char ch;
int main() {
int left = 0;
int ans = 0;
while (cin >> ch) {
if (ch == '[') {
++left;
ans = max(ans, left);
} else if (ch == ']') {
--left;
}
}
cout << ans << endl;
return 0;
}
class MainActivity:
def main(self):
# Read the data
s = input()
# Initialization
stack = []
result = 0
# Traverse
for char in s:
if char in {'[', ']'}:
if char == '[':
stack.append('[')
result = max(result, len(stack))
else:
if stack:
stack.pop()
print(result)
if __name__ == '__main__':
M = MainActivity()
M.main() package main
import (
"fmt"
"os"
"bufio"
)
var in=bufio.NewReader(os.Stdin)
func main() {
s,_:=in.ReadString('\n')
stk:=[]byte{}
max:=0
for _,ch:=range []byte(s){
if ch=='['||ch==']'{
if len(stk)>0&&stk[len(stk)-1]=='['&&ch==']'{
stk=stk[:len(stk)-1]
}else{
stk=append(stk,ch)
}
}
if len(stk)>max{
max=len(stk)
}
}
fmt.Print(max)
} #include <iostream>
#include <string>
int getMax(std::string exp)
{
int result = 0, temp = 0;
for (char x : exp)
{
if (x == '[')
{
temp++;
if (temp > result)
{
result = temp;
}
}
else if (x == ']')
{
temp--;
}
}
return result;
}
int main()
{
std::string exp;
std::getline(std::cin, exp);
std::cout << getMax(exp) << std::endl;
return 0;
} #include<iostream>
(720)#include<string>
#include<stack>
(850)#include<algorithm>
using namespace std;
const int MaxLen = 100000;
int main(void){
string s;
string temp;
stack<char> st;
int ans = 0;
char str[MaxLen];
while (scanf("%s", str) == 1){
string s1(str);
temp += s1;
}
for (int i = 0; i < temp.size(); i++){
if (temp[i] == '[')
st.push('[');
else if(temp[i] == ']'){
ans = max(ans, int(st.size()));
st.pop();
}
}
cout<<ans<<endl;
return 0;
}
def find(s): q=[] res=0 for i in s: if i=='[': q.append(i) elif i==']': q.pop() res=max(res,len(q)) print(res) find(input())
一种笨办法,但是我觉得可行?
var arr=eval(readline())
// 提示 请检查是否存在语法错误或者数组越界非法访问等情况
// 通过率只有86%,什么原因??
function mynum(arr){
// 数组
if(Object.prototype.toString.call(arr)=='[object Array]'){
if(arr.length==0){
return 1
}else{
let myarr=[]
arr.forEach((item)=>{
if(Object.prototype.toString.call(item)=='[object Array]'){
myarr.push(mynum(item))
}else{
myarr.push(0)
}
})
return 1+Math.max.apply(null,myarr)
}
}else{
// 不是数组的时候返回 0
return 0
}
}
console.log(mynum(arr)) #include <iostream>
#include <string>
using namespace std;
int arrayMaxDepth(string s)
{
int i = 0;
int maxDepth = 0;
int temp = 0;
while (s[i] != '\0'){
if (s[i] == '['){
temp++;
i++;
}
else if (s[i] == ']') {
temp--;
i++;
}
else {
i++;
}
if (temp > maxDepth){
maxDepth = temp;
}
}
return maxDepth;
}
int main()
{
string s;
getline(cin,s);
cout << arrayMaxDepth(s);
return 0;
} import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = bf.readLine();
int count = 0;
int max = 0;
for(int i =0;i < str.length();i++){
if(str.charAt(i) == '['){
count++;
if(count > max){
max = count;
}
}
if(str.charAt(i) == ']'){
count--;
}
}
System.out.println(max);
}
} leetcode上面的一道题很类似,拿走不谢:https://leetcode-cn.com/problems/remove-outermost-parentheses/