mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-23 20:22:56 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -8,13 +8,13 @@
|
||||
|
||||
/* Linear search (array) */
|
||||
int linearSearchArray(vector<int> &nums, int target) {
|
||||
// Traverse array
|
||||
// Traverse the array
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
// Found the target element, return its index
|
||||
// The target element was found; return its index
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
// Target element not found, return -1
|
||||
// The target element was not found; return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -22,28 +22,28 @@ int linearSearchArray(vector<int> &nums, int target) {
|
||||
ListNode *linearSearchLinkedList(ListNode *head, int target) {
|
||||
// Traverse the linked list
|
||||
while (head != nullptr) {
|
||||
// Found the target node, return it
|
||||
// The target node was found; return it
|
||||
if (head->val == target)
|
||||
return head;
|
||||
head = head->next;
|
||||
}
|
||||
// Target node not found, return nullptr
|
||||
// The target node was not found; return nullptr
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
int target = 3;
|
||||
|
||||
/* Perform linear search in array */
|
||||
/* Perform linear search on the array */
|
||||
vector<int> nums = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8};
|
||||
int index = linearSearchArray(nums, target);
|
||||
cout << "Index of target element 3 = " << index << endl;
|
||||
|
||||
/* Perform linear search in linked list */
|
||||
/* Perform linear search on the linked list */
|
||||
ListNode *head = vecToLinkedList(nums);
|
||||
ListNode *node = linearSearchLinkedList(head, target);
|
||||
cout << "Node object corresponding to target node value 3 is " << node << endl;
|
||||
cout << "The node object corresponding to target value 3 is " << node << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user