我想做的事情在下面的评论中描述。我怎么能有效地做到?
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
int n = Int32.Parse(Console.ReadLine());
bool[][] flags = new bool[26][n]; // I want this to be a 26 x n array of false values
for(int k = 0; k < n; ++k)
{
string line = Console.WriteLine();
for(int i = 0; i < line.Length; ++i)
flags[(int)line[i] - (int)'a'] = true;
}
int gems = flags.Count(arr => arr.Count(j => j == true) == arr.Length);
Console.WriteLine
}
}
答案 0 :(得分:7)
LINQ应该可以很好地实现您的目标:
NSUserDefaults
或者正如Jim Mischel所说:
bool[][] flags = Enumerable.Range(0, 26).Select(_ => Enumerable.Repeat(true, n).ToArray()).ToArray();
但是第一个示例使用的内存较少,因为bool[][] flags = Enumerable.Repeat(Enumerable.Repeat(true, n).ToArray(), 26).ToArray();
方法在遍历项目时不会重新定义Select
。
答案 1 :(得分:4)
您似乎对Multidimensional数组和Jagged Array(数组数组)感到困惑。
如果您正在寻找二维阵列,您可以这样做。
bool[,] flags = new bool[26,n];
//or
bool[,] flags = new bool[,];
bool[,] flags = new bool[26,];
如果是Jagged Arrays,你可以这样做。
bool[][] flags1 = new bool[26][];
flags1[0] = new bool[n]; // since you want n elements
flags1[1] = new bool[n];
...
// initialization
flags1[0] = new bool[] {true, false};
flags1[1] = new bool[] {false};