mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-11 08:20:38 +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:
@@ -11,13 +11,13 @@ import utils.*;
|
||||
public class linear_search {
|
||||
/* Linear search (array) */
|
||||
static int linearSearchArray(int[] nums, int target) {
|
||||
// Traverse array
|
||||
// Traverse the array
|
||||
for (int i = 0; i < nums.length; 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;
|
||||
}
|
||||
|
||||
@@ -25,26 +25,26 @@ public class linear_search {
|
||||
static ListNode linearSearchLinkedList(ListNode head, int target) {
|
||||
// Traverse the linked list
|
||||
while (head != null) {
|
||||
// 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 null
|
||||
// The target node was not found; return null
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int target = 3;
|
||||
|
||||
/* Perform linear search in array */
|
||||
/* Perform linear search on the array */
|
||||
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
|
||||
int index = linearSearchArray(nums, target);
|
||||
System.out.println("Index of target element 3 = " + index);
|
||||
|
||||
/* Perform linear search in linked list */
|
||||
/* Perform linear search on the linked list */
|
||||
ListNode head = ListNode.arrToLinkedList(nums);
|
||||
ListNode node = linearSearchLinkedList(head, target);
|
||||
System.out.println("Node object corresponding to target node value 3 is " + node);
|
||||
System.out.println("The node object corresponding to target value 3 is " + node);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user