Skip to content

Commit 90df0ed

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 88dcaf1 commit 90df0ed

7 files changed

+574
-1
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
## 157. Read N Characters Given Read4
2+
3+
### Question
4+
Given a file and assume that you can only read the file using a given method read4, implement a method to read n characters.
5+
6+
7+
8+
Method read4:
9+
10+
The API read4 reads 4 consecutive characters from the file, then writes those characters into the buffer array buf.
11+
12+
The return value is the number of actual characters read.
13+
14+
Note that read4() has its own file pointer, much like FILE *fp in C.
15+
16+
Definition of read4:
17+
18+
Parameter: char[] buf
19+
Returns: int
20+
21+
Note: buf[] is destination not source, the results from read4 will be copied to buf[]
22+
23+
Below is a high level example of how read4 works:
24+
25+
File file("abcdefghijk"); // File is "abcdefghijk", initially file pointer (fp) points to 'a'
26+
char[] buf = new char[4]; // Create buffer with enough space to store characters
27+
read4(buf); // read4 returns 4. Now buf = "abcd", fp points to 'e'
28+
read4(buf); // read4 returns 4. Now buf = "efgh", fp points to 'i'
29+
read4(buf); // read4 returns 3. Now buf = "ijk", fp points to end of file
30+
31+
Method read:
32+
33+
By using the read4 method, implement the method read that reads n characters from the file and store it in the buffer array buf. Consider that you cannot manipulate the file directly.
34+
35+
The return value is the number of actual characters read.
36+
37+
Definition of read:
38+
39+
Parameters: char[] buf, int n
40+
Returns: int
41+
42+
Note: buf[] is destination not source, you will need to write the results to buf[]
43+
44+
```
45+
Example 1:
46+
47+
Input: file = "abc", n = 4
48+
Output: 3
49+
Explanation: After calling your read method, buf should contain "abc". We read a total of 3 characters from the file, so return 3. Note that "abc" is the file's content, not buf. buf is the destination buffer that you will have to write the results to.
50+
51+
Example 2:
52+
53+
Input: file = "abcde", n = 5
54+
Output: 5
55+
Explanation: After calling your read method, buf should contain "abcde". We read a total of 5 characters from the file, so return 5.
56+
57+
Example 3:
58+
59+
Input: file = "abcdABCD1234", n = 12
60+
Output: 12
61+
Explanation: After calling your read method, buf should contain "abcdABCD1234". We read a total of 12 characters from the file, so return 12.
62+
63+
Example 4:
64+
65+
Input: file = "leetcode", n = 5
66+
Output: 5
67+
Explanation: After calling your read method, buf should contain "leetc". We read a total of 5 characters from the file, so return 5.
68+
```
69+
70+
71+
Note:
72+
1. Consider that you cannot manipulate the file directly, the file is only accesible for read4 but not for read.
73+
2. The read function will only be called once for each test case.
74+
3. You may assume the destination buffer array, buf, is guaranteed to have enough space for storing n characters.
75+
76+
### Solutions:
77+
* Method 1: 14m39s
78+
```
79+
/**
80+
* The read4 API is defined in the parent class Reader4.
81+
* int read4(char[] buf);
82+
*/
83+
public class Solution extends Reader4 {
84+
/**
85+
* @param buf Destination buffer
86+
* @param n Number of characters to read
87+
* @return The number of actual characters read
88+
*/
89+
public int read(char[] buf, int n) {
90+
int read = 0;
91+
int index = 0;
92+
while(read < n){
93+
char[] buffer = new char[4];
94+
int cnt = read4(buffer);
95+
if(cnt < 4 && read + cnt <= n){
96+
for(int i = 0; i < cnt; i++){
97+
buf[index++] = buffer[i];
98+
}
99+
return read + cnt;
100+
}else if(cnt == 4 && read + cnt < n){
101+
for(int i = 0; i < 4; i++){
102+
buf[index++] = buffer[i];
103+
}
104+
read += 4;
105+
}else if(read + cnt >= n){
106+
while(index < n){
107+
buf[index] = buffer[index - read];
108+
index++;
109+
}
110+
return n;
111+
}
112+
}
113+
return n;
114+
}
115+
}
116+
```
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
## 158. Read N Characters Given Read4 II - Call multiple times
2+
3+
### Question
4+
Given a file and assume that you can only read the file using a given method read4, implement a method read to read n characters. Your method read may be called multiple times.
5+
6+
7+
8+
Method read4:
9+
10+
The API read4 reads 4 consecutive characters from the file, then writes those characters into the buffer array buf.
11+
12+
The return value is the number of actual characters read.
13+
14+
Note that read4() has its own file pointer, much like FILE *fp in C.
15+
16+
Definition of read4:
17+
18+
Parameter: char[] buf
19+
Returns: int
20+
21+
Note: buf[] is destination not source, the results from read4 will be copied to buf[]
22+
23+
Below is a high level example of how read4 works:
24+
25+
File file("abcdefghijk"); // File is "abcdefghijk", initially file pointer (fp) points to 'a'
26+
char[] buf = new char[4]; // Create buffer with enough space to store characters
27+
read4(buf); // read4 returns 4. Now buf = "abcd", fp points to 'e'
28+
read4(buf); // read4 returns 4. Now buf = "efgh", fp points to 'i'
29+
read4(buf); // read4 returns 3. Now buf = "ijk", fp points to end of file
30+
31+
32+
33+
Method read:
34+
35+
By using the read4 method, implement the method read that reads n characters from the file and store it in the buffer array buf. Consider that you cannot manipulate the file directly.
36+
37+
The return value is the number of actual characters read.
38+
39+
Definition of read:
40+
41+
Parameters: char[] buf, int n
42+
Returns: int
43+
44+
Note: buf[] is destination not source, you will need to write the results to buf[]
45+
46+
```
47+
Example 1:
48+
49+
File file("abc");
50+
Solution sol;
51+
// Assume buf is allocated and guaranteed to have enough space for storing all characters from the file.
52+
sol.read(buf, 1); // After calling your read method, buf should contain "a". We read a total of 1 character from the file, so return 1.
53+
sol.read(buf, 2); // Now buf should contain "bc". We read a total of 2 characters from the file, so return 2.
54+
sol.read(buf, 1); // We have reached the end of file, no more characters can be read. So return 0.
55+
56+
Example 2:
57+
58+
File file("abc");
59+
Solution sol;
60+
sol.read(buf, 4); // After calling your read method, buf should contain "abc". We read a total of 3 characters from the file, so return 3.
61+
sol.read(buf, 1); // We have reached the end of file, no more characters can be read. So return 0.
62+
```
63+
64+
Note:
65+
1. Consider that you cannot manipulate the file directly, the file is only accesible for read4 but not for read.
66+
2. The read function may be called multiple times.
67+
3. Please remember to RESET your class variables declared in Solution, as static/class variables are persisted across multiple test cases. Please see here for more details.
68+
4. You may assume the destination buffer array, buf, is guaranteed to have enough space for storing n characters.
69+
5. It is guaranteed that in a given test case the same buffer buf is called by read.
70+
71+
72+
### Solutions:
73+
* Method 1:
74+
```
75+
/**
76+
* The read4 API is defined in the parent class Reader4.
77+
* int read4(char[] buf);
78+
*/
79+
public class Solution extends Reader4 {
80+
private String s;
81+
private int index = 0;
82+
/**
83+
* @param buf Destination buffer
84+
* @param n Number of characters to read
85+
* @return The number of actual characters read
86+
*/
87+
public int read(char[] buf, int n) {
88+
if(s == null){
89+
s = "";
90+
char[] buffer = new char[4];
91+
int cnt = 0;
92+
while((cnt = read4(buffer)) != 0){
93+
for(int i = 0; i < cnt; i++)
94+
s += buffer[i];
95+
}
96+
}
97+
if(index >= s.length()) return 0;
98+
if(index + n <= s.length()){
99+
String sub = s.substring(index, index + n);
100+
for(int i = 0; i < n; i++){
101+
buf[i] = sub.charAt(i);
102+
}
103+
index += n;
104+
return n;
105+
}else{
106+
for(int i = index; i < s.length(); i++){
107+
buf[i - index] = s.charAt(i);
108+
}
109+
int res = s.length() - index;
110+
index = s.length();
111+
return res;
112+
}
113+
}
114+
}
115+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## 170. Two Sum III - Data structure design
2+
3+
### Question
4+
Design and implement a TwoSum class. It should support the following operations: add and find.
5+
6+
add - Add the number to an internal data structure.
7+
find - Find if there exists any pair of numbers which sum is equal to the value.
8+
9+
```
10+
Example 1:
11+
12+
add(1); add(3); add(5);
13+
find(4) -> true
14+
find(7) -> false
15+
16+
Example 2:
17+
18+
add(3); add(1); add(2);
19+
find(3) -> true
20+
find(6) -> false
21+
```
22+
23+
### Solutions:
24+
* Method 1: HashMap + List
25+
```Java
26+
class TwoSum {
27+
private List<Integer> list;
28+
private Map<Integer, Integer> map;
29+
/** Initialize your data structure here. */
30+
public TwoSum() {
31+
this.list = new ArrayList<>();
32+
this.map = new HashMap<>();
33+
}
34+
35+
/** Add the number to an internal data structure.. */
36+
public void add(int number) {
37+
this.list.add(number);
38+
this.map.put(number, map.getOrDefault(number, 0) + 1);
39+
}
40+
41+
/** Find if there exists any pair of numbers which sum is equal to the value. */
42+
public boolean find(int value) {
43+
for(int i = 0; i < list.size(); i++){
44+
int num = list.get(i);
45+
int another = value - num;
46+
if(num == another && map.get(num) >= 2) return true;
47+
else if(num != another && map.containsKey(another)) return true;
48+
}
49+
return false;
50+
}
51+
}
52+
53+
/**
54+
* Your TwoSum object will be instantiated and called as such:
55+
* TwoSum obj = new TwoSum();
56+
* obj.add(number);
57+
* boolean param_2 = obj.find(value);
58+
*/
59+
```

leetcode/3. Longest Substring Without Repeating Characters.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,31 @@ class Solution {
7474
return max;
7575
}
7676
}
77-
```
77+
```
78+
79+
### Third Time
80+
* Method 1: Two pointers + Set
81+
```Java
82+
class Solution {
83+
public int lengthOfLongestSubstring(String s) {
84+
if(s == null || s.length() == 0) return 0;
85+
int slow = 0, fast = 0;
86+
Set<Character> set = new HashSet<>();
87+
char[] arr = s.toCharArray();
88+
int res = 0;
89+
while(fast < arr.length){
90+
if(!set.contains(arr[fast])){
91+
set.add(arr[fast]);
92+
res = Math.max(res, fast - slow + 1);
93+
}else{ // set already contains key of arr[fast]
94+
while(arr[slow] != arr[fast] && slow < fast){
95+
set.remove(arr[slow++]);
96+
}
97+
slow++;
98+
}
99+
fast++;
100+
}
101+
return res;
102+
}
103+
}
104+
```

0 commit comments

Comments
 (0)