Skip to content

Instantly share code, notes, and snippets.

@kerryChen95
Created March 21, 2013 09:13
Show Gist options
  • Save kerryChen95/5211727 to your computer and use it in GitHub Desktop.
Save kerryChen95/5211727 to your computer and use it in GitHub Desktop.
["dwdwad","dwdawd","dwdwad","ddd","sss"],找“ddd”这个字符串的索引,怎么样牺牲空间复杂度,换取时间复杂度为O(1)
var i, arr = ["dwdwad","dwdawd","dwdwad","ddd","sss"], target = 'ddd';
var hash = {};
for(i = 0; i < arr.length; ++i) {
hash[arr[i]] = i;
}
console.log(hash[target]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment