Skip to content

Commit d7d7118

Browse files
committed
👍
1 parent 3b6dd69 commit d7d7118

File tree

162 files changed

+6429
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+6429
-0
lines changed

add-two-numbers/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<h2>2. Add Two Numbers</h2><h3>Medium</h3><hr><div><p>You are given two <b>non-empty</b> linked lists representing two non-negative integers. The digits are stored in <b>reverse order</b>, and each of their nodes contains a single digit. Add the two numbers and return the sum&nbsp;as a linked list.</p>
2+
3+
<p>You may assume the two numbers do not contain any leading zero, except the number 0 itself.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong>Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg" style="width: 483px; height: 342px;">
8+
<pre><strong>Input:</strong> l1 = [2,4,3], l2 = [5,6,4]
9+
<strong>Output:</strong> [7,0,8]
10+
<strong>Explanation:</strong> 342 + 465 = 807.
11+
</pre>
12+
13+
<p><strong>Example 2:</strong></p>
14+
15+
<pre><strong>Input:</strong> l1 = [0], l2 = [0]
16+
<strong>Output:</strong> [0]
17+
</pre>
18+
19+
<p><strong>Example 3:</strong></p>
20+
21+
<pre><strong>Input:</strong> l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
22+
<strong>Output:</strong> [8,9,9,9,0,0,0,1]
23+
</pre>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Constraints:</strong></p>
27+
28+
<ul>
29+
<li>The number of nodes in each linked list is in the range <code>[1, 100]</code>.</li>
30+
<li><code>0 &lt;= Node.val &lt;= 9</code></li>
31+
<li>It is guaranteed that the list represents a number that does not have leading zeros.</li>
32+
</ul></div>

add-two-numbers/add-two-numbers.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
*     this.val = (val===undefined ? 0 : val)
5+
*     this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} l1
10+
* @param {ListNode} l2
11+
* @return {ListNode}
12+
*/
13+
var addTwoNumbers = function(l1, l2) {
14+
   
15+
   
16+
   let d = new ListNode(-1);
17+
   let res = d;
18+
   
19+
   let sum = 0;
20+
   let carry = 0;
21+
   
22+
   
23+
   while(l1 || l2 || carry){
24+
       sum = ((l1 && l1.val) || 0) + ((l2&&l2.val) || 0) + carry
25+
       
26+
       d.next = new ListNode(sum % 10)
27+
       
28+
       carry = sum > 9 ? 1 : 0;
29+
       
30+
       d = d.next;
31+
       l1 = l1 && l1.next;
32+
       l2 = l2 && l2.next
33+
34+
  }
35+
   
36+
   return res.next;
37+
};

alphabet-board-path/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[Discussion Post (created on 28/0/2021 at 21:34)](https://leetcode.com/problems/alphabet-board-path/discuss/1039802/Leetcode-in-JavaScript)
2+
<h2>1138. Alphabet Board Path</h2><h3>Medium</h3><hr><div><p>On an alphabet board, we start at position <code>(0, 0)</code>, corresponding to character&nbsp;<code>board[0][0]</code>.</p>
3+
4+
<p>Here, <code>board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]</code>, as shown in the diagram below.</p>
5+
6+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/07/28/azboard.png" style="width: 250px; height: 317px;"></p>
7+
8+
<p>We may make the following moves:</p>
9+
10+
<ul>
11+
<li><code>'U'</code> moves our position up one row, if the position exists on the board;</li>
12+
<li><code>'D'</code> moves our position down one row, if the position exists on the board;</li>
13+
<li><code>'L'</code> moves our position left one column, if the position exists on the board;</li>
14+
<li><code>'R'</code> moves our position right one column, if the position exists on the board;</li>
15+
<li><code>'!'</code>&nbsp;adds the character <code>board[r][c]</code> at our current position <code>(r, c)</code>&nbsp;to the&nbsp;answer.</li>
16+
</ul>
17+
18+
<p>(Here, the only positions that exist on the board are positions with letters on them.)</p>
19+
20+
<p>Return a sequence of moves that makes our answer equal to <code>target</code>&nbsp;in the minimum number of moves.&nbsp; You may return any path that does so.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Example 1:</strong></p>
24+
<pre><strong>Input:</strong> target = "leet"
25+
<strong>Output:</strong> "DDR!UURRR!!DDD!"
26+
</pre><p><strong>Example 2:</strong></p>
27+
<pre><strong>Input:</strong> target = "code"
28+
<strong>Output:</strong> "RR!DDRR!UUL!R!"
29+
</pre>
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= target.length &lt;= 100</code></li>
35+
<li><code>target</code> consists only of English lowercase letters.</li>
36+
</ul></div>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @param {string} target
3+
* @return {string}
4+
*/
5+
var alphabetBoardPath = function(target) {
6+
let board = ["abcde".split(""), "fghij".split(""), "klmno".split(""), "pqrst".split(""), "uvwxy".split(""), "z".split("")]
7+
8+
//directions
9+
let dirs = [
10+
[-1,0, "U"],
11+
[1,0, "D"],
12+
[0,-1,"L"],
13+
[0,1, "R"]
14+
]
15+
//x,y, res
16+
17+
if(target == "a")return "!"
18+
19+
let res = "";
20+
let x = 0,y = 0;
21+
22+
// let map = {};
23+
24+
for(let i =0; i < target.length; i++){
25+
const {ans, x1, y1} = bfs(target[i], x, y);
26+
console.log(ans,x,y)
27+
28+
if(target[i] != target[i-1]){
29+
const {ans, x1, y1} = bfs(target[i], x, y);
30+
x = x1;
31+
y = y1;
32+
res += ans
33+
}else{
34+
res += "!"
35+
}
36+
37+
38+
}
39+
40+
console.log(res)
41+
42+
return res;
43+
44+
function bfs(char, x, y){
45+
46+
47+
48+
49+
let queue = [[x,y, ""]];
50+
51+
52+
53+
let set = new Set();
54+
while(queue.length > 0){
55+
56+
let [x,y,res] = queue.shift();
57+
58+
if(x== 0 && y == 0 && char =="a"){
59+
return {
60+
"ans" : "!",
61+
"x1": 0,
62+
"y1" : 0
63+
}
64+
}
65+
66+
// if()
67+
for(let i=0; i < 4; i++){
68+
69+
let x1 = x+ dirs[i][0]
70+
let y1 = y+ dirs[i][1];
71+
let res1 = res + dirs[i][2];
72+
73+
let key = x1+"-"+y1+"-"+res1;
74+
75+
//bounbdary checker
76+
if(board[x1] && board[x1][y1] && !set.has(key)){
77+
if(board[x1][y1] == char){
78+
return {
79+
"ans" : res1+ "!",
80+
"x1" :x1,
81+
"y1" : y1
82+
}
83+
}
84+
85+
queue.push([x1,y1, res1])
86+
set.add(key)
87+
}
88+
89+
}
90+
91+
92+
}
93+
}
94+
95+
96+
};

