有效地删除整数列表(c#或vb.net)中的最后零
我正在删除整数列表中最右边的零(它们只能是0或1),如下所示:有效地删除整数列表(c#或vb.net)中的最后零,c#,vb.net,list,trim,C#,Vb.net,List,Trim,我正在删除整数列表中最右边的零(它们只能是0或1),如下所示: For i As Integer = Product.Count - 1 To 0 Step -1 If Product(i) = 0 Then Product.RemoveAt(i) Else Exit For End If Next 你能提出一个比我现在做的事情更简单、更有效的解决方案吗 如果可能的话,我还希望看到一个保留1 0的替代解决方案,如果列表只包含1 0和1 0
For i As Integer = Product.Count - 1 To 0 Step -1
If Product(i) = 0 Then
Product.RemoveAt(i)
Else
Exit For
End If
Next
你能提出一个比我现在做的事情更简单、更有效的解决方案吗
如果可能的话,我还希望看到一个保留1 0的替代解决方案,如果列表只包含1 0和1 0。谢谢(语言,vb.net的c#是无关紧要的)
(请注意,作为问题的一部分,“效率”是一个客观标准,不是基于观点。)我不确定它是否更优雅,但它确实使用了LINQ
int lastOne = i.IndexOf(i.Last(n => n == 1));
List<int> trimmedList = i.Take(lastOne + 1); //Account for zero-based index
这将删除单零情况,在这种情况下,我将使用三元:
i.All(n => n == 0) ? i.Take(1) : i.Reverse().SkipWhile(i=> i==0).Reverse();
我不确定它是否更优雅,但它确实使用了LINQ
int lastOne = i.IndexOf(i.Last(n => n == 1));
List<int> trimmedList = i.Take(lastOne + 1); //Account for zero-based index
这将删除单零情况,在这种情况下,我将使用三元:
i.All(n => n == 0) ? i.Take(1) : i.Reverse().SkipWhile(i=> i==0).Reverse();
我不确定它是否更优雅,但它确实使用了LINQ
int lastOne = i.IndexOf(i.Last(n => n == 1));
List<int> trimmedList = i.Take(lastOne + 1); //Account for zero-based index
这将删除单零情况,在这种情况下,我将使用三元:
i.All(n => n == 0) ? i.Take(1) : i.Reverse().SkipWhile(i=> i==0).Reverse();
我不确定它是否更优雅,但它确实使用了LINQ
int lastOne = i.IndexOf(i.Last(n => n == 1));
List<int> trimmedList = i.Take(lastOne + 1); //Account for zero-based index
这将删除单零情况,在这种情况下,我将使用三元:
i.All(n => n == 0) ? i.Take(1) : i.Reverse().SkipWhile(i=> i==0).Reverse();
如果值只能为0或1,则可以搜索最后一个1并删除其后的所有内容
Dim index As Integer
index = Product.LastIndexOf(1) + 1
Product.RemoveRange(index, Product.Count - index)
如果列表仅包含0,则索引将等于0,因此可以在RemoveRange之前添加If语句
Dim index As Integer
index = Product.LastIndexOf(1) + 1
If index = 0 Then index = 1
Product.RemoveRange(index, Product.Count - index)
(如果列表中没有项目,则可能会崩溃)
更新
因为它正在搜索1,所以前0的索引将是返回的索引+1。这也解决了函数返回-1时全部为零的问题。如果值只能为0或1,则可以搜索最后一个1并删除之后的所有内容
Dim index As Integer
index = Product.LastIndexOf(1) + 1
Product.RemoveRange(index, Product.Count - index)
如果列表仅包含0,则索引将等于0,因此可以在RemoveRange之前添加If语句
Dim index As Integer
index = Product.LastIndexOf(1) + 1
If index = 0 Then index = 1
Product.RemoveRange(index, Product.Count - index)
(如果列表中没有项目,则可能会崩溃)
更新
因为它正在搜索1,所以前0的索引将是返回的索引+1。这也解决了函数返回-1时全部为零的问题。如果值只能为0或1,则可以搜索最后一个1并删除之后的所有内容
Dim index As Integer
index = Product.LastIndexOf(1) + 1
Product.RemoveRange(index, Product.Count - index)
如果列表仅包含0,则索引将等于0,因此可以在RemoveRange之前添加If语句
Dim index As Integer
index = Product.LastIndexOf(1) + 1
If index = 0 Then index = 1
Product.RemoveRange(index, Product.Count - index)
(如果列表中没有项目,则可能会崩溃)
更新
因为它正在搜索1,所以前0的索引将是返回的索引+1。这也解决了函数返回-1时全部为零的问题。如果值只能为0或1,则可以搜索最后一个1并删除之后的所有内容
Dim index As Integer
index = Product.LastIndexOf(1) + 1
Product.RemoveRange(index, Product.Count - index)
如果列表仅包含0,则索引将等于0,因此可以在RemoveRange之前添加If语句
Dim index As Integer
index = Product.LastIndexOf(1) + 1
If index = 0 Then index = 1
Product.RemoveRange(index, Product.Count - index)
(如果列表中没有项目,则可能会崩溃)
更新
因为它正在搜索1,所以前0的索引将是返回的索引+1。这也解决了函数返回全零时返回-1的问题。只需修剪:)
只是修剪:)
只是修剪:)
只是修剪:)
我会这样做:
public void RemoveTrailingZeros( List<int> digits )
{
int i = digits.Count ;
while ( --i >= 0 && digits[i] == 0 )
{
digits.RemoveAt(i) ;
}
return ;
}
public void removetrailingzero(列表数字)
{
int i=数字。计数;
而(--i>=0&&digits[i]==0)
{
数字。删除(i);
}
返回;
}
更整洁:
static void RemoveTrailingZeroes( List<int> digits )
{
int i = 1 + digits.FindLastIndex( x => x != 0 ) ;
digits.RemoveRange(i,digits.Count-i) ;
return ;
}
static void removetrailingzero(列表数字)
{
inti=1+位数。FindLastIndex(x=>x!=0);
数字。删除范围(i,数字。计数-i);
返回;
}
我会这样做:
public void RemoveTrailingZeros( List<int> digits )
{
int i = digits.Count ;
while ( --i >= 0 && digits[i] == 0 )
{
digits.RemoveAt(i) ;
}
return ;
}
public void removetrailingzero(列表数字)
{
int i=数字。计数;
而(--i>=0&&digits[i]==0)
{
数字。删除(i);
}
返回;
}
更整洁:
static void RemoveTrailingZeroes( List<int> digits )
{
int i = 1 + digits.FindLastIndex( x => x != 0 ) ;
digits.RemoveRange(i,digits.Count-i) ;
return ;
}
static void removetrailingzero(列表数字)
{
inti=1+位数。FindLastIndex(x=>x!=0);
数字。删除范围(i,数字。计数-i);
返回;
}
我会这样做:
public void RemoveTrailingZeros( List<int> digits )
{
int i = digits.Count ;
while ( --i >= 0 && digits[i] == 0 )
{
digits.RemoveAt(i) ;
}
return ;
}
public void removetrailingzero(列表数字)
{
int i=数字。计数;
而(--i>=0&&digits[i]==0)
{
数字。删除(i);
}
返回;
}
更整洁:
static void RemoveTrailingZeroes( List<int> digits )
{
int i = 1 + digits.FindLastIndex( x => x != 0 ) ;
digits.RemoveRange(i,digits.Count-i) ;
return ;
}
static void removetrailingzero(列表数字)
{
inti=1+位数。FindLastIndex(x=>x!=0);
数字。删除范围(i,数字。计数-i);
返回;
}
我会这样做:
public void RemoveTrailingZeros( List<int> digits )
{
int i = digits.Count ;
while ( --i >= 0 && digits[i] == 0 )
{
digits.RemoveAt(i) ;
}
return ;
}
public void removetrailingzero(列表数字)
{
int i=数字。计数;
而(--i>=0&&digits[i]==0)
{
数字。删除(i);
}
返回;
}
更整洁:
static void RemoveTrailingZeroes( List<int> digits )
{
int i = 1 + digits.FindLastIndex( x => x != 0 ) ;
digits.RemoveRange(i,digits.Count-i) ;
return ;
}
static void removetrailingzero(列表数字)
{
inti=1+位数。FindLastIndex(x=>x!=0);
数字。删除范围(i,数字。计数-i);
返回;
}
就效率而言,你已经无能为力了。您的解决方案已经是O(n),您无法再做得更好了。至于优雅,这是非常主观的.“去掉最右边的零”是什么意思?从您的代码来看,似乎您只需要1?虽然假设它封装到单个方法中,但我觉得这很好。你的解决方案已经是O(n),我理解它,尽管我对vb.net的理解是最低限度的,这意味着可读性很好,甚至对我来说也很优雅。请不要低估您的解决方案。你可能会得到很多答案,但就效率而言,你的算法将胜过一切。谢谢@Abhi。很高兴听到你这么说。与此同时,我可以看到许多有趣的提议正在出台,我将饶有兴趣地研究它们。在我上面的代码中,for可以运行到1而不是0,以“保留”product=0时的大小写。稍后,我将以各位专家更大的共识来标记答案。就效率而言,你能做的已经不多了。您的解决方案已经是O(n),您无法再做得更好了。至于优雅,这是非常主观的.“去掉最右边的零”是什么意思?从你的代码看来,你只是想要1?它是l