Skip to content

Commit c5e44ac

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 209015a commit c5e44ac

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,12 @@
475475

476476
[304. Range Sum Query 2D - Immutable](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/304.%20Range%20Sum%20Query%202D%20-%20Immutable.md)
477477

478+
[305. Number Of Island II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/305.%20Number%20Of%20Island%20II.md)
479+
480+
[306. Additive Number](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/306.%20Additive%20Number.md)
481+
482+
[307. Range Sum Query - Mutable](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/307.%20Range%20Sum%20Query%20-%20Mutable.md)
483+
478484

479485
## Algorithm(4th_Edition)
480486
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.

leetcode/306. Additive Number.md

+38
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
---
2+
layout: post
3+
title: "306. Additive Number"
4+
date: 2019-03-03 17:37
5+
author: Botao Xiao
6+
categories: Leetcode
7+
description:
8+
---
19
## 306. Additive Number
210

311
### Question
@@ -59,4 +67,34 @@ class Solution {
5967
}
6068
}
6169
}
70+
```
71+
72+
### Second time
73+
```Java
74+
class Solution {
75+
public boolean isAdditiveNumber(String num) {
76+
boolean result = false;
77+
for(int i = 1; i <= num.length() - 2; i++){
78+
for(int j = i + 1; j <= num.length() - 1; j++){
79+
if(i != 1 && num.substring(0, i).startsWith("0")) continue;
80+
if(j - i != 1 && num.substring(i, j).startsWith("0")) continue;
81+
result |= backtrace(num, Long.parseLong(num.substring(0, i)), Long.parseLong(num.substring(i, j)), j);
82+
}
83+
}
84+
return result;
85+
}
86+
87+
private boolean backtrace(String num, long first, long second, int idx){
88+
if(idx >= num.length()) return true;
89+
long sum = first + second;
90+
if(sum != 0 && num.substring(idx).startsWith("0")) return false;
91+
else{
92+
boolean temp = false;
93+
if(num.substring(idx).startsWith("" + sum)){
94+
temp |= backtrace(num, second, sum, idx + ("" + sum).length());
95+
}
96+
return temp;
97+
}
98+
}
99+
}
62100
```

leetcode/307. Range Sum Query - Mutable.md

+54
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,60 @@ class NumArray {
7474
}
7575
}
7676

77+
/**
78+
* Your NumArray object will be instantiated and called as such:
79+
* NumArray obj = new NumArray(nums);
80+
* obj.update(i,val);
81+
* int param_2 = obj.sumRange(i,j);
82+
*/
83+
```
84+
85+
### Second time
86+
1. Use binary index tree
87+
```Java
88+
class NumArray {
89+
private int[] c;
90+
private int[] nums;
91+
private int lowBit(int n){
92+
return n & (-n);
93+
}
94+
public NumArray(int[] nums) {
95+
c = new int[nums.length + 1];
96+
this.nums = nums;
97+
for(int i = 1; i <= nums.length; i++){
98+
int lowBit = lowBit(i);
99+
int sum = 0;
100+
for(int j = i - lowBit + 1; j <= i; j++){
101+
sum += nums[j - 1];
102+
}
103+
c[i] = sum;
104+
}
105+
}
106+
public void update(int i, int val) {
107+
int diff = val - nums[i];
108+
nums[i] = val;
109+
int index = i + 1;
110+
while(index <= this.nums.length){
111+
c[index] += diff;
112+
index += lowBit(index);
113+
}
114+
}
115+
public int sumRange(int i, int j) {
116+
int sum1 = 0;
117+
int index1 = i;
118+
while(index1 > 0){
119+
sum1 += c[index1];
120+
index1 -= lowBit(index1);
121+
}
122+
int sum2 = 0;
123+
int index2 = j + 1;
124+
while(index2 > 0){
125+
sum2 += c[index2];
126+
index2 -= lowBit(index2);
127+
}
128+
return sum2 - sum1;
129+
}
130+
}
77131
/**
78132
* Your NumArray object will be instantiated and called as such:
79133
* NumArray obj = new NumArray(nums);

0 commit comments

Comments
 (0)