This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import heapq | |
class Solution: | |
def findKthLargest(self, nums: List[int], k: int) -> int: | |
heap = [] | |
for n in nums: | |
heapq.heappush(heap, -n) | |
if len(heap) > k: | |
# list popは引数がない場合、最後の要素を削除する。その計算量はO(1) | |
# https://wiki.python.org/moin/TimeComplexity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TreeDrawer { | |
public static String treeNodeToString(TreeNode root) { | |
if (root == null) { | |
return "[]"; | |
} | |
String output = ""; | |
Queue<TreeNode> nodeQueue = new LinkedList<>(); | |
nodeQueue.add(root); | |
while (!nodeQueue.isEmpty()) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSON.stringify(undefined); // undefined | |
JSON.stringify(function() {}); // undefined | |
JSON.stringify([1, undefined, function() {}, 4]); // "[1,null,null,4]" | |
JSON.stringify({a: 2, b: function() {}}); // "{"a":2}" | |
// The second argument is called a replacer. It filters out which will be in stringified JSON | |
var a = { | |
a: 1, | |
b: 2, | |
c: 3, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = new String('abc'); | |
a.__proto__ === String.prototype; // true | |
- a.__proto__.constructor === String; // true | |
// JS internal [[Class]] can be revealed by calling Object.prototype.toString | |
Object.prototype.toString.call([1,2,3]); // "[object Array]" | |
Object.prototype.toString.call(/regex-literal/i); // "[object RegExp]" | |
Object.prototype.toString.call(null); // "[object Null]" | |
Object.prototype.toString.call(undefined); // "[object Undefined]" | |
// There are no Null() nor Undefined(), but still there are internal constructor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo(x) { | |
x.push(4); | |
console.log(x); // [1,2,3,4]; | |
x = [4,5,6]; | |
x.push(7); | |
console.log(x); // [4,5,6,7] | |
} | |
var a = [1,2,3]; | |
foo(a); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void 2; // undefined | |
void 'abc'; // undefined | |
void function() {}; // undefined | |
void undefined; // undefined | |
void null; // undefined | |
void {}; // undefined | |
void true; // undefined | |
void false; // undefined | |
// NaN means "Not a Number", however the type of NaN is a "number" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo() { | |
var undefined = 2; | |
console.log(undefined); // 2 | |
} | |
foo(); | |
// In strict mode, this is also valid | |
function foo() { | |
'use strict'; | |
var undefined = 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42.toFixed(3); // SyntaxError | |
// This is invalid since it first recognizes . as fraction point. | |
(42).toFixed(3); // "42.000" | |
0.42.toFixed(3); // "0.420" | |
42..toFixed(3); // "42.000" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverse(str) { | |
var strReversed = ''; | |
for (let i = str.length - 1; i >= 0; i--) { | |
strReversed += str.charAt(i); | |
} | |
return strReversed; | |
} | |
reverse('foo 𝌆 bar'); // rab �� oof | |
reverse('mañana mañana'); // anãnam anañam | |
// Use the package like Esrever to handle such things. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = []; | |
a[2] = 1; | |
a.length; // 3 | |
a; // [undefined, undefined, 1] | |
// You can set the key value pair to the array, but it doesn't count in length | |
a.a = 12; | |
a; // [undefined, undefined, a, a: 12]; | |
a.length; // 3 | |
a.a; // 12 |
NewerOlder