leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

sqrtx.dart (684B)


      1 //Find the square root without the builtin sqrt() function.
      2 //This is done by checking if the value selected squared is greater than
      3 //the val if it is then you subtract one and return the value. This is
      4 //done because leetcode wants the value that is the floor so if you have 2.9 that would need to return 2.
      5 //Time complexity = O(sqrt(n))
      6 //Time: 317ms Beats: 70.83%
      7 //Memory: 143.2MB Beats: 36.11%
      8 
      9 void main(){
     10     Solution sol = new Solution();
     11     print(sol.mySqrt(226332020323023));
     12 }
     13 
     14 
     15 class Solution {
     16   int mySqrt(int x) {
     17       int count = 0;
     18       while(true){
     19           if(x < count * count){
     20               return count-1;
     21           }
     22           count += 1;
     23       }
     24   }
     25 }