Leetcode [724, 747,66] Array专题 (1)

亚麻 OA 2019 题目 Most Common Word Prison Cells After N Days K Closest Points to Origin Reorder Log Files Partition Labels Min Cost to Add New Roads Min distance to remove the obstacle Roll Dice Min Cost to Con... 阅读详情

Operations in Array

// "static void main" must be defined in a public class.
public class Main {
    public static void main(String[] args) {
        // 1. Initialize
        int[] a0 = new int[5];
        int[] a1 = {1, 2, 3};
        // 2. Get Length
        System.out.println("The size of a1 is: " + a1.length);
        // 3. Access Element
        System.out.println("The first element is: " + a1[0]);
        // 4. Iterate all Elements
        System.out.print("[Version 1] The contents of a1 are:");
        for (int i = 0; i < a1.length; ++i) {
            System.out.print(" " + a1[i]);
        }
        System.out.println();
        System.out.print("[Version 2] The contents of a1 are:");
        for (int item: a1) {
            System.out.print(" " + item);
        }
        System.out.println();
        // 5. Modify Element
        a1[0] = 4;
        // 6. Sort
        Arrays.sort(a1);
    }
}
// "static void main" must be defined in a public class.
public class Main {
    public static void main(String[] args) {
        // 1. initialize
        List<Integer> v0 = new ArrayList<>();
        List<Integer> v1;                           // v1 == null
        // 2. cast an array to a vector
        Integer[] a = {0, 1, 2, 3, 4};
        v1 = new ArrayList<>(Arrays.asList(a));
        // 3. make a copy
        List<Integer> v2 = v1;                      // another reference to v1
        List<Integer> v3 = new ArrayList<>(v1);     // make an actual copy of v1
        // 3. get length
        System.out.println("The size of v1 is: " + v1.size());
        // 4. access element
        System.out.println("The first element in v1 is: " + v1.get(0));
        // 5. iterate the vector
        System.out.print("[Version 1] The contents of v1 are:");
        for (int i = 0; i < v1.size(); ++i) {
            System.out.print(" " + v1.get(i));
        }
        System.out.println();
        System.out.print("[Version 2] The contents of v1 are:");
        for (int item : v1) {
            System.out.print(" " + item);
        }
        System.out.println();
        // 6. modify element
        v2.set(0, 5);       // modify v2 will actually modify v1
        System.out.println("The first element in v1 is: " + v1.get(0));
        v3.set(0, -1);
        System.out.println("The first element in v1 is: " + v1.get(0));
        // 7. sort
        Collections.sort(v1);
        // 8. add new element at the end of the vector
        v1.add(-1);
        v1.add(1, 6);
        // 9. delete the last element
        v1.remove(v1.size() - 1);
    }
}

 

724 Find Pivot Index

Given an array of integers nums, write a method that returns the "pivot" index of this array.

We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.

If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.

Example 1:

Input: 
nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation: 
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
Also, 3 is the first index where this occurs.

Example 2:

Input: 
nums = [1, 2, 3]
Output: -1
Explanation: 
There is no index that satisfies the conditions in the problem statement.

Note:

  • The length of nums will be in the range [0, 10000].
  • Each element nums[i] will be an integer in the range [-1000, 1000].
class Solution {
    public int pivotIndex(int[] nums) {
        int len = nums.length;
        for(int i=1;i<len-1;i++){
            int leftSum = 0;
            int rightSum = 0;
            for(int j=0; j<i;j++){
                leftSum+=nums[j];
            }
            for(int k=i+1;k<len;k++){
                rightSum+=nums[k];        
            }
            if(leftSum == rightSum){
                    return i;
            }
        }
        return -1;
    }
}
//更简洁的版本
class Solution {
    public int pivotIndex(int[] nums) {
        int sum = 0, leftsum = 0;
        for (int x: nums) sum += x;
        for (int i = 0; i < nums.length; ++i) {
            if (leftsum == sum - leftsum - nums[i]) return i;
            leftsum += nums[i];
        }
        return -1;
    }
}

Complexity Analysis

  • Time Complexity: O(N), where NN is the length of nums.

  • Space Complexity: O(1), the space used by leftsum and S.

747 Largest Number At Least Twice of Others

In a given integer array nums, there is always exactly one largest element.

