[11] Container With Most Water
Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
my solution : Brute Force
穷举所有面积的可能性,最后对面积进行排序,找到最大值.
中间一度尝试过将 partArea 组成数组先排序,也还是超时.
1 | var maxArea = function (height) { |
Two pointers
采用双指针做法, 对于 S(i, j) 来说, 都是每次向里移动一步.
移动短板, 短板有可能变长, 面积有可能变大.
但是移动长板, 短板只会不变或者变小, 因为盛水的体积取决于短板, 所以面积只会不变或变小.
1 | // 此算法需要证明 |
Complexity Analysis
- Time complexity : O(n)O(n). Single pass.
- Space complexity : O(1)O(1). Constant space is used.
[26] Remove Duplicates from Sorted Array
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
my solution
借用 js 数组splice方法
1 | var removeDuplicates = function (nums) { |
Two pointers
1 | // 参考双指针的方法, 优化了解法 |
[27] Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
my solution
我自己的解法是利用了js数组操作的特性, 可以直接删除数组元素然后剩余元素位置前移的那种,比较方便,但是运行效果不咋地.
1 | // solution 1 |
Two pointers
下面的解法是参考了示例解法, 双指针解法. 我觉得很精巧.
主要思路是, 将需要保留的元素都赋值给数组的前部分, 使用 i 标记赋值的位置.
1 | // solution two pointers |
最差的情况应该是, 没有一个一样的, 但是遍历数组两遍而不是嵌套, 所以是 O(n).
Complexity analysis
- Time complexity : O(n). Assume the array has a total of nn elements, both i and j traverse at most 2n steps.
- Space complexity : O(1).
[15] 3Sum
Given an array
numsof n integers, are there elements a, b, c innumssuch that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:
The solution set must not contain duplicate triplets.
数组排序后, 方便去除重复的元素 + 双指针移动不用嵌套且有方向可循.
1 | // solution 2 :将数组排序后的双指针解法 |
[16] 3Sum Closest
Given an array
numsof n integers and an integertarget, find three integers innumssuch that the sum is closest totarget. Return the sum of the three integers. You may assume that each input would have exactly one solution.
注意这里不是找相等, 而是保留最接近target的值, 实现方法类似.
与target比较, 由于一直在找最接近的, 比target小就left++, 比target大就right—, 总之就是不断靠近target.
1 | var threeSumClosest = function (nums, target) { |
[18] 4Sum
Given an array
numsof n integers and an integertarget, are there elements a, b, c, and dinnumssuch that a + b + c + d =target? Find all unique quadruplets in the array which gives the sum oftarget.Note:
The solution set must not contain duplicate quadruplets.
有了双指针, nSum都可解, 不过这个嵌套有点多,估计有更巧妙的解法.
1 | var fourSum = function (nums, target) { |
[283] Move Zeroes
Given an array
nums, write a function to move all0‘s to the end of it while maintaining the relative order of the non-zero elements.Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
双指针解法, 一次成功.
1 | // solution: two pointers |
[66] Plus One
Given a non-empty array of digits representing a non-negative integer, increment one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
主要是运用数组特性.不是很难, 理解题意即可.
还挺多人不喜欢这道题的, 可能觉得太弱智了?…
1 | var plusOne = function (digits) { |
[88] Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
- The number of elements initialized in nums1 and nums2 are mand n respectively.
- You may assume that nums1 has enough space (size that is equal to m + n) to hold additional elements from nums2.
我用的方法比较死板, 感觉没有什么难度. 就是分情况讨论.
不过用到了 js 里 Array 的特性.
1 | var merge = function (nums1, m, nums2, n) { |
[80] Remove Duplicates from Sorted Array II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
时间复杂度: 遍历整个数组 $O(N)$
空间复杂度: 仅使用两个index: left和right标记数组元素位置, $O(1)$
使用splice方法直接修改数组. (这个方法的实现原理需要了解一下 TODO)
1 | // 数组是排好序的 |
[202] Happy Number
Write an algorithm to determine if a number
nis “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Return True if
nis a happy number, and False if not.
复杂度分析有点困难, 以及为什么只有循环或者归1两种结果, 而不是无穷大.
1 | // solution 1: 快慢指针法 |