commit 8edb2b98142efeeac4ff2eb494bcf511c46b145a parent 7e1876d18448940299ef0fe72132af8a030f1fea Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Wed, 19 Apr 2023 13:48:54 -0500 Square root problem using dart Diffstat:
| A | sqrtx/sqrtx.dart | | | 25 | +++++++++++++++++++++++++ |
1 file changed, 25 insertions(+), 0 deletions(-)
diff --git a/sqrtx/sqrtx.dart b/sqrtx/sqrtx.dart @@ -0,0 +1,25 @@ +//Find the square root without the builtin sqrt() function. +//This is done by checking if the value selected squared is greater than +//the val if it is then you subtract one and return the value. This is +//done because leetcode wants the value that is the floor so if you have 2.9 that would need to return 2. +//Time complexity = O(sqrt(n)) +//Time: 317ms Beats: 70.83% +//Memory: 143.2MB Beats: 36.11% + +void main(){ + Solution sol = new Solution(); + print(sol.mySqrt(226332020323023)); +} + + +class Solution { + int mySqrt(int x) { + int count = 0; + while(true){ + if(x < count * count){ + return count-1; + } + count += 1; + } + } +}