[LeetCode] NO. 389 Find the Difference

[题目]

Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

Example:

Input:
s = "abcd"
t = "abcde"

Output:
e

Explanation:
‘e‘ is the letter that was added.

[题目解析] 根据题意,字符串中只含有小写字母,则可以用一个26位的数组表示map结构,记录对应字符串的字符和出现次数。如下。

   public char findTheDifference(String s, String t) {
       int[] hash = new int[26];
       char[] sarray = s.toCharArray();
       char[] tarray = t.toCharArray();
       for(int i = 0; i < s.length(); i++){
    	   int num = sarray[i] - ‘a‘;
    	   hash[num]++;
       }
       for(int i = 0; i < t.length(); i++){
    	   int tnum = tarray[i] - ‘a‘;
    	   hash[tnum]--;
    	   if(hash[tnum] < 0){
    		   return (char) ((char)tnum+‘a‘);
    	   }
       }
       return ‘-‘;
   }

  

时间: 2024-11-06 11:27:56

[LeetCode] NO. 389 Find the Difference的相关文章

LeetCode之389. Find the Difference

-------------------------------------------------- 先计算每个字母的出现次数然后减去,最后剩下的那一个就是后来添加的了. AC代码: public class Solution { public char findTheDifference(String s, String t) { int book[]=new int[26]; for(int i=0;i<s.length();i++) book[s.charAt(i)-'a']++; for

[leetcode] 389. Find the Difference 解题报告

Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was added in t. Example: Input: s = "abcd" t = "ab

Leetcode 389 Find the Difference

Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was added in t. Example: Input: s = "abcd" t = "ab

LeetCode算法题-Find the Difference(Java实现-五种解法)

这是悦乐书的第214次更新,第227篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第82题(顺位题号是389).给定两个字符串s和t,它们只包含小写字母.字符串t由随机混洗字符串s生成,然后在随机位置再添加一个字母.找到t中添加的字母.例如: 输入:s ="abcd", t ="abcde" 输出:'e' 说明:'e'是添加的字母. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Jav

389. Find the Difference

Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was added in t. Example: Input: s = "abcd" t = "ab

【leetcode】1200. Minimum Absolute Difference

题目如下: Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements. Return a list of pairs in ascending order(with respect to pairs), each pair [a, b] follows a, b are from arr a < b b -

【leetcode389】389. Find the Difference

异或 找不同 -.- 1 public class Solution { 2 public char findTheDifference(String s, String t) { 3 char temp = 0x00; 4 for(int i = 0;i < s.length();i++){ 5 temp =(char) (temp ^ s.charAt(i)); 6 } 7 for(int i = 0;i < t.length();i++){ 8 temp =(char) (temp ^

【leetcode?python】Find the Difference

#-*- coding: UTF-8 -*- class Solution(object):    def findTheDifference(self, s, t):                s=sorted(list(s))        t=sorted(list(t))           for st in s:                  p=t.index(st)            del t[p] return ''.join(t)                

Python解Leetcode: 539. Minimum Time Difference

题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值. 思路: 把给定的链表排序,并且在排序的同时把60进制的时间转化成十进制整数: 遍历排序的数组,求出两个相邻值之间的差值: 求出首尾两个值之间的差值. class Solution(object): def findMinDifference(self, timePoints): """ :type timePoints: List[str] :rtype: int """