Java solution, space complexity o(1)


  • -6
    L

    public class Solution {
    public int findDuplicate(int[] nums) {
    int length = nums.length;
    int temp=0;
    boolean out = false;
    for(int i=0;i<length-1;i++){
    temp = nums[i];
    for(int j=i+1;j<length;j++){
    if(temp==nums[j]){out=true;break;}
    }
    if(out){break;}
    }
    return temp;
    }
    }


  • 0
    S

    this is O(n^2) solution


  • 0
    L

    the time complexity is O(n^2) but the space complexity is o(1)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.