JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  174 随笔 :: 0 文章 :: 0 评论 :: 30092 阅读

只有一个答案,找到最后一个不可以的点,下一个是开始点

复制代码
 1 public class Solution {
 2     public int canCompleteCircuit(int[] gas, int[] cost) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         int total = 0;
 6         int sum = 0;
 7         int startindex = -1;
 8         for(int i = 0; i < gas.length; i++)
 9         {
10             total += (gas[i] - cost[i]);
11             sum += (gas[i] - cost[i]);
12             if(sum < 0)
13             {
14                 sum = 0;
15                 startindex = i;
16             }
17         }
18         return total < 0? -1:startindex + 1;
19     }
20 }
复制代码

 

posted on   JasonChang  阅读(266)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
点击右上角即可分享
微信分享提示