【LeetCode OJ 136】Single Number
生活随笔
收集整理的這篇文章主要介紹了
【LeetCode OJ 136】Single Number
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:https://leetcode.com/problems/single-number/
題目:Given an array of integers, every element appears?twice?except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
解題思路:題意為:給定一個數組。僅僅有一個元素出現了一次。其他元素都出現了兩次,找出那個僅僅出現一次的數。
能夠遍歷數組。分別進行異或運算。
注:異或運算:同樣為0,不同為1。遍歷并異或的結果就是那個僅僅出現了一次的數。
演示樣例代碼:
public class Solution {public int singleNumber(int[] nums) {int result=nums[0];for (int i = 1; i < nums.length; i++){result^=nums[i];}return result;} }
總結
以上是生活随笔為你收集整理的【LeetCode OJ 136】Single Number的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android蓝牙串口程序开发
- 下一篇: 阿里云全球首批MVP张建平专访 - 我对