Skip to content

Commit e45da7a

Browse files
committed
add: Single Number
1 parent 2c4372c commit e45da7a

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This is the solution collection of my LeetCode problems, most of them are progra
2222
|66|[Plus One](https://leetcode.com/problems/plus-one/) | [JavaScript](./src/plus-one/res.js)|Easy|
2323
|69|[Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [JavaScript](./src/sqrtx/res.js)|Easy|
2424
|73|[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [JavaScript](./src/set-matrix-zeroes/res.js)|Medium|
25+
|136|[Single Number](https://leetcode.com/problems/single-number/) | [JavaScript](./src/single-number/res.js)|Easy|
2526
|175|[Combine Two Tables](https://leetcode.com/problems/combine-two-tables/)| [SQL](./src/combine-two-tables/res.txt)|Easy|
2627
|176|[Second Highest Salary](https://leetcode.com/problems/second-highest-salary/)| [SQL](./src/second-highest-salary/res.txt)|Easy|
2728
|177|[Nth Highest Salary](https://leetcode.com/problems/nth-highest-salary/)| [SQL](./src/nth-highest-salary/res.txt)|Medium|
@@ -35,6 +36,7 @@ This is the solution collection of my LeetCode problems, most of them are progra
3536
|197|[Rising Temperature](https://leetcode.com/problems/rising-temperature/) | [SQL](./src/rising-temperature/res.txt)|Easy|
3637
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [JavaScript](./src/reverse-linked-list/res.js)|Easy|
3738
|240|[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [JavaScript](./src/search-a-2d-matrix-ii/res.js)|Medium|
39+
|307|[Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [JavaScript](./src/range-sum-query-mutable/res.js)|Medium|
3840
|342|[Power of Four](https://leetcode.com/problems/power-of-four/) | [JavaScript](./src/power-of-four/res.js)|Easy|
3941
|344|[Reverse String](https://leetcode.com/problems/reverse-string/) | [JavaScript](./src/reverse-string/res.js)|Easy|
4042
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [JavaScript](./src/sum-of-left-leaves/res.js)|Easy|

src/range-sum-query-mutable/res.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
3+
*
4+
* The update(i, val) function modifies nums by updating the element at index i to val.
5+
*
6+
* Example:
7+
* Given nums = [1, 3, 5]
8+
* sumRange(0, 2) -> 9
9+
* update(1, 2)
10+
* sumRange(0, 2) -> 8
11+
*
12+
* Note:
13+
* The array is only modifiable by the update function.
14+
*
15+
* You may assume the number of calls to update and sumRange function is distributed evenly.
16+
*
17+
* res.js
18+
* @authors Joe Jiang (hijiangtao@gmail.com)
19+
* @date 2017-02-28 20:10:00
20+
* @version $Id$
21+
*/
22+
23+
/**
24+
* @param {number[]} nums
25+
*/
26+
var NumArray = function(nums) {
27+
this.nums = nums;
28+
};
29+
30+
/**
31+
* @param {number} i
32+
* @param {number} val
33+
* @return {void}
34+
*/
35+
NumArray.prototype.update = function(i, val) {
36+
this.nums[i] = val;
37+
};
38+
39+
/**
40+
* @param {number} i
41+
* @param {number} j
42+
* @return {number}
43+
*/
44+
NumArray.prototype.sumRange = function(i, j) {
45+
let sum = 0;
46+
for (let ind=i; ind<=j; ind++) {
47+
sum += this.nums[ind];
48+
}
49+
50+
return sum;
51+
};
52+
53+
/**
54+
* Your NumArray object will be instantiated and called as such:
55+
* var obj = Object.create(NumArray).createNew(nums)
56+
* obj.update(i,val)
57+
* var param_2 = obj.sumRange(i,j)
58+
*/

src/single-number/res.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Given an array of integers, every element appears twice except for one. Find that single one.
3+
*
4+
* Note:
5+
* Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
6+
*
7+
* @authors Joe Jiang (hijiangtao@gmail.com)
8+
* @date 2017-02-28 22:47:17
9+
* @version $Id$
10+
*
11+
* @param {number[]} nums
12+
* @return {number}
13+
*/
14+
let singleNumber = function(nums) {
15+
let size = nums.length,
16+
result = 0;
17+
18+
if (!size) {
19+
return null;
20+
}
21+
22+
for (let i=0; i<size; i++) {
23+
result ^= nums[i];
24+
}
25+
26+
return result;
27+
};

0 commit comments

Comments
 (0)