Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Refactor JS - Array & Hash#426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
aakhtar3 wants to merge1 commit intoneetcode-gh:mainfromaakhtar3:main
Closed
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletionsjavascript/1-Two-Sum.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
/**
* @param {number[]} nums
* @param {number} target
* Time O(N) || Space O(N)
* @return {number[]}
*/
var twoSum = function(nums, target) {
let map = {};
for (let i = 0; i < nums.length; i++) {
if (target - nums[i] in map) {
return [map[target-nums[i]], i];
} else {
map[nums[i]] = i;
var twoSum = (nums, target, map = new Map()) => {
for (let index = 0; index < nums.length; index++) {
const num = nums[index];
const complement = target - num;

if (map.has(complement)) {
const sumIndex = map.get(complement);

return [ index, sumIndex ]
}

map.set(num, index);
}
};

return [ -1, -1 ]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't thinkreturn [-1, -1] is needed since"You may assume that each input would haveexactly one solution" for this problem.

}
93 changes: 13 additions & 80 deletionsjavascript/128-Longest-consecutive-sequence.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,23 @@
//////////////////////////////////////////////////////////////////////////////
// Linear Search With A Hash Map
// Time: O(n)
// Space: O(n)
// This solution only makes one pass over the `nums` array and is the highest
// performing solution.
//////////////////////////////////////////////////////////////////////////////

/**
* @param {number[]} nums
* Time O (N) | Space O(N)
* @return {number}
*/
function longestConsecutive(nums) {

if (!nums.length) {
return 0;
}

const map = Object.create(null);
let max = 0;

for (const num of nums) {

if (num in map) {
continue;
}

const prev = num - 1;
const next = num + 1;
let len = 1;

if (prev in map) {
if (next in map) {
len += map[prev] + map[next];
map[prev - map[prev] + 1] = len;
map[next + map[next] - 1] = len;
} else {
len += map[prev];
++map[prev - map[prev] + 1];
}
} else if (next in map) {
len += map[next];
++map[next + map[next] - 1];
}
map[num] = len;
max = Math.max(max, len);
}

return max;
}

//////////////////////////////////////////////////////////////////////////////
// Linear Search With A Hash Set
// Time: O(n)
// Space: O(n)
// This solution does three passes over the `nums` array. A first pass to
// setup the hash set. A second pass to find the numbers that mark the
// beginning of a sequence. A third pass to calculate the length of each
// sequence. The nested `while` loop does not cause quadratic calculations as
// it is only initiated on the first number of each sequence.
//////////////////////////////////////////////////////////////////////////////
var longestConsecutive = function(nums, longestStreak = 0) {
const numSet = new Set(nums);

for (const num of [ ...numSet ]) {
if (numSet.has(num - 1)) continue;

/**
* @param {number[]} nums
* @return {number}
*/
function longestConsecutive(nums) {
let [ currentNum, currentStreak ] = [ num, 1 ];

const set = new Set(nums);
let max = 0;

for (let i = 0; i < nums.length; i++) {
const num = nums[i];

if (set.has(num - 1)) {
continue;
while (numSet.has(currentNum + 1)) {
currentNum += 1;
currentStreak += 1;
}

let currentMax = 1;
while (set.has(num + currentMax)) {
currentMax++;
}

if (currentMax > max) {
max = currentMax;
}
longestStreak = Math.max(longestStreak, currentStreak);
}

returnmax;
}
returnlongestStreak;
}
10 changes: 2 additions & 8 deletionsjavascript/217-Contains-Duplicate.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
/**
* @param {number[]} nums
* Time O(N) | Space O(N)
* @return {boolean}
*/
var containsDuplicate = function(nums) {
const numsSet = new Set()
for(const i of nums){
if(numsSet.has(i)){
return true
}
numsSet.add(i)
}
return false
return (new Set(nums)).size !== nums.length
Comment on lines 1 to +7
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This solution has already been proposed (#371)

};
33 changes: 19 additions & 14 deletionsjavascript/238-Product-of-Array-Except-Self.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
/**
* @param {number[]} nums
* Time O(N) | Space O(N)
* @return {number[]}
*/
var productExceptSelf = function(nums) {
constres =[];
constproducts =new Array(nums.length).fill(1);

let product = 1;

for (let i = 0; i < nums.length; i++) {
res[i] = product;
product *= nums[i];
carryForward(nums, products);
carryBackward(nums, products);

return products;
};

const carryForward = (nums, products, product = 1) => {
for (let index = 0; index < nums.length; index++) {
products[index] = product;
product *= nums[index];
}
product = 1;
for (let j = nums.length - 1; j >= 0; j--) {
res[j] *= product;
product *= nums[j];
}

const carryBackward = (nums, products, product = 1) => {
for (let index = (nums.length - 1); 0 <= index; index--) {
products[index] *= product;
product *= nums[index];
}

return res;

};
}
58 changes: 38 additions & 20 deletionsjavascript/242-Valid-Anagram.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
/**
* @param {string} s
* @param {string} t
* Time O(N * logN) | Space O(N)
* @return {boolean}
*/
var isAnagram = function(s, t) {
let map = {};
if (s.length !== t.length) return false;

const reOrder = (str) => str
.split('')
.sort((a, b) => a.localeCompare(b))
.join('');

return reOrder(s) === reOrder(t)
};

/**
* @param {string} s
* @param {string} t
* Time O(N) | Space O(N)
* @return {boolean}
*/
var isAnagram = function(s, t, map = new Map()) {
if (s.length !== t.length) return false;

if (s.length !== t.length) {
return false;
}
addCharFrequency(s, map);
return subtractCharFrequency(t, map)

for (let i = 0; i < s.length; i++) {
if (map[s[i]]) {
map[s[i]]++;
} else {
map[s[i]] = 1;
}
};
Comment on lines 7 to +30
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Adding multiple solutions is being discussed here (#189)


const addCharFrequency = (str, map) => {
for (const char of str) {
map.set(char, (map.get(char) || 0) + 1);
}
for (let i = 0; i < t.length; i++) {
if (map[t[i]]) {
map[t[i]]--;
} else {
}

const subtractCharFrequency = (str,map) => {
for (const char of str) {
if (!map.has(char)) {
return false;
}

map.set(char, (map.get(char) - 1));
}

return true;
};
}




32 changes: 20 additions & 12 deletionsjavascript/271-Encode-and-Decode-Strings.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
/**
* @param {string[]} strs
* Time O(N) | Space O (N)
* @return {string}
*/
function encode(strs) {
Expand All@@ -8,23 +9,30 @@ function encode(strs) {

/**
* @param {string} str
* Time O(N) | Space O (N)
* @return {string[]}
*/
function decode(str) {
const res = [];
let i = 0;
function decode(str, index = 0, decodedWords = []) {
while (index < str.length) {
const { nextIndex, word } = delimitWord(str, index);

while (i < str.length) {
let j = i;
while (str[j] !== "#") {
++j;
}
decodedWords.push(word);

const len = Number(str.slice(i, j));
res.push(str.slice(++j, j + len));
i = j + len;
index = nextIndex;
}

return res;
return decodedWords;
}

const delimitWord = (str, index) => {
const delimiter = str.indexOf('#', index);
const length = Number(str.slice(index, delimiter));
const [ start, end ] = [ (delimiter + 1), (delimiter + length) + 1 ];
const word = str.slice(start, end);

return {
nextIndex: end,
word
};
}

34 changes: 0 additions & 34 deletionsjavascript/347-Top-K-Frquent-Elements.js
View file
Open in desktop

This file was deleted.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp