Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 59c35f0

Browse files
solves #157: Read N Characters Given Read 4 in java
1 parent 88b0657 commit 59c35f0

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

β€ŽREADME.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
| 153 | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array) | [![Java](assets/java.png)](src/FindMinimumInRotatedSortedArray.java) | |
152152
| 155 | [Min Stack](https://leetcode.com/problems/min-stack) | [![Java](assets/java.png)](src/MinStack.java) [![Python](assets/python.png)](python/min_stack.py) | |
153153
| 156 | πŸ”’ [Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down) | | |
154-
| 157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4) | | |
154+
| 157 | πŸ”’ [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4) | [![Java](assets/java.png)](src/ReadNCharactersGivenRead4java) | |
155155
| 159 | πŸ”’ [Longest Substring With At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters) | | |
156156
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists) | [![Java](assets/java.png)](src/IntersectionOf2LinkedLists.java) [![Python](assets/python.png)](python/intersecction_of_two_linked_lists.py) | |
157157
| 161 | πŸ”’ [One Edit Distance](https://leetcode.com/problems/one-edit-distance) | | |

β€Žsrc/ReadNCharactersGivenRead4.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// https://leetcode.com/problems/read-n-characters-given-read4
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class ReadNCharactersGivenRead4 {
6+
static class Reader4 {
7+
public int read4(char[] buffer) {
8+
return 0;
9+
}
10+
}
11+
12+
static class Solution extends Reader4 {
13+
public int read(char[] buf, int n) {
14+
int copiedChars = 0, readChars = 4;
15+
char[] buf4 = new char[4];
16+
17+
while (copiedChars < n && readChars == 4) {
18+
readChars = read4(buf4);
19+
20+
for (int i = 0; i < readChars; ++i) {
21+
if (copiedChars == n) return copiedChars;
22+
buf[copiedChars] = buf4[i];
23+
++copiedChars;
24+
}
25+
}
26+
return copiedChars;
27+
}
28+
}
29+
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /