代码随想录Day06(力扣242.有效的字母异位词&349.两个数组的交集&202.快乐数]&1.两数之和)

文章介绍了Java编程中的四个问题:判断字符串是否为有效字母异位词、找出两个数组的交集、检测快乐数以及寻找数组中两数之和。涉及到的方法包括计数数组、哈希表、双指针等技巧。
摘要由CSDN通过智能技术生成

代码随想录Day06

242.有效的字母异位词

  1. 根据题意要符合条件,至少两个字符串长度相等,所以先确认二者长度是否相等,不等直接false
  2. 相等,定义一个26字母的计数数组
  3. 遍历字符串s和t,计数时:s中的字母 +1,t中的字符 -1
  4. 最后判断计数数组是否全为零,若不全为零则false
class Solution {
    public boolean isAnagram(String s, String t) {
        //如果两个字符串长度不相等那么它们一定不符合题意
        if(s.length() != t.length()){
            return false;
        }

        //定义一个字母数组,存储每个字母出现的次数
        //charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1
        int[] nums = new int[26];
        for(int i=0;i<s.length();i++){
            nums[s.charAt(i) - 'a']++;  //s中的每个字符,增加数组中每个字母对应计数
            nums[t.charAt(i) - 'a']--;  //减少数组中每个字母对应计数
        }

        //校验nums数组中是否存在非零值,若存在则不符合条件
        for(int i=0;i<26;i++){
            if(nums[i] != 0){
                return false;
            }
        }
         return true;
    }
}

349.两个数组的交集

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        //定义两个HashSet对象(利用HashSet自动去重的特性)
        Set<Integer> set1=new HashSet<>();
        Set<Integer> set2=new HashSet<>();
        
        //将nums1放入set1中
        for(int num : nums1){
            set1.add(num);
        }

        //遍历nums2,将二者交集放入set2中
        for(int num : nums2){
            if(set1.contains(num)){
                set2.add(num);
            }
        }

        //将set2转换为int数组,然后return
        int[] intArray = new int[set2.size()];  
        int index = 0;  
        for (Integer num : set2) {  
            intArray[index++] = num;  
        }  
        return intArray;
    }
}

202.快乐数

在这里插入图片描述

哈希表

class Solution {
    //计算快乐数
    private int Sum(int n){
        int sum = 0;
        while(n>0){
            int m=n%10;
            sum += m * m;
            n /= 10;
        }
        return sum;
    }

    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();  
        while(n != 1 && !set.contains(n)){  
            set.add(n);  
            n = Sum(n);  
        }  
    return n == 1; 
    }
}

双指针

“快指针” 每次走两步,“慢指针” 每次走一步,若两个指针最后能相遇,则说明这个数组存在循环,不符合快乐数的要求;

public class Solution {  
      
    // 计算快乐数的值
    private int Sum(int n) {  
        int sum = 0;  
        while (n > 0) {  
            int m = n % 10; 
            sum += m * m;  
            n /= 10;      
        }  
        return sum;  
    }  
  
    // 判断一个数是否为快乐数  
    public boolean isHappy(int n) {  
        // 定义快慢指针,初始时都指向n  
        int slow = n;  
        int fast = Sum(n);  
          
        // 当fast不等于1,且slow和fast不相遇(即未进入循环)时继续循环  
        while (fast != 1 && slow != fast) {  
            slow = Sum(slow);  // 慢指针每次移动一步  
            fast = Sum(Sum(fast)); // 快指针每次移动两步  
        }  
          
        // 如果fast等于1,说明找到了快乐数,返回true;否则返回false  
        return fast == 1;  
    }  
}

1.两数之和

在这里插入图片描述

class Solution {
    public int[] twoSum(int[] nums, int target) {
        //定义一个value-key的哈希表
        HashMap<Integer,Integer> HashSet = new HashMap<>();
        //遍历数组
        for(int i=0;i<nums.length;i++){
            //need是与当前数互补的值
            int need = target - nums[i];
            //检查哈希表中是否存在need
            if(HashSet.containsKey(need)){
                //若存在,返回结果
                return new int[]{HashSet.get(need),i};
            }
            //不存在,将value值,及其key存入我们的哈希表中
            HashSet.put(nums[i],i);
        }
        //若没有符合条件的数,则返回null
        return null;
    }
}
在 C 语言中,给定两个无序整数组 A 和 B,要找到它们的交集,可以使用哈希集合(如标准库中的 `std::unordered_set` 或自定义的散列表结构)来解决这个问题。以下是基本步骤: 1. 创建一个空的哈希集合(集合不会包含重复元素)。 2. 遍历数组 A,将每个元素添加到集合中。 3. 再次遍历数组 B,对于集合中已存在的元素(即存在于数组 A 中),记录下来。 4. 返回收集到的所有共同元素。 下面是一个简单的示例代码片段: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 typedef struct Node { int data; struct Node* next; } Node; // 自定义链表辅助函 Node* createSet(int arr[], int n) { Node* head = NULL; for (int i = 0; i < n; i++) { if (!head) { head = (Node*)malloc(sizeof(Node)); head->data = arr[i]; head->next = NULL; } else { Node* temp = head; while (temp->next != NULL && arr[i] > temp->next->data) temp = temp->next; if (arr[i] <= temp->next->data) continue; Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = arr[i]; newNode->next = temp->next; temp->next = newNode; } } return head; } int* intersect(int* nums1, int m, int* nums2, int n) { Node* set = createSet(nums1, m); int intersection[MAX_SIZE] = {0}; int count = 0; for (int i = 0; i < n; i++) { if (find(set, nums2[i])) { intersection[count++] = nums2[i]; } } // 如果没有任何交集,返回空指针 if (count == 0) return NULL; // 缩小结果数组大小并返回 intersection[count] = 0; return intersection; } // 查找链表中是否存在指定值 int find(Node* head, int value) { while (head != NULL && head->data != value) head = head->next; return head != NULL; } void printArray(int arr[], int size) { Node* temp = arr; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main() { int nums1[] = {1, 2, 2, 1}; // 示例数组 A int nums2[] = {2, 2}; // 示例数组 B int m = sizeof(nums1) / sizeof(nums1[0]); int n = sizeof(nums2) / sizeof(nums2[0]); int* result = intersect(nums1, m, nums2, n); if (result) { printf("Intersection: "); printArray(result, count); } else { printf("No common elements.\n"); } free(result); // 释放内存 return 0; } ``` 在这个例子中,我们首先创建了一个链表表示数组 A 的唯一元素,然后遍历数组 B,查找链表中存在的元素,并将它们存入新数组 `intersection`。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值