>

Leetcode - Range Sum Query - Immutable

- 编辑:正版管家婆马报彩图 -

Leetcode - Range Sum Query - Immutable

图片 1Screenshot from 2016-02-27 21:06:26.png

图片 2

My code:

My code:

public class NumArray { private int[] sums; private int[] nums; public NumArray(int[] nums) { this.nums = nums; this.sums = new int[nums.length]; for (int i = 0; i < nums.length; i++) sums[i] = nums[i]; for (int i = 1; i < sums.length; i++) sums[i] += sums[i - 1]; } public int sumRange(int i, int j) { if (i < 0 || i >= sums.length) return 0; else if (j < 0 || j >= sums.length) return 0; return nums[i] + sums[j] - sums[i]; }}// Your NumArray object will be instantiated and called as such:// NumArray numArray = new NumArray;// numArray.sumRange;// numArray.sumRange;
public class Solution { public void sortColors(int[] nums) { if (nums == null || nums.length == 0) return; int head = 0; int tail = nums.length - 1; for (int i = 0; i < nums.length; i++) { if (i > tail) return; if (nums[i] == 2) { int j = tail; while (j > i && nums[j] == 2) j--; if  return; nums[i] = nums[j]; nums[j] = 2; tail = j - 1; i--; } else if (nums[i] == 0) { nums[i] = 1; nums[head] = 0; head++; } else continue; } }}

同贰个思路。Anyway, Good luck, Richardo!

My test result:

My code:

图片 3Paste_Image.png

public class NumArray { public NumArray(int[] nums) { } public int sumRange(int i, int j) { }}// Your NumArray object will be instantiated and called as such:// NumArray numArray = new NumArray;// numArray.sumRange;// numArray.sumRange;

那道标题大概相比轻易的。具体看笔者的图腾剖析。

简易题,没什么好说的。

图片 4

Anyway, Good luck, Richardo! -- 08/20/2016

因而设置头尾指针,来将三种颜色分类。0 的话往前移,2的以后移。1的一直跳过。

**总结: Array, Two pointer**

Anyway, Good luck, Richardo!

My code:

public class Solution { public void sortColors(int[] nums) { if (nums == null || nums.length == 0) return; int i = 0; int j = nums.length - 1; int scan = 0; while (scan <= j) { if (nums[scan] == 0) { nums[scan] = nums[i]; nums[i] = 0; i++; scan++; } else if (nums[scan] == 1) { scan++; } else { nums[scan] = nums[j]; nums[j] = 2; j--; } } }}

感到笔者第二遍写的代码比第三回简单比很多哟。正是遇上1就往前走。借使超越了0,就和i换,然后i++,scan++若是境遇了2,就和j换,j--

Anyway, Good luck, Richardo!

My code:

public class Solution { public void sortColors(int[] nums) { if (nums == null || nums.length == 0) { return; } int begin = 0; int i = 0; int end = nums.length - 1; while (i <= end) { if (nums[i] == 0) { nums[i] = nums[begin]; nums[begin] = 0; begin++; i++; } else if (nums[i] == 1) { i++; } else { nums[i] = nums[end]; nums[end] = 2; end--; } } }}

跟飞速排序+思虑重新成分的算法基本上,四个指针消除。

Anyway, Good luck, Richardo! -- 08/07/2016

看来了一种全新的解法,并且并非swapMy code:

public class Solution { public void sortColors(int[] nums) { if (nums == null || nums.length == 0) { return; } int red = 0; int white = 0; for (int i = 0; i < nums.length; i++) { int temp = nums[i]; nums[i] = 2; if (temp < 2) { nums[white] = 1; white++; } if (temp == 0) { nums[red] = 0; red++; } } }}

Anyway, Good luck, Richardo! -- 09/04/2016

本文由编程应用发布,转载请注明来源:Leetcode - Range Sum Query - Immutable