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.

分析,找出多加上的单个字符。把s和t放到一起,则多余的一个一定是奇数个,可以用异或解决。

有问题是:

ch ^= t.charAt(i); 没有问题;
ch = ch^t.charAt(i);会报:
incompatible types: possible lossy conversion from int to char待解决~~
 

参考:http://www.cnblogs.com/baichangfu/p/7468682.html

class Solution {
    public char findTheDifference(String s, String t) {
        t = s + t;
        char ch = 0;
        for(int i = 0; i < t.length(); i++){
            ch ^= t.charAt(i);
        }
        return ch;
    }
}
时间: 2024-10-05 09:18:02

Find the Difference的相关文章

python: the difference between append and extend

Data Analysis: indoor localization using received signal strength (RSS) An error about list operation in python: append and extend elements We define a list A to storage objects, which the length is unknown, and list B and C to storage the final resu

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

The Difference Between a Router, Switch and Hub

Some technicians have a tendency to use the terms router, switch and hub interchangeably,  but have you ever wondered what the difference is? Some technicians have a tendency to use the terms router, hub and switch interchangeably. One minute they're

Leetcode 530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

The difference of Methods and Functions

Functions Functions are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed. Swift’s unified function syntax

lintcode 中等题:maximum subarray difference 最大子数组差

题目 最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 样例 给出数组[1, 2, -3, 1],返回 6 注意 子数组最少包含一个数 挑战 时间复杂度为O(n),空间复杂度为O(n) 解题 刚做了数组中两个子数组和的最大值,这一题是求差,感觉上题的求解思想应该是可以用的 A B 分别是两个子数组的和,则: 所以 当A>B 的时候A越大越好 B越小越好 当A<B 的时候B越大越好 A越小越好

[转载]Difference between &lt;context:annotation-config&gt; vs &lt;context:component-scan&gt;

在国外看到详细的说明一篇,非常浅显透彻.转给国内的筒子们:-) 原文标题: Spring中的<context:annotation-config>与<context:component-scan>到底有什么不同? 原文出处:http://stackoverflow.com/a/7456501 <context:annotation-config> is used to activate annotations in beans already registered in

Set Difference(所有子集的最值差)

点击打开题目链接https://www.codechef.com/problems/SETDIFF Set Difference Problem code: SETDIFF SUBMIT ALL SUBMISSIONS All submissions for this problem are available. Churu is working as a data scientist in Coderpur. He works on a lot of data on the daily bas

HDU 4715 Difference Between Primes (素数表+二分)

Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2998    Accepted Submission(s): 850 Problem Description All you know Goldbach conjecture.That is to say, Every even inte

difference between char *s and char s[ ]

The difference here is that : char *s = "hello,world!"; will place hello,world in read-only part of the memmory and makes s a pointer to that. making any writing operation on this illigal. while: char s[] = "hello,world!"; will puts th