mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-03 05:27:55 +08:00
Update linear_search and hashing_search.
This commit is contained in:
@ -20,7 +20,7 @@ comments: true
|
||||
|
||||
```java title="hashing_search.java"
|
||||
/* 哈希查找(数组) */
|
||||
int hashingSearch(Map<Integer, Integer> map, int target) {
|
||||
int hashingSearchArray(Map<Integer, Integer> map, int target) {
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 -1
|
||||
return map.getOrDefault(target, -1);
|
||||
@ -31,7 +31,7 @@ comments: true
|
||||
|
||||
```cpp title="hashing_search.cpp"
|
||||
/* 哈希查找(数组) */
|
||||
int hashingSearch(unordered_map<int, int> map, int target) {
|
||||
int hashingSearchArray(unordered_map<int, int> map, int target) {
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 -1
|
||||
if (map.find(target) == map.end())
|
||||
@ -44,7 +44,7 @@ comments: true
|
||||
|
||||
```python title="hashing_search.py"
|
||||
""" 哈希查找(数组) """
|
||||
def hashing_search(mapp, target):
|
||||
def hashing_search_array(mapp, target):
|
||||
# 哈希表的 key: 目标元素,value: 索引
|
||||
# 若哈希表中无此 key ,返回 -1
|
||||
return mapp.get(target, -1)
|
||||
@ -54,7 +54,7 @@ comments: true
|
||||
|
||||
```go title="hashing_search.go"
|
||||
/* 哈希查找(数组) */
|
||||
func hashingSearch(m map[int]int, target int) int {
|
||||
func hashingSearchArray(m map[int]int, target int) int {
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 -1
|
||||
if index, ok := m[target]; ok {
|
||||
@ -69,7 +69,7 @@ comments: true
|
||||
|
||||
```js title="hashing_search.js"
|
||||
/* 哈希查找(数组) */
|
||||
function hashingSearch(map, target) {
|
||||
function hashingSearchArray(map, target) {
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 -1
|
||||
return map.has(target) ? map.get(target) : -1;
|
||||
@ -80,7 +80,7 @@ comments: true
|
||||
|
||||
```typescript title="hashing_search.ts"
|
||||
/* 哈希查找(数组) */
|
||||
function hashingSearch(map: Map<number, number>, target: number): number {
|
||||
function hashingSearchArray(map: Map<number, number>, target: number): number {
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 -1
|
||||
return map.has(target) ? map.get(target) as number : -1;
|
||||
@ -97,7 +97,7 @@ comments: true
|
||||
|
||||
```csharp title="hashing_search.cs"
|
||||
/* 哈希查找(数组) */
|
||||
int hashingSearch(Dictionary<int, int> map, int target)
|
||||
int hashingSearchArray(Dictionary<int, int> map, int target)
|
||||
{
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 -1
|
||||
@ -109,7 +109,7 @@ comments: true
|
||||
|
||||
```swift title="hashing_search.swift"
|
||||
/* 哈希查找(数组) */
|
||||
func hashingSearch(map: [Int: Int], target: Int) -> Int {
|
||||
func hashingSearchArray(map: [Int: Int], target: Int) -> Int {
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
// 若哈希表中无此 key ,返回 -1
|
||||
return map[target, default: -1]
|
||||
@ -130,7 +130,7 @@ comments: true
|
||||
|
||||
```java title="hashing_search.java"
|
||||
/* 哈希查找(链表) */
|
||||
ListNode hashingSearch1(Map<Integer, ListNode> map, int target) {
|
||||
ListNode hashingSearchLinkedList(Map<Integer, ListNode> map, int target) {
|
||||
// 哈希表的 key: 目标结点值,value: 结点对象
|
||||
// 若哈希表中无此 key ,返回 null
|
||||
return map.getOrDefault(target, null);
|
||||
@ -141,7 +141,7 @@ comments: true
|
||||
|
||||
```cpp title="hashing_search.cpp"
|
||||
/* 哈希查找(链表) */
|
||||
ListNode* hashingSearch1(unordered_map<int, ListNode*> map, int target) {
|
||||
ListNode* hashingSearchLinkedList(unordered_map<int, ListNode*> map, int target) {
|
||||
// 哈希表的 key: 目标结点值,value: 结点对象
|
||||
// 若哈希表中无此 key ,返回 nullptr
|
||||
if (map.find(target) == map.end())
|
||||
@ -154,7 +154,7 @@ comments: true
|
||||
|
||||
```python title="hashing_search.py"
|
||||
""" 哈希查找(链表) """
|
||||
def hashing_search1(mapp, target):
|
||||
def hashing_search_linkedlist(mapp, target):
|
||||
# 哈希表的 key: 目标元素,value: 结点对象
|
||||
# 若哈希表中无此 key ,返回 -1
|
||||
return mapp.get(target, -1)
|
||||
@ -164,7 +164,7 @@ comments: true
|
||||
|
||||
```go title="hashing_search.go"
|
||||
/* 哈希查找(链表) */
|
||||
func hashingSearch1(m map[int]*ListNode, target int) *ListNode {
|
||||
func hashingSearchLinkedList(m map[int]*ListNode, target int) *ListNode {
|
||||
// 哈希表的 key: 目标结点值,value: 结点对象
|
||||
// 若哈希表中无此 key ,返回 nil
|
||||
if node, ok := m[target]; ok {
|
||||
@ -179,7 +179,7 @@ comments: true
|
||||
|
||||
```js title="hashing_search.js"
|
||||
/* 哈希查找(链表) */
|
||||
function hashingSearch1(map, target) {
|
||||
function hashingSearchLinkedList(map, target) {
|
||||
// 哈希表的 key: 目标结点值,value: 结点对象
|
||||
// 若哈希表中无此 key ,返回 null
|
||||
return map.has(target) ? map.get(target) : null;
|
||||
@ -190,7 +190,7 @@ comments: true
|
||||
|
||||
```typescript title="hashing_search.ts"
|
||||
/* 哈希查找(链表) */
|
||||
function hashingSearch1(map: Map<number, ListNode>, target: number): ListNode | null {
|
||||
function hashingSearchLinkedList(map: Map<number, ListNode>, target: number): ListNode | null {
|
||||
// 哈希表的 key: 目标结点值,value: 结点对象
|
||||
// 若哈希表中无此 key ,返回 null
|
||||
return map.has(target) ? map.get(target) as ListNode : null;
|
||||
@ -207,7 +207,7 @@ comments: true
|
||||
|
||||
```csharp title="hashing_search.cs"
|
||||
/* 哈希查找(链表) */
|
||||
ListNode? hashingSearch1(Dictionary<int, ListNode> map, int target)
|
||||
ListNode? hashingSearchLinkedList(Dictionary<int, ListNode> map, int target)
|
||||
{
|
||||
|
||||
// 哈希表的 key: 目标结点值,value: 结点对象
|
||||
@ -220,7 +220,7 @@ comments: true
|
||||
|
||||
```swift title="hashing_search.swift"
|
||||
/* 哈希查找(链表) */
|
||||
func hashingSearch1(map: [Int: ListNode], target: Int) -> ListNode? {
|
||||
func hashingSearchLinkedList(map: [Int: ListNode], target: Int) -> ListNode? {
|
||||
// 哈希表的 key: 目标结点值,value: 结点对象
|
||||
// 若哈希表中无此 key ,返回 null
|
||||
return map[target]
|
||||
|
||||
@ -16,7 +16,7 @@ comments: true
|
||||
|
||||
```java title="linear_search.java"
|
||||
/* 线性查找(数组) */
|
||||
int linearSearch(int[] nums, int target) {
|
||||
int linearSearchArray(int[] nums, int target) {
|
||||
// 遍历数组
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
// 找到目标元素,返回其索引
|
||||
@ -32,7 +32,7 @@ comments: true
|
||||
|
||||
```cpp title="linear_search.cpp"
|
||||
/* 线性查找(数组) */
|
||||
int linearSearch(vector<int>& nums, int target) {
|
||||
int linearSearchArray(vector<int>& nums, int target) {
|
||||
// 遍历数组
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
// 找到目标元素,返回其索引
|
||||
@ -48,7 +48,7 @@ comments: true
|
||||
|
||||
```python title="linear_search.py"
|
||||
""" 线性查找(数组) """
|
||||
def linear_search(nums, target):
|
||||
def linear_search_array(nums, target):
|
||||
# 遍历数组
|
||||
for i in range(len(nums)):
|
||||
if nums[i] == target: # 找到目标元素,返回其索引
|
||||
@ -60,7 +60,7 @@ comments: true
|
||||
|
||||
```go title="linear_search.go"
|
||||
/* 线性查找(数组) */
|
||||
func linerSearchArray(nums []int, target int) int {
|
||||
func linearSearchArray(nums []int, target int) int {
|
||||
// 遍历数组
|
||||
for i := 0; i < len(nums); i++ {
|
||||
// 找到目标元素,返回其索引
|
||||
@ -118,7 +118,7 @@ comments: true
|
||||
|
||||
```csharp title="linear_search.cs"
|
||||
/* 线性查找(数组) */
|
||||
int linearSearch(int[] nums, int target)
|
||||
int linearSearchArray(int[] nums, int target)
|
||||
{
|
||||
// 遍历数组
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
@ -137,7 +137,7 @@ comments: true
|
||||
|
||||
```swift title="linear_search.swift"
|
||||
/* 线性查找(数组) */
|
||||
func linearSearch(nums: [Int], target: Int) -> Int {
|
||||
func linearSearchArray(nums: [Int], target: Int) -> Int {
|
||||
// 遍历数组
|
||||
for i in nums.indices {
|
||||
// 找到目标元素,返回其索引
|
||||
@ -162,7 +162,7 @@ comments: true
|
||||
|
||||
```java title="linear_search.java"
|
||||
/* 线性查找(链表) */
|
||||
ListNode linearSearch(ListNode head, int target) {
|
||||
ListNode linearSearchLinkedList(ListNode head, int target) {
|
||||
// 遍历链表
|
||||
while (head != null) {
|
||||
// 找到目标结点,返回之
|
||||
@ -179,7 +179,7 @@ comments: true
|
||||
|
||||
```cpp title="linear_search.cpp"
|
||||
/* 线性查找(链表) */
|
||||
ListNode* linearSearch(ListNode* head, int target) {
|
||||
ListNode* linearSearchLinkedList(ListNode* head, int target) {
|
||||
// 遍历链表
|
||||
while (head != nullptr) {
|
||||
// 找到目标结点,返回之
|
||||
@ -196,7 +196,7 @@ comments: true
|
||||
|
||||
```python title="linear_search.py"
|
||||
""" 线性查找(链表) """
|
||||
def linear_search1(head, target):
|
||||
def linear_search_linkedlist(head, target):
|
||||
# 遍历链表
|
||||
while head:
|
||||
if head.val == target: # 找到目标结点,返回之
|
||||
@ -269,7 +269,7 @@ comments: true
|
||||
|
||||
```csharp title="linear_search.cs"
|
||||
/* 线性查找(链表) */
|
||||
ListNode? linearSearch(ListNode head, int target)
|
||||
ListNode? linearSearchLinkedList(ListNode head, int target)
|
||||
{
|
||||
// 遍历链表
|
||||
while (head != null)
|
||||
@ -288,7 +288,7 @@ comments: true
|
||||
|
||||
```swift title="linear_search.swift"
|
||||
/* 线性查找(链表) */
|
||||
func linearSearch(head: ListNode?, target: Int) -> ListNode? {
|
||||
func linearSearchLinkedList(head: ListNode?, target: Int) -> ListNode? {
|
||||
var head = head
|
||||
// 遍历链表
|
||||
while head != nil {
|
||||
|
||||
Reference in New Issue
Block a user