We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 676f1d0 commit 44e2a46Copy full SHA for 44e2a46
README.md
@@ -1179,15 +1179,24 @@ console.log(twoSum([3, 2, 4], 6)); // [1, 2]
1179
1180
```js
1181
const twoSum = (nums, target) => {
1182
- const map = new Map();
1183
-
1184
- for (let i = 0; i < nums.length; i++) {
1185
- const otherIndex = map.get(target - nums[i]);
1186
- if (typeof otherIndex !== 'undefined') {
1187
- return [otherIndex, i];
+ const len = nums.length;
+ for (let i = 0; i < len; i++) {
+ const j = nums.lastIndexOf(target - nums[i]);
+ if (j > i) {
+ return [i, j];
1188
}
1189
- map.set(nums[i], i);
1190
+
+ // Alternative solution using Map
1191
+ // const map = new Map();
1192
1193
+ // for (let i = 0; i < nums.length; i++) {
1194
+ // const otherIndex = map.get(target - nums[i]);
1195
+ // if (typeof otherIndex !== 'undefined') {
1196
+ // return [otherIndex, i];
1197
+ // }
1198
+ // map.set(nums[i], i);
1199
1200
};
1201
```
1202
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments