欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > LeetCode热题100(JavaScript)

LeetCode热题100(JavaScript)

2024/10/24 6:25:31 来源:https://blog.csdn.net/qq_60587956/article/details/140448719  浏览:    关键词:LeetCode热题100(JavaScript)

哈希

两数之和

暴力解法

/*** @param {number[]} nums* @param {number} target* @return {number[]}*/
var twoSum = function(nums, target) {for(let i =0;i<nums.length;i++){let x1 = nums[i]for(let j = 0 ; j<nums.length;j++){if(i!=j){let x2 = nums[j]if(x1+x2==target){return [i,j]}}}}
};

Map法

var twoSum = function(nums, target) {const map = new Map()for(let i=0;i<nums.length;i++){let key = target-nums[i]if(map.has(key)){return [map.get(key),i]}else{map.set(nums[i],i)}  }return []
};

字母异味词分组

/*** @param {string[]} strs* @return {string[][]}*/
var groupAnagrams = function(strs) {let map = new Map();  for (let i = 0; i < strs.length; i++) {  let word = strs[i];  // 使用数组的 sort 方法对字符串的字符进行排序,并将排序后的字符数组重新连接成字符串  let sorted_word = word.split('').sort().join('');  if (map.has(sorted_word)) {  map.get(sorted_word).push(word);  } else {  map.set(sorted_word, [word]);  }  }  // 将Map中的值(数组)转换为数组返回  return [...map.values()];  
};

最长连续序列

var longestConsecutive = function(nums) {if (nums.length === 0) return 0;  let set = new Set(nums);  let maxSize = 0;  for (let num of set) {  // 跳过非连续序列的起始数字(即那些前面有数字的数字)  if (!set.has(num - 1)) {  let currentNum = num;  let currentLength = 1;  // 遍历连续序列  while (set.has(currentNum + 1)) {  currentNum++;  currentLength++;  }  // 更新最大长度  maxSize = Math.max(maxSize, currentLength);  }  }  return maxSize; 
};

双指针

移动零

解法1

先遍历数组,计算非零元素的数量。然后,在第二次遍历中,只将非零元素复制回数组的前部,并跳过0。

function moveZeroes(nums) {  let count = 0;   for (let num of nums) {  if (num !== 0) {  count++;  }  }  let writeIndex = 0;   for (let num of nums) {  if (num !== 0) {  nums[writeIndex++] = num;  }   }  // 填充剩余的0for (let i = writeIndex; i < nums.length; i++) {  nums[i] = 0;  } return nums
}  

解法2

  1. 初始化两个指针,slow 和 fast,都指向数组的第一个元素。
  2. 遍历数组(fast 从头到尾),对于每个 nums[fast]
    • 如果 nums[fast] 不为 0,则交换 nums[slow] 和 nums[fast],并将 slow 向后移动一位(slow++)。
    • 如果 nums[fast] 为 0,则只移动 fast 指针。
  3. 遍历结束后,所有非零元素都已经被移动到了数组的前面,而后面的元素则自动成为了 0
var moveZeroes = function(nums) {let slow=0let fast = 0for(fast;fast<nums.length;fast++){if(nums[fast]!=0){let swap = nums[slow]nums[slow] = nums[fast]nums[fast] = swapslow++}}return nums
};

盛最多水的容器

  1. 初始化左指针 left 为 0,右指针 right 为 height.length - 1
  2. 进入循环,当 left < right 时执行以下步骤:
    • 计算当前容器的面积 area = Math.min(height[left], height[right]) * (right - left)
    • 更新最大面积 maxArea(如果 area 大于 maxArea)。
    • 如果 height[left] < height[right],则 left++(移动左指针),否则 right--(移动右指针)。
  3. 退出循环后,maxArea 就是所求的最大面积。
var maxArea = function(height) {let left = 0let right = height.length-1let max = 0while(left<right){let h1 = height[left]let h2 = height[right]let area = Math.min(h1, h2) * (right - left)max = Math.max(max, area);  if (h1 > h2) {  right--;  } else {  left++;  } }return max
};

三数之和

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com