You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
int left = 0, right = int(nums.size()) - 1;
while (left <= right) {
// Get the middle index and the middle value. int mid = left + (right - left) / 2;
// Case 1, return the middle index.if (nums[mid] == target) {
return mid;
}
// Case 2, discard the smaller half.elseif (nums[mid] < target) {
left = mid + 1;
}
// Case 3, discard the larger half.else {
right = mid - 1;
}
}
// If we finish the search without finding target, return -1.return -1;
foreachvertexuinG.V-{s}:#initializeu.color=WHITE#not visitedu.d=inf#distance from s to uu.pred=NIL#predecessors.color=GRAY# visitings.d=0s.pred=NILQ=Enqueue(Q,s);
whileQnotempty:
u=Dequeue(Q)
foreachvinG.Adj[u]:
v.color=Grayv.d=u.d+1v.pred=uEnqueue(Q,v)
u.color=Black# finish visiting u
Time:O(V+E)
Space:O(V)
DFS(G) G:graph
foreachvertexuinG.V: #initializeu.color=Whiteu.pred=NILtime=0foreachvertexuinG.V:
ifu.color=White:
DFS-VISIT(G,u)
DFS-VISIT(G,u):
time=time+1u.d=time#start time when discovering uu.color=GrayforeachvinG.Adj[u]:
ifv.color==White:
v.pred=uDFS-VISIT(G,v)
u.color=Blacktime=time+1u.f=time#time after finish visiting u