国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Lintcode: Subarray Sum

2019-11-14 23:39:09
字體:
來源:轉載
供稿:網友
Lintcode: Subarray Sum
Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.ExampleGiven [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].

推薦解法:The idea is based on the PRefix sum: Iterate through the array and for every element array【i】, calculate sum of elements form 0 to i (this can simply be done as sum += arr【i】). If the current sum has been seen before, then there is a zero sum array, the start and end index are returned.

用HashMap: O(N)時間,但是more memory, 大case會MLE

 1 public class Solution { 2     /** 3      * @param nums: A list of integers 4      * @return: A list of integers includes the index of the first number  5      *          and the index of the last number 6      */ 7     public ArrayList<Integer> subarraySum(int[] nums) { 8         // write your code here 9         ArrayList<Integer> res = new ArrayList<Integer>();10         if (nums==null || nums.length==0) return res;11         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();12         map.put(0, -1);13         int sum = 0;14         for (int i=0; i<nums.length; i++) {15             sum += nums[i];16             if (!map.containsKey(sum)) {17                 map.put(sum, i);18             }19             else {20                 res.add(map.get(sum)+1);21                 res.add(i);22                 return res;23             }24         }25         return res;26     }27 }

因為上面這個簡潔的代碼會MLE,所以(nlog(n))第二個算法,時間多一點,但是空間少一點

 1 class Element implements Comparable<Element>{ 2     int index; 3     int value; 4     public Element(int i, int v){ 5         index = i; 6         value = v; 7     } 8     public int compareTo(Element other){ 9         return this.value-other.value;10     }11     public int getIndex(){12         return index;13     }14     public int getValue(){15         return value;16     }17 }18 19 public class Solution {20     /**21      * @param nums: A list of integers22      * @return: A list of integers includes the index of the first number23      *          and the index of the last number24      */25     public ArrayList<Integer> subarraySum(int[] nums) {26         ArrayList<Integer> res = new ArrayList<Integer>();27         if (nums==null || nums.length==0) return res;28         int len = nums.length;29         Element[] sums = new Element[len+1];30         sums[0] = new Element(-1,0);31         int sum = 0;32         for (int i=0;i<len;i++){33             sum += nums[i];34             sums[i+1] = new Element(i,sum);35         }36         Arrays.sort(sums);37         for (int i=0;i<len;i++)38             if (sums[i].getValue()==sums[i+1].getValue()){39                 int start = Math.min(sums[i].getIndex(),sums[i+1].getIndex())+1;40                 int end = Math.max(sums[i].getIndex(),sums[i+1].getIndex());41                 res.add(start);42                 res.add(end);43                 return res;44             }45 46         return res;47     }48 }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 三原县| 霍山县| 永靖县| 日土县| 辉县市| 兴隆县| 祁东县| 集贤县| 门源| 遂川县| 奉节县| 五指山市| 喀什市| 贵德县| 新丰县| 诏安县| 西乌珠穆沁旗| 四子王旗| 马山县| 阿尔山市| 永胜县| 金乡县| 海宁市| 青川县| 怀安县| 合肥市| 新巴尔虎左旗| 丹棱县| 江达县| 梅河口市| 嘉定区| 临沂市| 奉化市| 探索| 大英县| 柳河县| 西峡县| 林周县| 隆尧县| 襄垣县| 罗城|