leetcode解题之28 #Implement strStr() Java版

    xiaoxiao2021-03-25  146

    28 Implement strStr().

    Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

    解:注意发生不匹配时重写匹配的起始位置。

    public int removeElement(int[] nums, int val) { if (nums.length == 0 || nums == null) return 0; int i = 0; int j = 0; //用j 来标记新数组 while (i < nums.length) { if (nums[i] == val) { i++; }else{ nums[j]=nums[i]; j++; i++; } } return j; }

    转载请注明原文地址: https://ju.6miu.com/read-6417.html

    最新回复(0)