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

Commit8f9843c

Browse files
committed
resume LeetCode, add 49, 169
1 parent3c3af81 commit8f9843c

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

‎Easy/169-majorityElement.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ var majorityElement = function(nums) {
2222

2323
// better solution
2424
varmajorityElement=function(nums){
25-
nums.sort();
25+
nums.sort(function(a,b){
26+
returna-b;
27+
});
2628
varmid=Math.floor(nums.length/2);
2729
returnnums[mid];
2830
};

‎Medium/229-majorityElementII.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
*@see http://www.cs.utexas.edu/~moore/best-ideas/mjrty/index.html
3+
*@param {number[]} nums
4+
*@return {number[]}
5+
*/
6+
varmajorityElement=function(nums){
7+
varn1=null;
8+
varn2=null;
9+
varc1=0;
10+
varc2=0;
11+
12+
for(vari=0;i<nums.length;i++){
13+
if(n1!==null&&n1===nums[i]){
14+
c1++;
15+
}elseif(n2!==null&&n2===nums[i]){
16+
c2++;
17+
}elseif(c1===0){
18+
n1=nums[i];
19+
c1++;
20+
}elseif(c2===0){
21+
n2=nums[i];
22+
c2++;
23+
}else{
24+
c1--;
25+
c2--;
26+
}
27+
}
28+
29+
c1=0;
30+
c2=0;
31+
for(vari=0;i<nums.length;i++){
32+
if(nums[i]===n1)c1++;
33+
if(nums[i]===n2)c2++;
34+
}
35+
36+
varresult=[];
37+
if(c1>Math.floor(nums.length/3))result.push(n1);
38+
if(c2>Math.floor(nums.length/3))result.push(n2);
39+
returnresult;
40+
};

‎Medium/49-groupAnagrams.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* key: sort each string in strs. If two strings are anagram to each other, then
3+
* after sorting, theses two strings are same. Use a map to track the same strings,
4+
* the string itself becomes the key, the value is the array of the anagram strings.
5+
*
6+
*@param {string[]} strs
7+
*@return {string[][]}
8+
*/
9+
vargroupAnagrams=function(strs){
10+
varmap={};
11+
for(vari=0;i<strs.length;i++){
12+
varstrCopy=strs[i].split('');
13+
strCopy.sort(strCompare);
14+
if(strCopy.join('')inmap){
15+
map[strCopy.join('')].push(strs[i]);
16+
}else{
17+
map[strCopy.join('')]=[strs[i]];
18+
}
19+
}
20+
21+
varresult=[];
22+
for(varkeyinmap){
23+
result.push(map[key].sort(strCompare));
24+
}
25+
26+
returnresult;
27+
};
28+
29+
varstrCompare=function(a,b){
30+
if(a<b)return-1;
31+
elseif(a>b)return1;
32+
elsereturn0;
33+
};

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
*[46. Permutations](https://leetcode.com/problems/permutations/) -[Solution](./Medium/46-permutations.js)
8888
*[47. Permutations II](https://leetcode.com/problems/permutations-ii/) -[Solution](./Medium/47-permutationsII.js)
8989
*[48. Rotate Image](https://leetcode.com/problems/rotate-image/) -[Solution](./Medium/48-rotateImage.js)
90+
*[49. Group Anagrams](https://leetcode.com/problems/anagrams/) -[Solution](./Medium/49-groupAnagrams.js)
9091
*[50. Pow(x, n)](https://leetcode.com/problems/powx-n/) -[Solution](./Medium/50-powerxn.js)
9192
*[53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) -[Solution](./Medium/53-maximumSubarray.js)
9293
*[54. Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) -[Solution](./Medium/54-spiralMatrix.js)
@@ -115,6 +116,7 @@
115116
*[199. Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/) -[Solution](./Medium/199-binaryTreeRightSideView.js)
116117
*[213. House Robber II](https://leetcode.com/problems/house-robber-ii/) -[Solution](./Medium/213-houseRobberII.js)
117118
*[215. Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) -[Solution](./Medium/215-KthLargestElementInArray.js)
119+
*[229. Majority Element II](https://leetcode.com/problems/majority-element-ii/) -[Solution](./Medium/229-majorityElementII.js)
118120
*[230. Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) -[Solution](./Medium/230-kthSmallestElementinBST.js)
119121
*[238. Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) -[Solution](./Medium/238-productExceptSelf.js)
120122
*[240. Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) -[Solution](./Medium/240-Search2DMatrixII.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp