C# 当不同的容量参数被传递给列表构造函数C时,不同的程序输出#
我正在C#中实现一个稍微花哨的计数排序版本。“稍微有趣”的部分是,我将排序输出中的某些元素替换为“-”而不是原始值。以下是一个示例输入/输出对(可能的整数值范围在0到99之间): 在 出去 以下是我的实现:C# 当不同的容量参数被传递给列表构造函数C时,不同的程序输出#,c#,C#,我正在C#中实现一个稍微花哨的计数排序版本。“稍微有趣”的部分是,我将排序输出中的某些元素替换为“-”而不是原始值。以下是一个示例输入/输出对(可能的整数值范围在0到99之间): 在 出去 以下是我的实现: using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) {
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
List<List<string>> rsltLists = new List<List<string>>(100);
for(int i=0; i<n; i++)
{
rsltLists.Add(new List<String>()); // PROBLEM IS HERE
}
for(int a0 = 0; a0 < n; a0++)
{
string[] tokens_x = Console.ReadLine().Split(' ');
int x = Convert.ToInt32(tokens_x[0]);
string s = tokens_x[1];
if(a0 < n/2)
{
// Replace string with '-'
rsltLists[x].Add("-");
}
else
{
rsltLists[x].Add(s);
}
}
foreach(List<string> rsltList in rsltLists)
{
foreach(string rslt in rsltList)
{
Console.Write(rslt + " ");
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
类解决方案
{
静态void Main(字符串[]参数)
{
int n=Convert.ToInt32(Console.ReadLine());
列表rsltLists=新列表(100);
对于(int i=0;i而言,“内部”列表将始终恰好有两个元素,其中一个元素您希望将其视为数字而不是字符串。最好在此处使用一个小类甚至元组,而不是嵌套列表
我只使用VS2015,不支持元组,因此此代码未经检查,可能有一两个错误:
static void Main(String[] args)
{
int n = int.Parse(Console.ReadLine());
var data = new List<(int, string)>(n);
for(int a0 = 0; a0 < n; a0++)
{
var tokens = Console.ReadLine().Split(' ');
int x = int.Parse(tokens[0]);
if(a0 < n/2) tokens[1] = "-";
data.Add( (val: x, str: tokens[1]) )
}
foreach(var item in data.OrderBy(i => i.val))
{
Console.Write(item.str + " ");
}
}
static void Main(字符串[]args)
{
int n=int.Parse(Console.ReadLine());
var数据=新列表(n);
对于(int a0=0;a0i.val)中的var项)
{
Console.Write(item.str+“”);
}
}
对此有几点想法:
- 您正在为每个选项创建一个列表…但许多选项未被使用。只实例化您实际使用的列表如何
- 再加上上面的一个,您将创建100个列表,每个列表的容量为100…这是一个需要留出的大量内存,您将不会使用这些内存
一个解决方案:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
class Solution
{
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int threshold = n / 2;
List<string>[] stringMap = new List<string>[100];
for(int a0 = 0; a0 < n; a0++){
string[] tokens_x = Console.ReadLine().Split(' ');
int x = Convert.ToInt32(tokens_x[0]);
if(stringMap[x] == null)
{
stringMap[x] = new List<string>();
}
stringMap[x].Add((a0 >= threshold ? tokens_x[1] : "-"));
}
List<string> output = new List<string>();
for(int i = 0; i < stringMap.Length; i++)
{
if(stringMap[i] == null)
{
continue;
}
output.AddRange(stringMap[i]);
}
Console.WriteLine(string.Join(" ", output));
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
类解决方案
{
静态void Main(字符串[]参数)
{
int n=Convert.ToInt32(Console.ReadLine());
int阈值=n/2;
列表[]stringMap=新列表[100];
对于(int a0=0;a0=threshold?tokens_x[1]:“-”);
}
列表输出=新列表();
对于(int i=0;i
解决内存占用/处理时间过长的一种方法是将输入存储在分类词典中。键将是输入的整数部分,值将是一个列表,其中包含输入的另一部分(每个输入一项与键匹配)
然后,当我们填充字典时,我们可以按顺序输出每个列表
(排序字典
已经按键
排序)
通过这种方式,我们只创建实际需要的列表,每个列表只需要它需要的长度(我认为这两个都是导致原始代码中出现错误的原因,但我不知道在哪里可以找到要验证的实际测试用例代码)
private static void Main()
{
var length=Convert.ToInt32(Console.ReadLine());
var=长度/2;
var items=新的SortedDictionary();
对于(int-inputLine=0;inputLinei.Value));
//未提交至网站,但用于本地测试:
控制台。写入(“\n\n按任意键退出…”);
Console.ReadKey();
}
输出
为什么要创建n rsltlist
?这不是要求。有100个可能的值,数组更好。这里不应该使用n。x是100
for(int i=0; i<n; i++) // no, problem is here
{
rsltLists.Add(new List<String>()); // PROBLEM IS HERE
}
for(int i=0;i x!=null))
{
sb.Append(string.Join(“,ls)+”);
}
Debug.WriteLine(sb.ToString().TrimEnd());
使某人返回到字符串()TrimEnd();
}
如何失败?出现了什么错误?您指的是什么测试用例?您是否将输出与测试数据进行了比较?我很困惑。您分配RSLTLIST以保留100个插槽,然后将n
项写入其中。如果您提前知道需要多少插槽,为什么不保留n
插槽?您说大小是“巨大的”。对于一些人来说,超过1000是巨大的,而对于一些人来说,超过万亿是巨大的。不要说它是“巨大的”。说它有多大。这很重要。他使用所有的项目。他需要“-”作为额外的条目。“”在第20行作为三元处理。“额外的条目”是指数值(代码中的x)-限制允许0 hackerrank上的问题似乎不支持C#6元组,但逻辑看起来基本上是合理的。但是,我不确定它是否能通过效率要求,因为我相信LINQ的OrderBy
将不会像“计数排序”那样高效这就是问题的目的。此外,我不认为它保证是“稳定”排序。OrderBy()确实保证了稳定排序
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
class Solution
{
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int threshold = n / 2;
List<string>[] stringMap = new List<string>[100];
for(int a0 = 0; a0 < n; a0++){
string[] tokens_x = Console.ReadLine().Split(' ');
int x = Convert.ToInt32(tokens_x[0]);
if(stringMap[x] == null)
{
stringMap[x] = new List<string>();
}
stringMap[x].Add((a0 >= threshold ? tokens_x[1] : "-"));
}
List<string> output = new List<string>();
for(int i = 0; i < stringMap.Length; i++)
{
if(stringMap[i] == null)
{
continue;
}
output.AddRange(stringMap[i]);
}
Console.WriteLine(string.Join(" ", output));
}
}
private static void Main()
{
var length = Convert.ToInt32(Console.ReadLine());
var halfway = length / 2;
var items = new SortedDictionary<int, List<string>>();
for (int inputLine = 0; inputLine < length; inputLine++)
{
var input = Console.ReadLine().Split();
var sortIndex = Convert.ToInt32(input[0]);
var value = inputLine < halfway ? "-" : input[1];
if (items.ContainsKey(sortIndex)
{
items[sortIndex].Add(value);
}
else
{
items.Add(sortIndex, new List<string> {value});
}
}
Console.WriteLine(string.Join(" ", items.SelectMany(i => i.Value)));
// Not submitted to website, but for local testing:
Console.Write("\n\nPress any key to exit...");
Console.ReadKey();
}
for(int i=0; i<n; i++) // no, problem is here
{
rsltLists.Add(new List<String>()); // PROBLEM IS HERE
}
public static string HackerSort()
{
List<string> input = new List<string>() {"20"
, "0 ab"
, "6 cd"
, "0 ef"
, "6 gh"
, "4 ij"
, "0 ab"
, "6 cd"
, "0 ef"
, "6 gh"
, "0 ij"
, "4 that"
, "3 be"
, "0 to"
, "1 be"
, "5 question"
, "1 or"
, "2 not"
, "4 is"
, "2 to"
, "4 the" };
List<string>[] wl = new List<string>[100];
int n = int.Parse(input[0]);
int half = n/2;
char split = ' ';
for (int i = 0; i < n; i++)
{
string s = input[i + 1];
string[] ss = s.Split(split);
//Debug.WriteLine(ss[0]);
int row = int.Parse(ss[0]);
if(wl[row] == null)
{
wl[row] = new List<string>((n / 100) + 1);
}
wl[row].Add(i < half ? "-" : ss[1]);
}
StringBuilder sb = new StringBuilder();
foreach(List<string> ls in wl.Where(x => x != null))
{
sb.Append(string.Join(" ", ls) + ' ');
}
Debug.WriteLine(sb.ToString().TrimEnd());
return sb.ToString().TrimEnd();
}