# leetcode 第599题. 两个列表的最小索引总和,真心求优化

0

public string[] FindRestaurant(string[] list1, string[] list2) {
Dictionary<string, int> dic = new Dictionary<string, int>(list1.Length);
for (int i = 0; i < list1.Length; i++)
{
}
List<string> result = new List<string>();
int min = -1;
for (int i = 0; i < list2.Length; i++)
{
if (dic.Keys.Contains(list2[i]))
{
int t = i + dic[list2[i]];
if (min == -1 || min == t)
{
min = t;
}
else if (t < min)
{
min = t;
result.Clear();
}
}
}
return result.ToArray();
}

0

``````public class Solution {

public string[] FindRestaurant(string[] list1, string[] list2) {

Dictionary<string, int> dic = new Dictionary<string, int>(list1.Length);
for (int i = 0; i < list1.Length; i++)
{
}
int[] result = new int[list2.Length];

int[] num = new int[list2.Length];
int min = 9999999;
int pos=0;
int pos2=0;
int tag=0;
for (int i = 0; i < list2.Length; i++)
{
if(i>min) break;
if (dic.ContainsKey(list2[i]))
{
int t = i + dic[list2[i]];

if(min >= t)
{
result[pos]=i;
num[pos++]=t;

if(min==t)
tag++;
else{
tag=1;
min = t;
}

}
}
}
string[] ans = new string[tag];
for(int i=0;i<pos;i++)
{
if(num[i]==min)
{
ans[pos2++]=list2[result[i]];
}
}

return ans;
}

}``````

Shendu.CC | 小虾三级 |园豆：1918 | 2019-06-17 16:43

您需要登录以后才能回答，未注册用户请先注册