Find whether the largest element in the array is at least twice as much as every other number in the array.

If it is, return the index of the largest element, otherwise return -1.

Example 1:

Input: nums = [3, 6, 1, 0]
Output: 1
Explanation: 6 is the largest integer, and for every other number in the array x,
6 is more than twice as big as x.  The index of value 6 is 1, so we return 1.

Example 2:

Input: nums = [1, 2, 3, 4]
Output: -1
Explanation: 4 isn't at least as big as twice the value of 3, so we return -1.

Note:

  1. nums will have a length in the range [1, 50].
  2. Every nums[i] will be an integer in the range [0, 99].

 

class Solution {
    public int dominantIndex(int[] nums) {
        int max = -1, index = -1, second = -1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > max) {
                second = max;
                max = nums[i];
                index = i;
            } else if (nums[i] > second)
                second = nums[i];
        }
        if(second*2 <=max)
            return index;
        else
            return -1;
    }
}

Complexity Analysis

  • Time Complexity: O(N) where N is the length of nums.

  • Space Complexity: O(1), the space used by our int variables.

66 Plus One

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
class Solution {
    public int[] plusOne(int[] digits) {
    for (int i = digits.length - 1; i >=0; i--) {
        if (digits[i] != 9) {
            digits[i]++;
            break;
        } else {
            digits[i] = 0;
        }
    }
    if (digits[0] == 0) {
        int[] res = new int[digits.length+1];
        res[0] = 1;
        return res;
    }
    return digits;
}
}

Complexity Analysis

  • Time Complexity: O(N) where N is the length of digits.

  • Space Complexity: O(1), the space used by our int variables.

 

Leetcode 250. Count Univalue Subtrees 谷歌面试, Java解法详解 这是一道谷歌会考的题目 Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. Example : Input: root = [5,1,5,5,5,null,5] ... 阅读详情

相关推荐

双闭环PWM整流器VOC控制 +SVPWM调制仿真(Simulink仿真实现)

内容概要:本文围绕城市空中交通环境下多无人机协同路径规划问题展开深入研究,重点探讨了在包含建筑物、禁飞区、动态威胁等复杂约束条件下,如何利用仿生优化算法(如布谷鲶鱼优化算法CCO、灰雁优化算法GGO、田忌赛马优化算法THRO)实现安全、高效的多无人机路径规划。文章提出了一种分层规划策略,融合全局路径规划与局部动态避障机制,构建了综合考虑路径长度、威胁暴露度、高度变化及地形碰撞风险的多目标优化模型。通过Matlab仿真平台对多种算法进行对比实验,系统评估了各算法在路径安全性、收敛速度、优化性能和鲁棒性方面的表现,并深入分析了现有方法的局限性,对未来研究方向如动态环境适应、通信受限下的分布式协同等进行了展望。; 适合人群:具备一定编程基础,熟悉Matlab/Simulink仿真环境,从事无人机系统设计、智能交通、自动化控制、路径规划算法研究等相关领域的科研人员及高校研究生。; 使用场景及目标:①应用于城市空中交通系统中多无人机的协同任务执行与动态避障;②为复杂威胁环境下多智能体系统的路径规划提供先进算法支持与仿真验证框架;③辅助研究人员深入理解并复现仿生优化算法在高维、多约束路径规划问题中的具体实现与性能差异。; 阅读建议:此资源以Matlab代码实现为核心,紧密结合理论建模与仿真实验,建议读者在学习过程中重点关注算法的设计原理、参数调优策略及仿真结果的可视化分析,结合文中提供的实验数据进行横向对比,以深刻把握不同仿生算法的搜索机制、适用场景及其在实际工程应用中的潜力与挑战。

数据结构丨数组和字符串

数组简介 数组简介 数组是一种基本的数据结构,用于按顺序存储元素的集合。但是元素可以随机存取,因为数组中的每个元素都可以通过数组索引来识别。 数组可以有一个或多个维度。这里我们从一维数组开始,它也被称为线性数组。这里有一个例子: 在上面的例子中,数组 A 中有 6 个元素。也就是说,A 的长度是 6 。我们可以使用 A[0] 来表示数组中的第一个元素。因此,A[0] = 6 。类似地,A[1] ...

weixin_33724570的博客 353

仿真+课设Multisim直流可调稳压电源(0-15V)仿真设计基于LM317+LM337【仿真+课程报告】

内容概要:本文详细介绍了一种基于LM317(正压调节)和LM337(负压调节)芯片的Multisim直流可调稳压电源仿真设计方案,输出电压范围为0-15V,具备完整的电路仿真模型与课程设计报告。设计涵盖线性稳压电源的核心原理,包括电压调节机制、元件参数配置、双极性电源构建及仿真测试流程,突出理论与实践结合。文档还附带丰富的扩展资源,如三相PWM整流器、函数信号发生器等仿真项目,并提供百度网盘链接用于获取全套资料,适用于电子类课程设计与自主学习。整体内容系统性强,注重仿真操作指导与工程实践能力培养。; 适合人群:电子信息工程、自动化、电气工程及其相关专业的本科生、专科生,以及正在进行模拟电子技术课程设计或毕业设计的学生。; 使用场景及目标:①深入理解线性稳压电源的工作原理与设计方法;②掌握Multisim软件在电路仿真中的实际应用;③完成基于LM317/LM337的可调稳压电源课程设计任务;④提升模拟电路系统的设计、仿真与分析综合能力; 阅读建议:建议结合Multisim软件同步实践,按照文档电路图逐步搭建仿真模型,重点关注电压调节电路的实现细节与参数设置,并参考所提供的其他仿真资源拓展知识广度,强化工程思维训练。

Leetcode 1086. High Five Java 解法

先吐槽一下这个难度,咋也不能是easy啊,和其他easy的不在一个量级。 Given a list of scores of different students, return the average score of each student'stop five scoresinthe order of each student's id. Each entryitems[i]h...

YuanTheCoder的博客 1473

阻尼器磁流变阻尼器MR Damper非线性(阻尼、滞回)特性研究(Matlab代码实现)

内容概要:本文围绕磁流变阻尼器(MR Damper)的非线性特性开展深入研究,重点剖析其在动态载荷作用下的阻尼力响应与滞回行为所表现出的强非线性特征,并基于Matlab平台进行系统建模与数值仿真。通过建立能够准确描述MR阻尼器非线性力学行为的数学模型,模拟其在不同激励条件、电流强度和运动频率下的动态响应过程,进而对系统的非线性特性进行定性与定量分析。研究涵盖了模型构建、参数设定、仿真执行与结果可视化等全过程,突出了Matlab在处理复杂非线性系统仿真中的强大功能与工程实用性,为智能材料器件在振动控制领域的应用提供了坚实的理论支撑与技术路径。; 适合人群:具备一定控制理论、机械动力学或智能材料基础知识,熟悉Matlab编程环境的研究生、科研人员及从事振动控制、结构工程等相关领域的工程技术人员。; 使用场景及目标:①用于智能材料与结构领域的科研与教学,帮助深入理解磁流变阻尼器的工作机理与非线性动力学特性;②为车辆半主动悬架系统、建筑结构抗震减震、桥梁风振抑制等实际工程中的振动控制系统设计、仿真验证与参数优化提供技术支持;③作为非线性系统建模、动力学仿真与Matlab编程实践的经典案例,有效提升使用者的系统分析能力和工程软件应用水平。; 阅读建议:建议读者结合文中提供的Matlab代码进行动手实践,通过调整模型参数(如电流、激励幅值与频率)观察系统响应的变化规律,深入探究非线性特性的形成原因与影响因素,并可尝试将该建模方法迁移至其他智能阻尼器件(如压电、电流变阻尼器)的研究中,以拓展应用视野。

全桥LLC谐振(变频+移相混合控制)变换器闭环仿真模型(Simulink仿真实现)

内容概要:本文档详细介绍了一种基于Simulink实现的全桥LLC谐振变换器闭环仿真模型,采用变频与移相混合控制策略,重点研究系统的动态响应、稳态性能及高效软开关特性。内容涵盖系统建模、PI控制器设计、闭环反馈机制构建以及谐振参数的优化配置,全面展示了从理论分析到仿真验证的全过程。通过在不同输入电压与负载条件下进行仿真测试,验证了该模型在提升转换效率、实现零电压开通(ZVS)和精确调控输出电压方面的优越性能。同时,配套提供参数设计报告、仿真说明文档及参考文献,有助于深入理解与复现实验结果。; 适合人群:具备电力电子与电力系统基础知识,熟悉Simulink/Matlab仿真环境,从事高频DC-DC变换器、新能源电源、车载充电机或相关领域研发工作的工程师及高校研究生。; 使用场景及目标:① 掌握全桥LLC谐振变换器的建模方法与闭环控制系统设计流程;② 深入理解变频+移相混合控制在高效率电源变换中的协同作用机制;③ 用于科研项目复现、教学案例演示或工业级电源产品的前期仿真预研;④ 为实际硬件原型开发提供可靠的理论支撑与关键参数设计依据。; 阅读建议:建议结合所提供的仿真文件与设计报告同步操作,重点关注控制环路搭建、谐振腔参数选取及其对系统性能的影响,推荐通过调整负载与输入条件进行多工况仿真分析,有条件者可进一步推进至样机实验验证。

Leetcode 1051. Height Checker, Java解法

Students are asked to stand in non-decreasing order of heights for an annual photo. Return the minimum number of students not standing in the right positions. (This is the number of students that mu...

YuanTheCoder的博客 649

Proxmox PVE 黑苹果安装全攻略:从镜像准备到系统配置

本文提供了一份在Proxmox PVE虚拟化平台上安装macOS(黑苹果)的详细指南。内容涵盖从获取OpenCore引导镜像和macOS安装镜像、宿主机环境检查与调优,到创建虚拟机、修改关键配置文件、解决引导问题以及安装后优化的全流程。重点在于通过正确的PVE配置和参数调整,成功在KVM虚拟机中运行macOS系统,为开发测试提供灵活环境。

opencv7vision的博客 1107

Leetcode 1108. Defanging an IP Address, Java解法

Given a valid (IPv4) IPaddress, return a defanged version of that IP address. AdefangedIP addressreplaces every period"."with"[.]". Example 1: Input: address = "1.1.1.1" Output: "1[.]1[....

YuanTheCoder的博客 574

关系代数表达式优化步骤

关系代数表达式优化步骤 本篇主要讲解怎么画查询语法树并对其优化,因为我在学关系代数的语法树的时候,在网上找不到比较详细的教法或者技巧,最后通过答案反推原理,所以想写一篇技巧来描述一下这类题的解题方法。 先上书内讲解 1、构造查询树 第一步:把用高级语言定义的查询转换为关系代数表达式 ★ 以 SELECT子向对应投影操作,以FROM字向对应笛卡尔积以 WHERE子句对应选择操作,生成原始查询树 ★ SQL语句转化为原始查询树 第二步:把关系代数表达式转换为查询树。 注: 查询树是一种表示关系代数表达式的树形结

m0_51718449的博客 7015

【软碟通 | UltraISO】刻录光盘教程(制作U盘启动盘)

前言 今天老师说办公室的电脑有点卡,让我给重新安装一个 Windows 7 系统。于是就用软碟通(UltraISO)在 U 盘上刻录了一个 Windows 7 镜像,然后给老师重装了系统。顺便把制作 U 盘启动盘的教程也分享给大家。 软件 UltraISO v9.7.1.3519:点击跳转(密码:dd2k) 教程 将 U 盘插到电脑上,然后鼠标右击选择格式化(制作启动盘必须格式化) 点击文件-打开 选择下载好的系统镜像(如:Windows 7) 点击启动-写入硬盘映像 选择硬盘

XavierJ的博客 2万+

Java中 Queue队列的用法及例子

1. Usage 用法 // "static void main" must be defined in a public class. public class Main { public static void main(String[] args) { // 1. Initialize a queue. Queue<Integer> q...

YuanTheCoder的博客 557

视觉SLAM十四讲--第13讲 实践:设计SLAM系统(最详细的代码调试运行步骤)

最详细的代码调试运行避坑说明,也包含了我自己对代码的理解,如有理解错误的地方,欢迎指正交流。

记录和分享 1万+

Leetcode 509. Fibonacci Number , Java 解法

TheFibonacci numbers, commonly denotedF(n)form a sequence, called theFibonacci sequence, such that each number is the sum of the two preceding ones, starting from0and1. That is, F(0) = 0, F(...

YuanTheCoder的博客 503

多天线技术:5G波束成形&空间复用&空时编码

以提升信号的可靠性。然而,在20世纪90年代中期,研究者们惊讶地发现,多天线技术不仅仅提供分集增益,还可以开启一些单天线系统无法实现的新机遇。,我们可以将信道矩阵 ( H ) 视为一个满秩矩阵,则理论上可以建立min(𝑁𝑇,𝑁𝑅)个独立的数据流,形成一个。波束成形是5G物理层的核心技术,其本质是利用多个天线来增强特定方向上的信号,同时抑制干扰方向的信号。:物理波束成形 = 🔦 类似手电筒的聚焦光束,虚拟波束成形 = 🌊 类似水波的相干叠加。随着 5G 和 6G 时代的到来,多天线技术的作用愈发重

weixin_43199439的博客 1389

[Dify] -进阶13- 使用“知识库 + 工作流”打造智能推荐系统

本文介绍了在Dify平台上构建智能推荐系统的方法,重点阐述了知识库与工作流技术的结合应用。通过知识库管理RAG流程,实现文档上传、向量嵌入和检索优化;利用工作流设计节点化推荐逻辑,支持并行处理与条件分支。以电话销售话术推荐为例,展示了从数据收集到实时生成的完整流程。该方法具有准确性高、扩展性强、维护简便等优势,可广泛应用于各类推荐场景,实现从数据存储到智能推荐的闭环解决方案。

技术小甜甜:传播可落地技术,帮助个人和企业降低解决真实场景问题的成本 1066

Leetcode 977. Squares of a Sorted Array, Java解法

Given an array of integersAsorted in non-decreasing order,return an array of the squares of each number,also in sorted non-decreasing order. Example 1: Input: [-4,-1,0,3,10] Output: [0,1,9,16,1...

YuanTheCoder的博客 497

热更新方案 HybridCLR 学习教程 |(一)原理及准备工作

前面文章介绍了热更新相关的知识,包括使用xLua实现热更新实战等 那本文就来介绍一下 热更新方案 HybridCLR(代号wolong,原huatuo)和基本使用方法。 若是想了解更多关于HybridCLR的东西也可以去 官网 详细查阅。

努力前行,总会成为自己心中的那道光 2万+

狗家 Online Assessment

License Key Formatting Unique Email Addresses Fruit Into Baskets Odd Even Jump Maximum Area Serving Cake Min Days to Bloom Fill Matrix Compare Strings Largest Subarray Length K Decreasing Sub...

YuanTheCoder的博客 470

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Leetcode 922. Sort Array By Parity II , Java解法

Given an arrayAof non-negative integers, half of the integers in A are odd, and half of the integers are even. Sort the array so that wheneverA[i]is odd,iis odd; and wheneverA[i]is even,iis...

YuanTheCoder的博客 444

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Leetcode [141, 142] Linked List Cycle,java解法

141 Linked List Cycle Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integerposwhich represents the position (0-indexed)in the l...

YuanTheCoder的博客 419

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Leetcode 1089. Duplicate Zeros, java解法 及Queue的使用

Given a fixed lengtharrayarrof integers, duplicate each occurrence of zero, shifting the remaining elements to the right. Note that elements beyond the length of the original array are not written...

YuanTheCoder的博客 417

Leetcode 1002. Find Common Characters, Java 解法

Given an arrayAof strings made only from lowercase letters, return a list of all characters that show up in all strings within the list(including duplicates).For example, if a character occurs 3 ...

YuanTheCoder的博客 385

Leetcode 1119. Remove Vowels from a String, Java解法

Given a stringS, remove the vowels'a','e','i','o', and'u'from it, and return the new string. Example 1: Input: "leetcodeisacommunityforcoders" Output: "ltcdscmmntyfrcdrs" Example 2: Inpu...

YuanTheCoder的博客 380

Leetcode 561. Array Partition I, Java解法

Given an array of2nintegers, your task is to group these integers intonpairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as poss...

YuanTheCoder的博客 368
上一篇: Leetcode 867. Transpose Matrix, Java解法
下一篇: Leetcode 1119. Remove Vowels from a String, Java解法
YuanTheCoder
博客等级 码龄9年 2粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值