Why not use indexOf()?


  • 2
    E

    There's no limit to use indexOf, so Why not use indexOf()?

    public int strStr(String haystack, String needle) {
            return haystack.indexOf(needle, 0);
        }
    

  • 0

    @ermao Of course you can use built-in functions, but would you expect the interviewer want you to use it and ask nothing more? Ask yourself is this what you really want from the Leetcode, cheating yourself? Come on, man!


  • 1
    J

    However, I don't think everybody knows to use built-in func at first time, this solution is definitely a good way to figure out this problem.


  • 0

    @jumpdewey Then you must think everyone else is just fool...of course this is the most straightforward solution in the very first place...


  • 1
    J

    @LHearen of course your reputation said you are a good coder, and I'm a freshman. :) BUT NOT A FOOL


  • 0

    @jumpdewey You're getting little bit carried away on this, man. Sorry to be so aggressive before, under lots of pressure these days.

    I just wanted to say that we are doing algorithm so we should pay more attention to the details, the underlying implementations instead of the built-in functions which can be a very good reference, but now we should be the implementator instead of the library builder.

    Enjoy yourself! Sorry about my rudeness.


  • 1

    I'd like to point this out more clearly:

    Why not use indexOf()?

    Because "LeetCode OJ is a platform for preparing technical coding interviews" and you're not exactly going to impress your interviewers with that. Instead they'll be more like "WTF, are you stupid? Obviously I want to see whether and how you can implement indexOf".


  • 3
    C

    well, just use it


  • 1

    As far as I know String indexOf() method does the same thing as described in the problem, and I believe the main point of the problem would be to implement in a lower level without any built-in method.

    But why was my code still accepted? :(

    beating 96.90% of javasubmissions, 0ms

    public class Solution { public int strStr(String haystack, String needle) { return haystack.indexOf(needle); }}
    

    https://leetcode.com/submissions/detail/69394135/

    In the interview could I write two solutions? The first one with indexOf() while implementing the strStr without indexOf() in the second one, such that would this impress the interviewer of my familiarity of this method, what's more, of the ability to implement it as well?

    Anyway, I am going to solve it without indexOf() later :)


  • 2

    @BryanBo.Cao Anyway, accept is justice :)


  • 0

    @Zoey_Yue Leetcode High!


  • 0
    C

    @StefanPochmann So in this kind of question, we should not use indexOf(). When I see some solutions to these questions, some used subString(), is it also a build-in method? So I don't what kind of build-in method we can use to do this kind of questions.


Log in to reply
 

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