如何以更加数学的方式解决此状态检查? [英] How do I solve this status check in a more mathematical way?
本文介绍了如何以更加数学的方式解决此状态检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以解决这个问题:
private static string _positions;
//初始化_positions为'1'取决于项目(最多500项)
_positions = new string('1',items);
//如果一定条件满足,则设置该特定项目空白
_positions =新的StringBuilder的索引(_positions){[I] = ''}的ToString() ;
//测试_positions为空白,则所有的项目处理
如果(string.IsNullOrWhiteSpace(_positions))
{
DoSomething的();
}
有点像BitArray和0或类似的测试?它应该明显更快。我的问题是最多500件。
提前谢谢
我有什么试过:
这个和其他已知的解决方案。但我的问题是测试的高量的物品。
解决方案
写类,保持一个整数数组(或其他),说ARR
。 ctor会将所有数组项设置为默认状态(比如0
)和计数器
变量设置为0
。
然后提供一个更改项目的方法,比如setOne(int index)
。
在每个setOne
调用时,如果项目为0
,则将其更改为1
和计数器
递增;另一方面,如果它已经1
那么没有任何反应。
然后'全局空'测试将降低至(计数器== arr.Length)
CPallini您好,
很好的解决方案。非常感谢你!这是我生成的快速代码:
静态内部类PositionCheck
{
static private int _counter;
static internal int Counter {get => _计数器; }
static private char [] _arr;
static internal void SetItemCount(int items)
{
_arr = new char [items];
_counter = items;
}
static internal void Set(int index)
{
if(_arr [index] =='\ 0')
{
_arr [index] ='1';
_counter--;
}
}
静态内部bool Get(int index)
{
return _arr [index] =='\ 0';
}
}
Ken
Hi,
can i solve this:
private static string _positions; //Initialize _positions with '1' depending on items (up to 500 items) _positions = new string('1', items); //If a certain condition is fulfilled, then set the index of that specific item to blank _positions = new StringBuilder(_positions) { [i] = ' ' }.ToString(); //Test _positions for whitespace, then all items handled if (string.IsNullOrWhiteSpace(_positions)) { DoSomething(); }
with something like BitArray and test for 0 or similar? It should be significantly faster. My problem are the up to 500 items.
Thanks in advance
What I have tried:
This and other known solutions. But my problem is the high amount of items to test for.
解决方案
Write a class, holding an array of integers (or whatever), sayarr
. The ctor would set all the array items in default state (say0
) and acounter
variable to0
.
Then provide a method to change an item, saysetOne(int index)
.
At eachsetOne
call, if the item is0
then it is changed to1
andcounter
incremented; on the other hand, if it is already1
then nothing happens.
The 'global empty' test then would reduce to(counter == arr.Length)
.
Hi CPallini,
nice solution. Thank you very much! Here is my resulting quick code:
static internal class PositionCheck { static private int _counter; static internal int Counter { get => _counter; } static private char[] _arr; static internal void SetItemCount(int items) { _arr = new char[items]; _counter = items; } static internal void Set(int index) { if(_arr[index] == '\0') { _arr[index] = '1'; _counter--; } } static internal bool Get(int index) { return _arr[index] == '\0'; } }
Ken
这篇关于如何以更加数学的方式解决此状态检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文