Is my solution "cheating"? It seems to be the most pythonic way to do this.


  • 0
    D

    Here is my solution. I think that this is better in practical application, instead of trying to implement a manual binary search.

    class Solution:
    # @param {integer[]} nums
    # @param {integer} target
    # @return {integer}
    def search(self, nums, target):
        try:
            return nums.index(target)
        except ValueError:
            return -1

  • 1

    If that's your answer in an interview (which is what this site is about), don't be surprised to hear "Uh... ok... goodbye." :-P

    Whoever caused the list to be "rotated sorted" probably did so at some cost and for some reason. Probably so that searching could be fast. So you then ignoring that preparation and not doing it fast is just bad.


  • 0
    Z

    You think in practical application this is a "hard" problem that need to put it online for discussion? You think that the interviewer asking you this question is because he really does not know that python (even any other languages) already has such an function to use? Come on buddy, I think we all know why the question is here and please stop using the "practical solution" as an excuse.


  • 0
    P
    class Solution:
    # @param {integer[]} nums
    # @param {integer} target
    # @return {integer}
    def search(self, nums, target):
        for i, n in enumerate(nums):
            if n == target:
                return i
        return -1
    

    Another cheating solution.which passed in 52ms, though it makes no sense either.
    Maybe this represents that the test cases are not strong enough.


Log in to reply
 

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