如何优化这个嵌套的 for 循环?
如何优化这个嵌套的 for 循环?
程序应遍历从单词文本文件创建的数组中的每个单词,如果它大于 8 个字符,则将其添加到 goodWords
数组中。但需要注意的是,我只希望根词位于 goodWords 数组中,例如:
如果将greet 添加到数组中,我不想要greets 或greetings 或greeters 等。
NSString *string = [NSString stringWithContentsOfFile:@"/Users/james/dev/WordParser/word.txt" encoding:NSUTF8StringEncoding error:NULL];
NSArray *words = [string componentsSeparatedByString:@"\r\n"];
NSMutableArray *goodWords = [NSMutableArray array];
BOOL shouldAddToGoodWords = YES;
for (NSString *word in words)
{
NSLog(@"Word: %@", word);
if ([word length] > 8)
{
NSLog(@"Word is greater than 8");
for (NSString *existingWord in [goodWords reverseObjectEnumerator])
{
NSLog(@"Existing Word: %@", existingWord);
if ([word rangeOfString:existingWord].location != NSNotFound)
{
NSLog(@"Not adding...");
shouldAddToGoodWords = NO;
break;
}
}
if (shouldAddToGoodWords)
{
NSLog(@"Adding word: %@", word);
[goodWords addObject:word];
}
}
shouldAddToGoodWords = YES;
}
How can I optimise out this nested for loop?
The program should go through each word in the array created from the word text file, and if it's greater than 8 characters, add it to the goodWords
array. But the caveat is that I only want the root word to be in the goodWords array, for example:
If greet is added to the array, I don't want greets or greetings or greeters, etc.
NSString *string = [NSString stringWithContentsOfFile:@"/Users/james/dev/WordParser/word.txt" encoding:NSUTF8StringEncoding error:NULL];
NSArray *words = [string componentsSeparatedByString:@"\r\n"];
NSMutableArray *goodWords = [NSMutableArray array];
BOOL shouldAddToGoodWords = YES;
for (NSString *word in words)
{
NSLog(@"Word: %@", word);
if ([word length] > 8)
{
NSLog(@"Word is greater than 8");
for (NSString *existingWord in [goodWords reverseObjectEnumerator])
{
NSLog(@"Existing Word: %@", existingWord);
if ([word rangeOfString:existingWord].location != NSNotFound)
{
NSLog(@"Not adding...");
shouldAddToGoodWords = NO;
break;
}
}
if (shouldAddToGoodWords)
{
NSLog(@"Adding word: %@", word);
[goodWords addObject:word];
}
}
shouldAddToGoodWords = YES;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的事情怎么样?
它在我的机器(2.8GHz MBP)上运行得非常非常快。
How about something like this?
This runs very very quickly on my machine (2.8GHz MBP).
Trie 似乎适合您的目的。它类似于哈希,可用于检测给定字符串是否是已见过字符串的前缀。
A Trie seems suitable for your purpose. It is like a hash, and is useful for detecting if a given string is a prefix of an already seen string.
我使用
NSSet
来确保一次仅添加一个单词的 1 个副本。如果NSSet
尚未包含某个单词,它将添加该单词。然后,它检查新单词是否是已添加的任何单词的子字符串,如果为真,则不会添加新单词。它也不区分大小写。我写的是对你的代码的重构。它可能没有那么快,但如果您想在搜索已添加到树中的单词时使其速度更快,那么您确实需要树数据结构。
看看红黑树或B 树。
Words.txt
源代码
输出:
I used an
NSSet
to ensure that you only have 1 copy of a word added at a time. It will add a word if theNSSet
does not already contain it. It then checks to see if the new word is a substring for any word that has already been added, if true then it won't add the new word. It's case-insensitive as well.What I've written is a refactoring of your code. It's probably not that much faster but you really do want a tree data structure if you want to make it a lot faster when you want to search for words that have already been added to your tree.
Take a look at RedBlack Trees or B-Trees.
Words.txt
Source Code
Output: