Leetcode面试经典150题(一)

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];
         }
     }
 }
相关推荐
阿豪学编程13 分钟前
LeetCode724.:寻找数组的中心下标
算法·leetcode
墨韵流芳29 分钟前
CCF-CSP第41次认证第三题——进程通信
c++·人工智能·算法·机器学习·csp·ccf
csdn_aspnet1 小时前
C# 求n边凸多边形的对角线数量(Find number of diagonals in n sided convex polygon)
开发语言·算法·c#
禹中一只鱼1 小时前
【力扣热题100学习笔记】 - 哈希
java·学习·leetcode·哈希算法
凌波粒1 小时前
LeetCode--349.两个数组的交集(哈希表)
java·算法·leetcode·散列表
paeamecium3 小时前
【PAT甲级真题】- Student List for Course (25)
数据结构·c++·算法·list·pat考试
Book思议-3 小时前
【数据结构】栈与队列全方位对比 + C 语言完整实现
c语言·数据结构·算法··队列
SteveSenna3 小时前
项目:Trossen Arm MuJoCo
人工智能·学习·算法
NAGNIP3 小时前
一文搞懂CNN经典架构-DenseNet!
算法·面试
道法自然|~3 小时前
BugCTF黄道十二宫
算法·密码学