27. 移除元素
java
public int removeElement(int[] nums, int val) {
int res = 0;
for (int i = 0; i < nums.length; i++) {
if(nums[i]!=val){
nums[res++] = nums[i];
}
}
return res;
}
26. 删除有序数组中的重复项
java
class Solution {
public int removeDuplicates(int[] nums) {
int j = 0;
for(int i=0;i<nums.length;i++){
if(nums[i]!=nums[j]){
nums[++j] = nums[i];
}
}
return j+1;
}
}
80. 删除有序数组中的重复项 II
java
class Solution {
public int removeDuplicates(int[] nums) {
int n = 2;
int i = n,j=n;
while(j<nums.length){
if(nums[j]!=nums[i-n]){
nums[i++] = nums[j];
}
j++;
}
return i;
}
}
11. 盛最多水的容器
java
class Solution {
public int maxArea(int[] height) {
int l = 0,r = height.length-1;
int res = 0;
while(l<r){
int t = Math.min(height[l],height[r]) * (r-l);
res = Math.max(res,t);
if(height[l]<=height[r]) {
l++;
}else{
r--;
}
}
return res;
}
}
189. 轮转数组
java
class Solution {
public void rotate(int[] nums, int k) {
int[] arr = new int[nums.length];
for(int i=0;i<nums.length;i++){
arr[(i+k)%nums.length] = nums[i];
}
for (int i = 0; i < nums.length; i++) {
nums[i] = arr[i];
}
}
}