演算法系列 Divide Two Integers

2021-08-05 21:45:27 字數 588 閱讀 8419

divide two integers without using multiplication, division and mod operator.

if it is overflow, return max_int.

題目要求我們不使用乘法,除法和取模運算 實現兩個數的除法。

一種可能的方法,可以利用減法,不斷從被減數中減去減數,值到被減數小於等於0,統計減法操作的次數。這種方法的時間複雜度為o(n).我們嘗試加速這個過程。對減數b做累加操作,得到2b,4b,8b,16b…2的n次方b.知道大於被減數a 此時的有k個b。然後從 a中減去 k*b.重複上述過程,累加k。最後得到結果。

此時的時間複雜度為o(logn)

public

class

solution

res+=count;

a-=temp;

}if(negative)

res=0-res;

if(res>integer.max_value)

res=integer.max_value;

return (int)res;

}}

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 ...

演算法系列 Missing Number

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...