41. First Missing Positive

思路是把1放在数组第一个位置 nums[0],2放在第二个位置 nums[1],即需要把 nums[i] 放在 nums[nums[i] - 1]上,遍历整个数组,如果 nums[i] != i + 1, 而 nums[i] 为整数且不大于n,另外 nums[i] 不等于 nums[nums[i] - 1] 的话,将两者位置调换,如果不满足上述条件直接跳过,最后再遍历一遍数组,如果对应位置上的数不正确则返回正确的数

class Solution {
public int firstMissingPositive(int[] nums) {
int n = nums.length;
for(int i = 0; i < n; i++){
while(nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] != nums[i]){
swap(nums, nums[i] - 1, i);
}
}
for(int i = 0; i < n; i++){
if(nums[i] != i + 1) return i + 1;
}
return n + 1;
} public void swap(int[] a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

134. Gas Station

如果当前起点油量小于0,说明从0开始的任何一个点都不能作为起点,此时start + 1。

最后如果total总油量小于等于0,则说明不满足条件。

class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
if(gas == null || cost == null || gas.length == 0 || cost.length == 0) return -1; int total = 0;
int cur = 0;
int start = 0; for(int i = 0; i < cost.length; i++){
cur += gas[i] - cost[i]; if(cur < 0){
start = i + 1;
cur = 0;
} total += gas[i] - cost[i];
}
return total >= 0 ? start : -1;
}
}

<Array> 41 134的更多相关文章

  1. jdbcTemplate 调用存储过程。 入参 array 返回 cursor

    注:本文来源<   jdbcTemplate 调用存储过程. 入参 array 返回 cursor   > 需求: java传入一个list object.从数据库找到相关的数据并返回. ...

  2. 微信支付生成带logo的二维码

    利用到一个qrcode类 比较简洁 原作者没有加入二维码嵌入logo的功能 在这里我进行了小小的修改 可以实现生成微信支付二维码时打上logo 生成png格式的利用到该类中的png方法(我已经改好了) ...

  3. PHP生成二维码,PHPQRCode

    声明一个方法,直接调用即可 <?php /** * 功能:生成二维码 * @param string $qr_data 手机扫描后要跳转的网址 * @param string $qr_level ...

  4. Linux C程序操作Mysql 调用PHP采集淘宝商品 (转)

    还是继续这个项目. 在上一篇Linux下利用Shell使PHP并发采集淘宝产品中,采用shell将对PHP的调用推到后台执行,模拟多线程. 此方法有一致命缺点,只能人工预判每个程序执行时间.如果判断时 ...

  5. PHP qrcode 生成二维码

    <?php /* 下载地址 : https://sourceforge.net/projects/phpqrcode/ 这里下载的文件名为 phpqrcode-2010100721_1.1.4 ...

  6. LeetCode分类-前400题

    1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...

  7. 解决在页面中无法获取qrcode.js生成的base64的图片

    应用场景 生成带二维码的推广海报图片 旧方法: 将用户自己的推广连接先通过qrcode.js生成二维码,然后再用后台返回的一张背景图片和二维码通过canvas绘制成一张海报. 问题 在部分安卓手机上获 ...

  8. phpqrcode.php 生成二维码图片用于推广

    <?php /* * PHP QR Code encoder * * This file contains MERGED version of PHP QR Code library. * It ...

  9. ThinkPHP5生成二维码图片与另一张背景图片进行合成

    1.PHP方法 public function do_qrcode(){ Vendor('Qrcode.phpqrcode'); Vendor('Qrcode.Compress'); $object ...

随机推荐

  1. 【新特性速递】树表格结构由单层 TR 改为 TR-TD-TABLE 层级嵌套!

    由于历史原因,在之前实现树表格时,我们有点偷懒,本来应该是层级嵌套的树结构,被我们硬生生的拉平了,请看: 可以看到,basic目录的子节点和basic是在同一级别的,因为此目录尚未展开,所以这些子节点 ...

  2. DWR日志 在log4j.xml配置

    一.日志 DWR依赖 Apache Commons Logging,可以使用log4j实现日志记录功能. 1.1 日志简介 和其他日志框架一样,当设置低等级的日志时所有高于此等级的日志也将会打印出来. ...

  3. Leetcode练习题 7. Reverse Integer

    7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...

  4. 转:对softmax讲解比较清楚的博客

    https://blog.csdn.net/wgj99991111/article/details/83586508

  5. 微软宣布成立.NET基金会全面支持开源项目 包括C#编译器Roslyn【转】

    基金会初始董事包括 Mono 项目和 Xamarin 的老大 Miguel de Icaza,微软 .NET 团队代表和微软开放技术公司(这是微软专门为开源和开放技术.标准化成立的独立公司)代表. 首 ...

  6. 联合 CNCF 共同出品:Kubernetes and Cloud Native Meetup 成都站

    亮点解读 云原生前沿技术分享:阿里经济体“云原生化”宝贵经验与最佳实践成果 OpenKruise 价值几何? 防踩坑指南:国内知名容器平台架构师解读从 ECS 迁移到 K8S 走过哪些坑. ​云原生服 ...

  7. Installing on Kubernetes with NATS Operator

    https://github.com/nats-io/nats-operator https://hub.helm.sh/charts/bitnami/nats https://github.com/ ...

  8. dedecms5.7的获取本文章的TAG

    tag调用标签如下: {dede:tag row='10' getall='1' sort='month'} <li><a href='[field:link/]'>[fiel ...

  9. SQL Server中INSERT EXEC语句不能嵌套使用(转载)

    问: I have three stored procedures Sp1, Sp2 and Sp3.The first one (Sp1) will execute the second one ( ...

  10. Ubuntu18.04 安装 Mysql 5.7 问题

    在安装完Mysql5.7后 没有让你输入的密码的时候,便会生成一个默认的密码. 生成的密码在debian.cnf 文件中 记住用户名和密码.然后去登陆 mysql -udebian-sys-maint ...