C#在提供的索引点内搜索数组

时间:2015-08-08 10:16:29

标签: c# arrays algorithm indexing

我不确定如何最好地说出来。我有一个近80,000个单词的文本文件,我已将其转换为字符串数组。

基本上我想要一个方法,我将它传递给一个单词并检查它是否在单词字符串数组中。为了保存它每次搜索80,000,我索引了以每个字母开头的单词以二维数组开头和结尾的位置。所以当' A'时,wordIndex [0,0] = 0单词开始,wordIndex [1,0] = 4407是他们结束的地方。然后wordIndex [0,1] = 4408,这是以' B'开始等。

我想知道的是如何将此范围呈现给一种方法来搜索值。我知道我可以给出一个索引和长度,但这是唯一的方法吗?我可以说在y和z范围内寻找x吗?

2 个答案:

答案 0 :(得分:3)

看看Trie set。它可以帮助您使用少量内存和快速搜索存储许多单词。 Here是很好的实施。

答案 1 :(得分:1)

基本上你可以使用string word = "apple"; int start = 0; int end = 4407; bool found = false; for (int i = start; i <= end ; i++) { if (arrayOfWords[i] == word) { found = true; break; } } 循环来搜索数组的一部分:

angular.module('sample', [])
.directive('countdown', function () {

function CountdownController() {
    var countdownController = this;

    countdownController.digit = 5;

    setTimeout(function () {
        console.log('fired');
        countdownController.digit = 200;
    }, 3000);

}


return {
    restrict: 'E',
    controller: CountdownController,
    controllerAs: 'countdownController',
    bindToController: true,
    template: '{{countdownController.digit}}'
}
});

但是,由于索引的描述意味着您的数组已经排序,因此更好的方法可能是使用this