389. Find the Difference

Easy

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.
package leetcode.easy;

public class FindTheDifference {
public char findTheDifference(String s, String t) {
char[] first = s.toCharArray();
char[] second = t.toCharArray();
int res = 0;
for (int i = 0; i < first.length; i++) {
res += second[i];
res -= first[i];
}
res += second[second.length - 1];
return (char) res;
} @org.junit.Test
public void test() {
System.out.println(findTheDifference("abcd", "abcde"));
}
}

LeetCode_389. Find the Difference的更多相关文章

  1. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  2. What's the difference between a stub and mock?

    I believe the biggest distinction is that a stub you have already written with predetermined behavio ...

  3. [转载]Difference between <context:annotation-config> vs <context:component-scan>

    在国外看到详细的说明一篇,非常浅显透彻.转给国内的筒子们:-) 原文标题: Spring中的<context:annotation-config>与<context:componen ...

  4. What's the difference between <b> and <strong>, <i> and <em> in HTML/XHTML? When should you use each?

    ref:http://stackoverflow.com/questions/271743/whats-the-difference-between-b-and-strong-i-and-em The ...

  5. difference between forward and sendredirect

    Difference between SendRedirect and forward is one of classical interview questions asked during jav ...

  6. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

  7. MySQL: @variable vs. variable. Whats the difference?

    MySQL: @variable vs. variable. Whats the difference?   up vote351down votefavorite 121 In another qu ...

  8. Distribute numbers to two “containers” and minimize their difference of sum

    it can be solved by Dynamical Programming.Here are some useful link: Tutorial and Code: http://www.c ...

  9. difference between append and appendTo

    if you need append some string to element and need set some attribute on these string at the same ti ...

随机推荐

  1. nginx配置静态资源:配置绝对路径

    nginx配置静态资源:配置绝对路径 项目都是html格式的文件,我的项目路径:E:\javaservice\nginx-1.15.7\html assets:静态资源 html:站点文件 uploa ...

  2. PAT乙级1040 有几个PAT

    题目: 1040 有几个PAT (25分)   字符串 APPAPT 中包含了两个单词 PAT,其中第一个 PAT 是第 2 位(P),第 4 位(A),第 6 位(T):第二个 PAT 是第 3 位 ...

  3. [React] Write a Custom State Hook in React

    Writing your own custom State Hook is not as a daunting as you think. To keep things simple, we'll r ...

  4. 在golang中使用json

    jsoniter高性能json库 非常快,支持java和go marshal使用的一些坑 package main import ( "encoding/json" "f ...

  5. $spfa-dfs$优化板子

    \(spfa-dfs\)优化板子 快速判断是否存在负环(没负环时不要作死用) bool spfa(int u){ vis[u]=1; for(register int i=head[u];i;i=nx ...

  6. Windows系统清除占用的串口号列表批处理

    蛋疼总是无缘无故被占用 @echo off reg query "HKLM\SYSTEM\CurrentControlSet\Control\COM Name Arbiter" / ...

  7. SpringBoot集成mybatis,同时读取一个数据库中多个数据表

    SpringBoot集成mybatis,同时读取一个数据库中多个数据表: application.properties: mybatis.config-location=classpath:mybat ...

  8. RabbitMQ Management API调用

    RabbitMQ在运行时,偶尔会有一些死信,即消息未正常消费,造成消息积压在消息队列中, 一直卡住,重复循环消费原来的消息队列 那么就需要有一种机制,来查看RabbitMQ是否有消息未正常消费,从而让 ...

  9. 单点登录 sso -- cas CAS 原理 流程 分析

    Yelu大学研发的CAS(Central Authentication Server) 下面就以耶鲁大学研发的CAS为分析依据,分析其工作原理.首先看一下最上层的项目部署图: 部署项目时需要部署一个独 ...

  10. vmWare安装centos7之后使用yum安装提示there are on enabled repos(修改yum源)

    可以使用这个命令修改yum源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.rep ...