首页 > 其他分享 >【LeetCode】389_找不同_C

【LeetCode】389_找不同_C

时间:2024-03-04 19:00:25浏览次数:14  
标签:字符 ++ 不同 sum char int 字符串 389 LeetCode

题目描述

给定两个字符串 st ,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

https://leetcode.cn/problems/find-the-difference/description/

示例

示例 1:

输入:s = "abcd", t = "abcde"
输出:"e"
解释:'e' 是那个被添加的字母。

示例 2:

输入:s = "", t = "y"
输出:"y"

解题总结

本题可用三个方法来解决

  • 方法1

首先遍历字符串 s,对其中的每个字符都将计数值加 1;然后遍历字符串 t,对其中的每个字符都将计数值减 1。当发现某个字符计数值为负数时,说明该字符在字符串 t 中出现的次数大于在字符串 s 中出现的次数,因此该字符为被添加的字符。

此方法也是我解题时所用的方法,与“383.赎金信”一样,都是用哈希表的思想来解决问题

  • 方法2

分别对字符串 s 和 t 中每个字符的 ASCII 码的值求和,两者的差值即为被添加的字符

  • 方法3

将两个字符串拼接成一个字符串,则问题转换成求字符串中出现奇数次的字符,对这两个字符串中所有的字符逐个异或,最后的结果即为被添加的字符

异或运算的特点是

0 ^ 0 == 0

0 ^ 1 == 1

1 ^ 0 == 1

1 ^ 1 == 0

0 ^ x == x

x ^ x == 0

x ^ y == y ^ x

x ^ y ^ x == y

异或运算满足交换律和结合律,因此在本题中,字符串异或运算的过程大致可抽象为

a ^ c ^ b ^ c ^ d ^ a ^ d == a ^ a ^ c ^ c ^ d ^ d ^ b == 0 ^ 0 ^ 0 ^ b == b

b 即为被添加的字符

代码实现

  • 方法1的代码实现
char findTheDifference(char* s, char* t) {
    int hash[26] = {0};
    int i = 0;
    while(s[i] != 0)
    {
        hash[s[i] - 'a']++;
        i++;
    }
    i = 0;
    while(t[i] != 0)
    {
        hash[t[i] - 'a']--;
        i++;
    }
    char c = 0;
    for(i = 0; i < 26; i++)
    {
        if(hash[i] == -1)
            c = i + 'a';
    }
    return c;
}
  • 方法2的代码实现
char findTheDifference(char* s, char* t) {
    int sum_s = 0;
    int sum_t = 0;
    int i = 0;
    for(i = 0; i < strlen(s); i++)
        sum_s += s[i];
    for(i = 0; i < strlen(t); i++)
        sum_t += t[i];
    return sum_t - sum_s;
}
  • 方法3的代码实现
char findTheDifference(char* s, char* t) {
    int ret = 0;
    int i = 0;
    for(i = 0; i < strlen(s); i++)
        ret = ret ^ s[i];
    for(i = 0; i < strlen(t); i++)
        ret = ret ^ t[i];
    return ret;
}

标签:字符,++,不同,sum,char,int,字符串,389,LeetCode
From: https://www.cnblogs.com/changbaiqiusha/p/18052429

相关文章

  • Leetcode刷题第十六天-链表
    24:两两交换链表中的节点链接:24.两两交换链表中的节点-力扣(LeetCode)虚拟头节点#Definitionforsingly-linkedlist.#classListNode:#def__init__(self,val=0,next=None):#self.val=val#self.next=nextclassSolution:defswap......
  • C# 使用selenium 彻底解决浏览器版本不同的问题
    浏览器版本问题导致的错误如下:System.InvalidOperationException:“sessionnotcreated:ThisversionofChromeDriveronlysupportsChromeversion114Currentbrowserversionis122.0.6261.95withbinarypathC:\ProgramFiles\Google\Chrome\Application\chrome.exe(......
  • 【LeetCode】1768_交替合并字符串_C
    题目描述给你两个字符串word1和word2。请你从word1开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。返回合并后的字符串。示例示例1:输入:word1="abc",word2="pqr"输出:"apbqcr"解释:字符串合并情......
  • 【LeetCode】383_赎金信_C
    题目描述给你两个字符串:ransomNote和magazine,判断ransomNote能不能由magazine里面的字符构成。如果可以,返回true;否则返回false。magazine中的每个字符只能在ransomNote中使用一次。示例示例1:输入:ransomNote="a",magazine="b"输出:false示例2:输入:ran......
  • 代码随想录算法训练营day11 | leetcode 20. 有效的括号、1047. 删除字符串中的所有相
    目录题目链接:20.有效的括号-简单题目链接:1047.删除字符串中的所有相邻重复项-简单题目链接:150.逆波兰表达式求值-中等题目链接:20.有效的括号-简单题目描述:给定一个只包括'(',')','{','}','[',']'的字符串s,判断字符串是否有效。有效字符串需满足:左括号必须用相同类型的右......
  • 不同序构造二叉树
    一、根据前序与中序构造二叉树前序遍历:确定根节点root,最左端的节点即为根节点中序遍历:确定根节点左右两边的节点,通过计算左右两边节点集合的大小对root左节点集合与右节点集合执行重复操作,不断确定小集合的根节点,最终可构造出一整棵二叉树树的存储可以采用定义类或结构体,这......
  • 【LeetCode】876_链表的中间结点_C
    题目描述给你单链表的头结点head,请你找出并返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。https://leetcode.cn/problems/middle-of-the-linked-list/description/示例提示:链表的结点数范围是[1,100]1<=Node.val<=100解题思路思路一遍历链......
  • 代码随想录算法训练营day10 | leetcode 232. 用栈实现队列、225. 用队列实现栈
    目录题目链接:232.用栈实现队列-简单题目链接:225.用队列实现栈-简单题目链接:232.用栈实现队列-简单题目描述:请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):实现MyQueue类:voidpush(intx)将元素x推到队列的末尾intp......
  • LeetCode 2345. Finding the Number of Visible Mountains
    原题链接在这里:https://leetcode.com/problems/finding-the-number-of-visible-mountains/description/题目:Youaregivena 0-indexed 2Dintegerarray peaks where peaks[i]=[xi,yi] statesthatmountain i hasapeakatcoordinates (xi,yi).Amountaincan......
  • [LeetCode] 2864. Maximum Odd Binary Number
    Youaregivenabinarystringsthatcontainsatleastone'1'.Youhavetorearrangethebitsinsuchawaythattheresultingbinarynumberisthemaximumoddbinarynumberthatcanbecreatedfromthiscombination.Returnastringrepresentin......