1: class Solution {
2: public:
3: int shortestDistance(vector<string>& words, string word1, string word2) {
4: int i1 = -1, i2 = -1, dist = INT_MAX;
5: for (int i = 0; i < words.size(); i++) {
6: if (words[i] == word1) {
7: i1 = i;
8: if (i2 != -1) dist = min(dist, abs(i2-i1));
9: }
10: if (words[i] == word2) {
11: i2 = i;
12: if (i1 != -1) dist = min(dist, abs(i2-i1));
13: }
14: }
15: return dist;
16: }
17: };
Friday, August 12, 2016
243. Shortest Word Distance
Well, not much to say, a pretty straightforward solution.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment