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

Commit6afe52e

Browse files
committed
restart medium 144, 319, 94
1 parent3e94cf7 commit6afe52e

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

‎Medium/136-singleNumber.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ var singleNumber = function(nums) {
1616
returnparseInt(Object.keys(appears)[0]);
1717
};
1818

19-
// a better solution??
19+
// a better solution: use XOR. XOR same number is 0
20+
varsingleNumber=function(nums){
21+
varresult=0;
22+
for(vari=0;i<nums.length;i++){
23+
result=nums[i]^result;
24+
}
25+
returnresult;
26+
};

‎Medium/144-binaryTreePreorder.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* key: rightNodes is the stack to track right nodes.
10+
*@param {TreeNode} root
11+
*@return {number[]}
12+
*/
13+
varpreorderTraversal=function(root){
14+
varrightNodes=[];
15+
varorder=[];
16+
while(root||rightNodes.length>0){
17+
if(root){
18+
order.push(root.val);
19+
if(root.right)rightNodes.push(root.right);
20+
root=root.left;
21+
}elseroot=rightNodes.pop();
22+
}
23+
returnorder;
24+
};

‎Medium/319-bulbSwitcher.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Key, focus on the bulb, the ith bulb's stutus will not be changed after ith round
3+
* e.g. 2nd bulb's status only can be changed twice, 1*2, and 2*1, that is, 2's factors
4+
* If the number of factors of i is even, it will be no effect to the bulb, the bulb stays
5+
* off. (e.g. 6 has 1, 2, 3, 6, only (1,6) (2,3) (3,2) (6,1) can change the status,
6+
* but 4 times has no effect to the status). However, if the number of factors is odd, the
7+
* bulb status will be changed. (e.g. 4 has 1, 2, 4, (1,4), (2,2), (4,1)), so we can see only
8+
* the number of total 'on' bulb is the number of sqaure root of n.
9+
*
10+
* if the number of factors is odd, the
11+
*@param {number} n
12+
*@return {number}
13+
*/
14+
varbulbSwitch=function(n){
15+
returnMath.floor(Math.sqrt(n));
16+
};

‎Medium/94-binaryTreeInorder.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
*@param {TreeNode} root
10+
*@return {number[]}
11+
*/
12+
varinorderTraversal=function(root){
13+
varstack=[];
14+
varorder=[];
15+
while(stack.length>0||root){
16+
if(root){
17+
stack.push(root);
18+
root=root.left;
19+
}else{
20+
varnode=stack.pop();
21+
order.push(node.val);
22+
if(node.right)root=node.right;
23+
}
24+
}
25+
26+
returnorder;
27+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp