有人可以幫我找出這個編譯錯誤的最佳重載方法匹配 'System.Collections.Generic.Dictionary <INT,System.Collections.Generic.Dictionary <string,int>> .Dictionary(INT)'
編譯錯誤(第10行,第17列):最佳重載方法匹配 for 'System.Collections.Generic.Dictionary> .Dictionary(int)' 有一些無效參數編譯錯誤(第13行,第5列): 參數1:無法從 'System.Collections.Generic.IEnumerable >>' 轉換爲'int'上次運行:8:16:27 pm編譯:0s執行:0.188s內存:0B CPU:0
是指向我的代碼Enumerable.Range(1,21)
部分
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
int N = Int32.Parse(Console.ReadLine());
var counter = new Dictionary<int, Dictionary<string, int>>
(
Enumerable.Range(1,21)
.Select(i => new KeyValuePair<int, Dictionary<string, int>>(i, new Dictionary<string, int>()))
);
for(int i = 0; i < N; ++i)
{
string[] input = Console.ReadLine().Split(' ');
switch(input[0])
{
case "add":
for(int j = 1; j < input[1].Length; ++j)
{
string sub = input[1].Substring(0,j);
if(counter[j].ContainsKey(sub))
counter[j][sub] += 1;
else
counter[j][sub] = 1;
}
break;
case "find":
Console.WriteLine
(
counter[input[1].Length].ContainsKey(input[1])
? counter[input[1].Length][input[1]]
: 0
);
break;
default:
break;
}
}
}
}
我想初始化的鍵值對的字典
[1] = new Dictionary<string,int>(),
[2] = new Dictionary<string,int>(),
.
.
.
[21] = new Dictionary<string,int>()
此外,我很好奇C#是否具有更好的數據結構,以便通過快速查找子字符串來保存字符串集合(針對此問題https://www.hackerrank.com/challenges/contacts)。
可以使用'Enumerable.Range(1,21).ToDictionary(T => T,新詞典());' –
Saravanan
@Saravanan甲小的修正中所共享的代碼。它應該是'var counter = Enumerable.Range(1,21).ToDictionary(t => t,t => new Dictionary());' –
@ChetanRanpariya:明白了:) – Saravanan