Given an unsorted integer array, find the smallest missing positive integer.
Solution
/** * @param {number[]} nums * @return {number} */ var firstMissingPositive = function(nums) { sort(nums); for(let i = 0; i < nums.length; i++) { if(nums[i] !== i + 1) return i + 1; } return nums.length + 1; }; functionsort(nums) { let n = nums.length; for(let i = 0; i < n; i++) { let temp = nums[i]; while(temp !== i + 1) { if (temp <= 0 || temp > n || temp === nums[temp - 1]) break; nums[i] = nums[temp - 1]; nums[temp - 1] = temp; temp = nums[i]; } } }
75. Sort Colors
Description
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Solution
/** * @param {number[]} nums * @return {void} Do not return anything, modify nums in-place instead. */ var sortColors = function(nums) { let i = 0, j = nums.length - 1; for(let k = 0; k < j + 1;) { if(nums[k] === 0) { [nums[i], nums[k]] = [nums[k], nums[i]]; i++; k++; }elseif(nums[k] === 2) { [nums[j], nums[k]] = [nums[k], nums[j]]; j--; }else k++; } };
贪心
763. Partition Labels
Description
A string S of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts.
Solution
/** * @param {string} S * @return {number[]} */ var partitionLabels = function(S) { let last = {}; for(let i = 0; i < S.length; i++) { last[S[i]] = i; } let j = 0, prev = 0, result = []; for(let i = 0; i < S.length; i++) { j = Math.max(j, last[S[i]]); if(i === j) { result.push(i - prev + 1); prev = i + 1; } } return result; };
984. String Without AAA or BBB
Description
Given two integers A and B, return any string S such that:
S has length A + B and contains exactly A'a' letters, and exactly B'b' letters;
The substring 'aaa' does not occur in S;
The substring 'bbb' does not occur in S.
Solution
/** * @param {number} A * @param {number} B * @return {string} */ var strWithout3a3b = function(A, B) { let S = []; while(A > 0 || B > 0) { let writeA = false; let l = S.length; if(l >= 2 && S[l - 1] === S[l - 2]) { if(S[l - 1] === 'b') writeA = true; }else { if(A >= B) writeA = true; } if(writeA) { A--; S.push('a'); }else { B--; S.push('b'); } } return S.join(''); }
暴力搜索
46. Permutations
Description
Given a collection of distinct integers, return all possible permutations.
Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
Only one letter can be changed at a time.
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
Note:
Return 0 if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
You may assume no duplicates in the word list.
You may assume beginWord and endWord are non-empty and are not the same.