basic-calculator-ii/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<h2>227. Basic Calculator II</h2><h3>Medium</h3><hr><div><p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>.&nbsp;</p>
2+
3+
<p>The integer division should truncate toward zero.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong>Example 1:</strong></p>
7+
<pre><strong>Input:</strong> s = "3+2*2"
8+
<strong>Output:</strong> 7
9+
</pre><p><strong>Example 2:</strong></p>
10+
<pre><strong>Input:</strong> s = " 3/2 "
11+
<strong>Output:</strong> 1
12+
</pre><p><strong>Example 3:</strong></p>
13+
<pre><strong>Input:</strong> s = " 3+5 / 2 "
14+
<strong>Output:</strong> 5
15+
</pre>
16+
<p>&nbsp;</p>
17+
<p><strong>Constraints:</strong></p>
18+
19+
<ul>
20+
<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
21+
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
22+
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
23+
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
24+
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
25+
</ul>
26+
</div>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var calculate = function(s) {
6+
7+
8+
let stack = [];
9+
10+
let num = "";
11+
let sign = "+"
12+
13+
for(let i=0; i <= s.length; i++){
14+
15+
if(s[i] == " ")continue;
16+
17+
18+
if(!isNaN(s[i])){
19+
num += s[i]
20+
21+
}else{
22+
console.log(sign)
23+
switch(sign){
24+
25+
case "+":{
26+
stack.push(Number(num))
27+
break;
28+
}
29+
30+
case "-":{
31+
console.log("Rankapjwdwq")
32+
stack.push(-Number(num))
33+
break;
34+
}
35+
36+
case "/":{
37+
let data = parseInt(stack.pop()/ Number(num))
38+
stack.push(data)
39+
break;
40+
41+
}
42+
43+
case "*":{
44+
let data = stack.pop()* Number(num)
45+
stack.push(data)
46+
// break;
47+
break;
48+
}
49+
50+
51+
52+
53+
54+
}
55+
56+
sign = s[i]
57+
num = ""
58+
}
59+
60+
61+
}
62+
63+
console.log(stack)
64+
65+
return stack.reduce((a,b)=> a+b,0)
66+
};

basic-calculator-iii/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<h2>772. Basic Calculator III</h2><h3>Hard</h3><hr><div><p>Implement a basic calculator to evaluate a simple expression string.</p>
2+
3+
<p>The expression string contains only non-negative integers, <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, <code>'/'</code> operators, open <code>'('</code> and closing parentheses <code>')'</code> and empty spaces <code>' '</code>. The integer division should <strong>truncate toward zero</strong>.</p>
4+
5+
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong>Example 1:</strong></p>
9+
<pre><strong>Input:</strong> s = "1 + 1"
10+
<strong>Output:</strong> 2
11+
</pre><p><strong>Example 2:</strong></p>
12+
<pre><strong>Input:</strong> s = " 6-4 / 2 "
13+
<strong>Output:</strong> 4
14+
</pre><p><strong>Example 3:</strong></p>
15+
<pre><strong>Input:</strong> s = "2*(5+5*2)/3+(6/2+8)"
16+
<strong>Output:</strong> 21
17+
</pre><p><strong>Example 4:</strong></p>
18+
<pre><strong>Input:</strong> s = "(2+6* 3+5- (3*14/7+2)*5)+3"
19+
<strong>Output:</strong> -12
20+
</pre><p><strong>Example 5:</strong></p>
21+
<pre><strong>Input:</strong> s = "0"
22+
<strong>Output:</strong> 0
23+
</pre>
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>1 &lt;= s &lt;= 10<sup>4</sup></code></li>
29+
<li><code>s</code> consists of digits, <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, <code>'/'</code>, <code>'('</code>, <code>')'</code> and&nbsp;<code>' '</code>.</li>
30+
<li><code>s</code> is a <strong>valid</strong> expression.</li>
31+
</ul>
32+
33+
<p>&nbsp;</p>
34+
<strong>Follow up:</strong> Could you solve the problem without using built-in library functions?</div>

0 commit comments

Comments
 (0)