mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-15 09:57:09 +00:00
Compare commits
6 Commits
c45b01a838
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 88b99caf11 | |||
| 2c96c57870 | |||
| 6bc1bcb772 | |||
| f174c1edea | |||
| 3c88ab7152 | |||
| cbef5fb714 |
@@ -0,0 +1,68 @@
|
|||||||
|
# Definition for singly-linked list.
|
||||||
|
# class ListNode:
|
||||||
|
# def __init__(self, val=0, next=None):
|
||||||
|
# self.val = val
|
||||||
|
# self.next = next
|
||||||
|
class Solution:
|
||||||
|
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
|
||||||
|
if k == 1:
|
||||||
|
return head
|
||||||
|
|
||||||
|
dummyHead = ListNode(-1, head)
|
||||||
|
|
||||||
|
beforeSeg = dummyHead
|
||||||
|
firstInSeg = head
|
||||||
|
lastInSeg = None
|
||||||
|
afterSeg = None
|
||||||
|
|
||||||
|
# Get initial values for lastInSeg and afterSeg
|
||||||
|
curr = head
|
||||||
|
for i in range(k):
|
||||||
|
if i == k - 1:
|
||||||
|
lastInSeg = curr
|
||||||
|
afterSeg = curr.next
|
||||||
|
elif curr is None:
|
||||||
|
return head
|
||||||
|
|
||||||
|
curr = curr.next
|
||||||
|
|
||||||
|
|
||||||
|
# hold one before segment
|
||||||
|
# hold last in segment
|
||||||
|
# hold one after segment
|
||||||
|
|
||||||
|
while lastInSeg is not None:
|
||||||
|
# in segment, flip all links
|
||||||
|
prev = None
|
||||||
|
curr = beforeSeg.next
|
||||||
|
next = beforeSeg.next.next
|
||||||
|
|
||||||
|
for i in range(k):
|
||||||
|
newPrev = curr
|
||||||
|
newCurr = next
|
||||||
|
newNext = next.next if next is not None else None
|
||||||
|
|
||||||
|
curr.next = prev
|
||||||
|
|
||||||
|
prev, curr, next = newPrev, newCurr, newNext
|
||||||
|
|
||||||
|
|
||||||
|
beforeSeg.next = lastInSeg
|
||||||
|
firstInSeg.next = afterSeg
|
||||||
|
|
||||||
|
beforeSeg = firstInSeg
|
||||||
|
firstInSeg = afterSeg
|
||||||
|
|
||||||
|
curr = firstInSeg
|
||||||
|
lastInSeg = None
|
||||||
|
afterSeg = None
|
||||||
|
for i in range(k):
|
||||||
|
if curr is None:
|
||||||
|
break
|
||||||
|
elif i == k - 1:
|
||||||
|
lastInSeg = curr
|
||||||
|
afterSeg = curr.next
|
||||||
|
|
||||||
|
curr = curr.next
|
||||||
|
|
||||||
|
return dummyHead.next
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<h2><a href="https://leetcode.com/problems/reverse-nodes-in-k-group">25. Reverse Nodes in k-Group</a></h2><h3>Hard</h3><hr><p>Given the <code>head</code> of a linked list, reverse the nodes of the list <code>k</code> at a time, and return <em>the modified list</em>.</p>
|
||||||
|
|
||||||
|
<p><code>k</code> is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of <code>k</code> then left-out nodes, in the end, should remain as it is.</p>
|
||||||
|
|
||||||
|
<p>You may not alter the values in the list's nodes, only nodes themselves may be changed.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex1.jpg" style="width: 542px; height: 222px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> head = [1,2,3,4,5], k = 2
|
||||||
|
<strong>Output:</strong> [2,1,4,3,5]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex2.jpg" style="width: 542px; height: 222px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> head = [1,2,3,4,5], k = 3
|
||||||
|
<strong>Output:</strong> [3,2,1,4,5]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>The number of nodes in the list is <code>n</code>.</li>
|
||||||
|
<li><code>1 <= k <= n <= 5000</code></li>
|
||||||
|
<li><code>0 <= Node.val <= 1000</code></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Follow-up:</strong> Can you solve the problem in <code>O(1)</code> extra memory space?</p>
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
"""
|
||||||
|
# Definition for a Node.
|
||||||
|
class Node:
|
||||||
|
def __init__(self, val = 0, neighbors = None):
|
||||||
|
self.val = val
|
||||||
|
self.neighbors = neighbors if neighbors is not None else []
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
from collections import deque
|
||||||
|
class Solution:
|
||||||
|
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
|
||||||
|
if not node:
|
||||||
|
return node
|
||||||
|
|
||||||
|
newHead = Node(node.val)
|
||||||
|
|
||||||
|
frontier = deque([ node ])
|
||||||
|
nodeMapping = { node.val: newHead }
|
||||||
|
while len(frontier) > 0:
|
||||||
|
current = frontier.popleft()
|
||||||
|
|
||||||
|
for n in current.neighbors:
|
||||||
|
if n.val not in nodeMapping:
|
||||||
|
newNode = Node(n.val)
|
||||||
|
nodeMapping[newNode.val] = newNode
|
||||||
|
frontier.append(n)
|
||||||
|
nodeMapping[current.val].neighbors.append(nodeMapping[n.val])
|
||||||
|
|
||||||
|
return newHead
|
||||||
|
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
<h2><a href="https://leetcode.com/problems/clone-graph">133. Clone Graph</a></h2><h3>Medium</h3><hr><p>Given a reference of a node in a <strong><a href="https://en.wikipedia.org/wiki/Connectivity_(graph_theory)#Connected_graph" target="_blank">connected</a></strong> undirected graph.</p>
|
||||||
|
|
||||||
|
<p>Return a <a href="https://en.wikipedia.org/wiki/Object_copying#Deep_copy" target="_blank"><strong>deep copy</strong></a> (clone) of the graph.</p>
|
||||||
|
|
||||||
|
<p>Each node in the graph contains a value (<code>int</code>) and a list (<code>List[Node]</code>) of its neighbors.</p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
class Node {
|
||||||
|
public int val;
|
||||||
|
public List<Node> neighbors;
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>Test case format:</strong></p>
|
||||||
|
|
||||||
|
<p>For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with <code>val == 1</code>, the second node with <code>val == 2</code>, and so on. The graph is represented in the test case using an adjacency list.</p>
|
||||||
|
|
||||||
|
<p><b>An adjacency list</b> is a collection of unordered <b>lists</b> used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.</p>
|
||||||
|
|
||||||
|
<p>The given node will always be the first node with <code>val = 1</code>. You must return the <strong>copy of the given node</strong> as a reference to the cloned graph.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png" style="width: 454px; height: 500px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> adjList = [[2,4],[1,3],[2,4],[1,3]]
|
||||||
|
<strong>Output:</strong> [[2,4],[1,3],[2,4],[1,3]]
|
||||||
|
<strong>Explanation:</strong> There are 4 nodes in the graph.
|
||||||
|
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
|
||||||
|
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
|
||||||
|
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
|
||||||
|
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/07/graph.png" style="width: 163px; height: 148px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> adjList = [[]]
|
||||||
|
<strong>Output:</strong> [[]]
|
||||||
|
<strong>Explanation:</strong> Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> adjList = []
|
||||||
|
<strong>Output:</strong> []
|
||||||
|
<strong>Explanation:</strong> This an empty graph, it does not have any nodes.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>The number of nodes in the graph is in the range <code>[0, 100]</code>.</li>
|
||||||
|
<li><code>1 <= Node.val <= 100</code></li>
|
||||||
|
<li><code>Node.val</code> is unique for each node.</li>
|
||||||
|
<li>There are no repeated edges and no self-loops in the graph.</li>
|
||||||
|
<li>The Graph is connected and all nodes can be visited starting from the given node.</li>
|
||||||
|
</ul>
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
class Solution:
|
||||||
|
def calculate(self, s: str) -> int:
|
||||||
|
"""
|
||||||
|
Complexities:
|
||||||
|
Time: O(n)
|
||||||
|
Space: O(n)
|
||||||
|
|
||||||
|
where n = len(s)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
numbers = "0123456789"
|
||||||
|
|
||||||
|
# Read from left to right: save in stack (full number or operator)
|
||||||
|
# - save number
|
||||||
|
# - on operator:
|
||||||
|
# -- if * or /, evaluate
|
||||||
|
# -- if + or -, wait and continue
|
||||||
|
currentNumber = 0
|
||||||
|
stack = []
|
||||||
|
|
||||||
|
# Evaluate numbers and create stack (of additions + subtractions)
|
||||||
|
for c in s + "\0":
|
||||||
|
if c == " ":
|
||||||
|
continue
|
||||||
|
elif c in numbers:
|
||||||
|
digit = int(c)
|
||||||
|
currentNumber = (currentNumber * 10) + digit
|
||||||
|
else:
|
||||||
|
stack.append(currentNumber)
|
||||||
|
currentNumber = 0
|
||||||
|
|
||||||
|
if len(stack) >= 3 and stack[-2] in "*/":
|
||||||
|
rOperand = stack.pop()
|
||||||
|
operator = stack.pop()
|
||||||
|
lOperand = stack.pop()
|
||||||
|
|
||||||
|
if operator == "*":
|
||||||
|
stack.append(lOperand * rOperand)
|
||||||
|
elif operator == "/":
|
||||||
|
stack.append(int(lOperand / rOperand))
|
||||||
|
|
||||||
|
stack.append(c)
|
||||||
|
|
||||||
|
# Pop off null terminator
|
||||||
|
stack.pop()
|
||||||
|
|
||||||
|
# Evaluate all additions and subtractions from stack
|
||||||
|
current = stack[0]
|
||||||
|
for i in range(1, len(stack) - 1, 2):
|
||||||
|
operator = stack[i]
|
||||||
|
rOperand = stack[i + 1]
|
||||||
|
|
||||||
|
if operator == "+":
|
||||||
|
current += rOperand
|
||||||
|
elif operator == "-":
|
||||||
|
current -= rOperand
|
||||||
|
|
||||||
|
return current
|
||||||
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<h2><a href="https://leetcode.com/problems/basic-calculator-ii">227. Basic Calculator II</a></h2><h3>Medium</h3><hr><p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>. </p>
|
||||||
|
|
||||||
|
<p>The integer division should truncate toward zero.</p>
|
||||||
|
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<pre><strong>Input:</strong> s = "3+2*2"
|
||||||
|
<strong>Output:</strong> 7
|
||||||
|
</pre><p><strong class="example">Example 2:</strong></p>
|
||||||
|
<pre><strong>Input:</strong> s = " 3/2 "
|
||||||
|
<strong>Output:</strong> 1
|
||||||
|
</pre><p><strong class="example">Example 3:</strong></p>
|
||||||
|
<pre><strong>Input:</strong> s = " 3+5 / 2 "
|
||||||
|
<strong>Output:</strong> 5
|
||||||
|
</pre>
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
|
||||||
|
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
|
||||||
|
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
|
||||||
|
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
|
||||||
|
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
|
||||||
|
</ul>
|
||||||
Reference in New Issue
Block a user