标签:rand 遍历 int line one ret 插入 back div
Given two stringssandtwhich consist of only lowercase letters.
Stringtis generated by random shuffling stringsand then add one more letter at a random position.
Find the letter that was added int.
Example:
Input: s = "abcd" t = "abcde" Output: e Explanation: ‘e‘ is the letter that was added.题目大意是给定两个字符串s和t,t是由s打乱组合而成,并随机插入一个字母。找到这个添加的字母
public class Solution { public char findTheDifference(String s, String t) { int charCodeS = 0, charCodeT = 0; for (int i = 0; i < s.length(); ++i) charCodeS += (int)s.charAt(i); for (int i = 0; i < t.length(); ++i) charCodeT += (int)t.charAt(i); return (char)(charCodeT - charCodeS); } }
public char findTheDifference(String s, String t) { int b=0; for(int i = 0; i < t.length();i++) { b += (int)t.charAt(i); if(i < s.length()) b -= (int)s.charAt(i); } return (char)b; }
标签:rand 遍历 int line one ret 插入 back div
原文地址:http://www.cnblogs.com/wxshi/p/7598400.html