admin管理员组

文章数量:1636929

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;
	}


本文标签: LeetCodeimplementJavastrStr