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

Commit8508bc4

Browse files
committed
130, 143, 274
1 parentf7b6a17 commit8508bc4

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

‎Medium/130-surroundedRegions.js‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* solution: search boarders first, if there is an "O", search this O's neighbors,
3+
* if there is a connected "O", keeping searching.
4+
* The "O" can be captured only if it is connected to the "O" exsiting on the boarders
5+
* In other words, it can get out the board.
6+
*
7+
*@param {character[][]} board
8+
*@return {void} Do not return anything, modify board in-place instead.
9+
*/
10+
varsolve=function(board){
11+
varxIndex=[];
12+
varyIndex=[];
13+
varrows=board.length;
14+
if(rows===0)return;
15+
varcols=board[0].length;
16+
17+
for(vari=0;i<rows;i++){
18+
if(board[i][0]==='O'){
19+
xIndex.push(i);
20+
yIndex.push(0);
21+
}
22+
23+
if(board[i][cols-1]==='O'){
24+
xIndex.push(i);
25+
yIndex.push(cols-1);
26+
}
27+
}
28+
29+
for(i=0;i<cols;i++){
30+
if(board[0][i]==='O'){
31+
xIndex.push(0);
32+
yIndex.push(i);
33+
}
34+
35+
if(board[rows-1][i]==='O'){
36+
xIndex.push(rows-1);
37+
yIndex.push(i);
38+
}
39+
}
40+
vark=0;
41+
while(k<xIndex.length){
42+
varx=xIndex[k];
43+
vary=yIndex[k];
44+
board[x][y]='Y';
45+
if(x>1&&board[x-1][y]==='O'){
46+
xIndex.push(x-1);
47+
yIndex.push(y);
48+
}
49+
if(x<rows-2&&board[x+1][y]==='O'){
50+
xIndex.push(x+1);
51+
yIndex.push(y);
52+
}
53+
if(y>1&&board[x][y-1]==='O'){
54+
xIndex.push(x);
55+
yIndex.push(y-1);
56+
}
57+
if(y<cols-2&&board[x][y+1]==='O'){
58+
xIndex.push(x);
59+
yIndex.push(y+1);
60+
}
61+
k++;
62+
}
63+
64+
for(varj=0;j<rows;j++){
65+
for(varh=0;h<cols;h++){
66+
if(board[j][h]==='O'){
67+
board[j][h]='X';
68+
}
69+
if(board[j][h]==='Y'){
70+
board[j][h]='O';
71+
}
72+
}
73+
74+
}
75+
};
76+
77+
// test cases
78+
// ["OOO","OOO","OOO"]
79+
// ["XXXX","XOOX","XXOX", "XOXX"]
80+
// ["XOX","XOX","XOX"]

‎Medium/143-reorderList.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* solution: reverse the second half of the list, then merge first half and reversed second half.
10+
*@param {ListNode} head
11+
*@return {void} Do not return anything, modify head in-place instead.
12+
*/
13+
varreorderList=function(head){
14+
if(!head||!head.next)return;
15+
varfast=head;
16+
varslow=head;
17+
while(fast.next&&fast.next.next){
18+
slow=slow.next;
19+
fast=fast.next.next;
20+
}
21+
22+
// reverse the second half of the list
23+
varmiddleHead=slow;
24+
varmiddleHeadPrev=null;
25+
while(middleHead){
26+
varheadNext=middleHead.next;
27+
middleHead.next=middleHeadPrev;
28+
middleHeadPrev=middleHead;
29+
middleHead=headNext;
30+
}
31+
// then merge first half and reversed second half.
32+
slow=head;
33+
while(slow){
34+
varslowNext=slow.next;
35+
slow.next=middleHeadPrev;
36+
varheadPrevNext=middleHeadPrev.next;
37+
middleHeadPrev.next=slowNext;
38+
slow=slowNext;
39+
middleHeadPrev=headPrevNext;
40+
}
41+
};

‎Medium/274-hIndex.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
*@param {number[]} citations
3+
*@return {number}
4+
*/
5+
// sort first.
6+
varhIndex=function(citations){
7+
citations.sort(function(a,b){
8+
returna-b;
9+
});
10+
11+
varresult=0;
12+
for(vari=0,length=citations.length;i<length;i++){
13+
varmin=Math.min(citations[i],length-i);
14+
result=Math.max(result,min);
15+
}
16+
17+
returnresult;
18+
};

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,14 @@
132132
*[122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) -[Solution](./Medium/122-bestTimeToBuySellStockII.js)
133133
*[129. Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) -[Solution](./Medium/129-sumRootToLeafNumbers.js)
134134
*[127. Word Ladder](https://leetcode.com/problems/word-ladder/) -[Solution](./Medium/127-wordLadder.js)
135+
*[130. Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) -[Solution](./Medium/130-surroundedRegions.js)
135136
*[131. Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/) -[Solution](./Medium/131-palindromePartitioning.js)
136137
*[134. Gas Station](https://leetcode.com/problems/gas-station/) -[Solution](./Medium/134-gasStation.js)
137138
*[136. Single Number](https://leetcode.com/problems/single-number/) -[Solution](./Medium/136-singleNumber.js)
138139
*[139. Word Break](https://leetcode.com/problems/word-break/) -[Solution](./Medium/139-wordBreak.js)
139140
*[141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) -[Solution](./Medium/141-linkedListCycle.js)
140141
*[142. Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) -[Solution](./Medium/142-linkedListCycleII.js)
142+
*[143. Reorder List](https://leetcode.com/problems/reorder-list/) -[Solution](./Medium/143-reorderList.js)
141143
*[144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) -[Solution](./Medium/144-binaryTreePreorder.js)
142144
*[151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) -[Solution](./Medium/151-reverseWordsInAString.js)
143145
*[152. Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) -[Solution](./Medium/152-MaxProductSubarray.js)
@@ -156,6 +158,7 @@
156158
*[241. Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/) -[Solution](./Medium/241-differentWaysAddParentheses.js)
157159
*[260. Single Number III](https://leetcode.com/problems/single-number-iii/) -[Solution](./Medium/260-singleNumberIII.js)
158160
*[268. Missing Number](https://leetcode.com/problems/missing-number/) -[Solution](./Medium/268-missingNumber.js)
161+
*[274. H-Index](https://leetcode.com/problems/h-index/) -[Solution](./Medium/274-hIndex.js)
159162
*[300. Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) -[Solution](./Medium/300-longestIncreasingSubsequence.js)
160163
*[309. Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) -[Solution](./Medium/309-bestTimeStockCooldown.js)
161164
*[318. Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) -[Solution](./Medium/318-maximumProductWordLengths.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp