feat: 双指针
This commit is contained in:
24
26.remove-duplicates-from-sorted-array.java
Normal file
24
26.remove-duplicates-from-sorted-array.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=26 lang=java
|
||||||
|
*
|
||||||
|
* [26] Remove Duplicates from Sorted Array
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
class Solution {
|
||||||
|
public int removeDuplicates(int[] nums) {
|
||||||
|
if(nums.length == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int i = 0;
|
||||||
|
for(int j = 0; j< nums.length; j++) {
|
||||||
|
if(nums[j] != nums[i]) {
|
||||||
|
i++;
|
||||||
|
nums[i] = nums[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
21
27.remove-element.java
Normal file
21
27.remove-element.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=27 lang=java
|
||||||
|
*
|
||||||
|
* [27] Remove Element
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
class Solution {
|
||||||
|
public int removeElement(int[] nums, int val) {
|
||||||
|
int i = 0;
|
||||||
|
for(int j = 0; j < nums.length; j++) {
|
||||||
|
if(nums[j] != val) {
|
||||||
|
nums[i] = nums[j];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
24
28.find-the-index-of-the-first-occurrence-in-a-string.java
Normal file
24
28.find-the-index-of-the-first-occurrence-in-a-string.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=28 lang=java
|
||||||
|
*
|
||||||
|
* [28] Find the Index of the First Occurrence in a String
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
class Solution {
|
||||||
|
public int strStr(String haystack, String needle) {
|
||||||
|
int idx = -1;
|
||||||
|
if(haystack.length() < needle.length()) {
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
for(int i = 0; i < haystack.length(); i++) {
|
||||||
|
if(i + needle.length() <= haystack.length() && haystack.substring(i, i + needle.length()).equals(needle)) {
|
||||||
|
idx = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
26
80.remove-duplicates-from-sorted-array-ii.java
Normal file
26
80.remove-duplicates-from-sorted-array-ii.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=80 lang=java
|
||||||
|
*
|
||||||
|
* [80] Remove Duplicates from Sorted Array II
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
class Solution {
|
||||||
|
public int removeDuplicates(int[] nums) {
|
||||||
|
int i = -1, freq = 0;
|
||||||
|
for(int j = 0; j< nums.length; j++) {
|
||||||
|
if(i<0 || nums[i] == nums[j] && freq < 2) {
|
||||||
|
i++;
|
||||||
|
nums[i] = nums[j];
|
||||||
|
freq++;
|
||||||
|
} else if(nums[i] != nums[j]) {
|
||||||
|
freq = 1;
|
||||||
|
i++;
|
||||||
|
nums[i] = nums[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
Reference in New Issue
Block a user