Python naive solution

  • 0

    Naive way is to go through the provided list twice.
    First, we iterate based on index and take that value in that index, move to that area and replace by negative number.
    Repeat until reach to the last element in the list.
    Remember not to negatively change if the value is already negative.
    We go to the list again and push only positive value index to returned list.

    t = 0
    for i in range(len(nums)):
    if nums[i] > 0:
    t = nums[i]
    t = nums[i] * (-1)
    if nums[t-1] > 0:
    nums[t-1] *= (-1)
    temp = []
    for j in range(len(nums)):
    if nums[j] > 0:
    return temp

Log in to reply

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