演算法系列 Missing Number

2021-08-04 02:59:17 字數 544 閱讀 9039

given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.

for example,

given nums = [0, 1, 3] return 2.

note:

your algorithm should run in linear runtime complexity. could you implement it using only constant extra space complexity?

還是考察位操作。.那麼思路是既然0到n之間少了乙個數,我們將這個少了乙個數的數組合0到n之間完整的陣列異或一下,那麼相同的數字都變為0了,剩下的就是少了的那個數字了。

也可以利用求和公式,算出 0-n的和,然後將陣列求和,做差即可。

**不再給出。

public

class solution

}

java演算法系列

棧的概念 棧是一種特殊的線性表,堆疊的資料元素以及資料元素之間的關係和線性表是完全一樣的。差別是線性表是在任意位置進行插入和刪除操作,棧是只允許在固定的一端進行插入和刪除,棧的插入和刪除只允許在棧頂,棧的插入和刪除通常稱為進棧和出棧。資料集合 每個資料元素的資料型別可以是任意的型別 操作的集合 進棧...

演算法系列 Move Zeroes

given an array nums,write a function to move all 0 s to the end of it while maintaining the relative order of the non zero elements.for example,given ...

演算法系列 Reverse Integer

reverse digits of an integer.example1 x 123,return 321 example2 x 123,return 321 note the input is assumed to be a 32 bit signed integer.your function...