Leetcode 136 只出現一次的數字 && Leecode 389 找不同
阿新 • • 發佈:2018-12-08
【136】
給定一個非空整數陣列,除了某個元素只出現一次以外,其餘每個元素均出現兩次。找出那個只出現了一次的元素。
說明:
你的演算法應該具有線性時間複雜度。 你可以不使用額外空間來實現嗎?
示例 1:
輸入: [2,2,1] 輸出: 1
示例 2:
輸入: [4,1,2,1,2] 輸出: 4
class Solution { public: int singleNumber(vector<int>& nums) { int res = 0; for(auto c : nums){ res ^= c; } return res; } };
【389】
給定兩個字串 s 和 t,它們只包含小寫字母。
字串 t 由字串 s 隨機重排,然後在隨機位置新增一個字母。
請找出在 t 中被新增的字母。
示例:
輸入: s = "abcd" t = "abcde" 輸出: e 解釋: 'e' 是那個被新增的字母。
class Solution { public: char findTheDifference(string s, string t) { int res=0; string m = s+t; for(auto c : m){ res ^=c; } return res; } };