Movatterモバイル変換


[0]ホーム

URL:


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

Commitc247963

Browse files
authored
Added tasks 6-23
1 parentd565b71 commitc247963

File tree

21 files changed

+1532
-0
lines changed

21 files changed

+1532
-0
lines changed

‎README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ Cpp-based LeetCode algorithm problem solutions, regularly updated.
164164

165165
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
166166
|-|-|-|-|-|-
167+
| 0021 |[Merge Two Sorted Lists](src/main/cpp/g0001_0100/s0021_merge_two_sorted_lists/Solution.cpp)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
167168

168169
####Day 4 Linked List
169170

@@ -244,6 +245,7 @@ Cpp-based LeetCode algorithm problem solutions, regularly updated.
244245

245246
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
246247
|-|-|-|-|-|-
248+
| 0019 |[Remove Nth Node From End of List](src/main/cpp/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Big_O_Time_O(L)_Space_O(L) | 0 | 100.00
247249

248250
####Day 4 Linked List
249251

@@ -341,12 +343,15 @@ Cpp-based LeetCode algorithm problem solutions, regularly updated.
341343

342344
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
343345
|-|-|-|-|-|-
346+
| 0007 |[Reverse Integer](src/main/cpp/g0001_0100/s0007_reverse_integer/Solution.cpp)| Medium | Top_Interview_Questions, Math | 0 | 100.00
347+
| 0009 |[Palindrome Number](src/main/cpp/g0001_0100/s0009_palindrome_number/Solution.cpp)| Easy | Math | 0 | 100.00
344348

345349
####Udemy Strings
346350

347351
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
348352
|-|-|-|-|-|-
349353
| 0003 |[Longest Substring Without Repeating Characters](src/main/cpp/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1) | 5 | 86.87
354+
| 0020 |[Valid Parentheses](src/main/cpp/g0001_0100/s0020_valid_parentheses/Solution.cpp)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
350355
| 0005 |[Longest Palindromic Substring](src/main/cpp/g0001_0100/s0005_longest_palindromic_substring/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 7 | 94.72
351356
| 0394 |[Decode String](src/main/cpp/g0301_0400/s0394_decode_string/Solution.cpp)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
352357

@@ -386,6 +391,7 @@ Cpp-based LeetCode algorithm problem solutions, regularly updated.
386391

387392
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
388393
|-|-|-|-|-|-
394+
| 0021 |[Merge Two Sorted Lists](src/main/cpp/g0001_0100/s0021_merge_two_sorted_lists/Solution.cpp)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
389395

390396
####Udemy Tree Stack Queue
391397

@@ -415,6 +421,8 @@ Cpp-based LeetCode algorithm problem solutions, regularly updated.
415421

416422
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
417423
|-|-|-|-|-|-
424+
| 0022 |[Generate Parentheses](src/main/cpp/g0001_0100/s0022_generate_parentheses/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 0 | 100.00
425+
| 0017 |[Letter Combinations of a Phone Number](src/main/cpp/g0001_0100/s0017_letter_combinations_of_a_phone_number/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 0 | 100.00
418426

419427
####Udemy Bit Manipulation
420428

@@ -464,6 +472,7 @@ Cpp-based LeetCode algorithm problem solutions, regularly updated.
464472

465473
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
466474
|-|-|-|-|-|-
475+
| 0021 |[Merge Two Sorted Lists](src/main/cpp/g0001_0100/s0021_merge_two_sorted_lists/Solution.cpp)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
467476

468477
####Day 8 Linked List
469478

@@ -474,6 +483,7 @@ Cpp-based LeetCode algorithm problem solutions, regularly updated.
474483

475484
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
476485
|-|-|-|-|-|-
486+
| 0020 |[Valid Parentheses](src/main/cpp/g0001_0100/s0020_valid_parentheses/Solution.cpp)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
477487

478488
####Day 10 Tree
479489

@@ -639,6 +649,7 @@ Cpp-based LeetCode algorithm problem solutions, regularly updated.
639649

640650
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
641651
|-|-|-|-|-|-
652+
| 0019 |[Remove Nth Node From End of List](src/main/cpp/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Big_O_Time_O(L)_Space_O(L) | 0 | 100.00
642653

643654
####Day 6 Sliding Window
644655

@@ -665,6 +676,7 @@ Cpp-based LeetCode algorithm problem solutions, regularly updated.
665676

666677
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
667678
|-|-|-|-|-|-
679+
| 0021 |[Merge Two Sorted Lists](src/main/cpp/g0001_0100/s0021_merge_two_sorted_lists/Solution.cpp)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
668680

669681
####Day 11 Recursion Backtracking
670682

@@ -745,6 +757,8 @@ Cpp-based LeetCode algorithm problem solutions, regularly updated.
745757

746758
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
747759
|-|-|-|-|-|-
760+
| 0017 |[Letter Combinations of a Phone Number](src/main/cpp/g0001_0100/s0017_letter_combinations_of_a_phone_number/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 0 | 100.00
761+
| 0022 |[Generate Parentheses](src/main/cpp/g0001_0100/s0022_generate_parentheses/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 0 | 100.00
748762

749763
####Day 12 Dynamic Programming
750764

@@ -1262,9 +1276,19 @@ Cpp-based LeetCode algorithm problem solutions, regularly updated.
12621276
| 0338 |[Counting Bits](src/main/cpp/g0301_0400/s0338_counting_bits/Solution.cpp)| Easy | Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 0 | 100.00
12631277
| 0322 |[Coin Change](src/main/cpp/g0301_0400/s0322_coin_change/Solution.cpp)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(amount) | 12 | 97.65
12641278
| 0300 |[Longest Increasing Subsequence](src/main/cpp/g0201_0300/s0300_longest_increasing_subsequence/Solution.cpp)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Algorithm_II_Day_16_Dynamic_Programming, Binary_Search_II_Day_3, Dynamic_Programming_I_Day_18, Udemy_Dynamic_Programming, Big_O_Time_O(n\*log_n)_Space_O(n) | 4 | 94.11
1279+
| 0023 |[Merge k Sorted Lists](src/main/cpp/g0001_0100/s0023_merge_k_sorted_lists/Solution.cpp)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Heap_Priority_Queue, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(k\*n\*log(k))_Space_O(log(k)) | 7 | 98.72
1280+
| 0022 |[Generate Parentheses](src/main/cpp/g0001_0100/s0022_generate_parentheses/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(2^n)_Space_O(n) | 0 | 100.00
1281+
| 0021 |[Merge Two Sorted Lists](src/main/cpp/g0001_0100/s0021_merge_two_sorted_lists/Solution.cpp)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_7_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
1282+
| 0020 |[Valid Parentheses](src/main/cpp/g0001_0100/s0020_valid_parentheses/Solution.cpp)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Data_Structure_I_Day_9_Stack_Queue, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1283+
| 0019 |[Remove Nth Node From End of List](src/main/cpp/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Algorithm_I_Day_5_Two_Pointers, Level_2_Day_3_Linked_List, Big_O_Time_O(L)_Space_O(L) | 0 | 100.00
1284+
| 0017 |[Letter Combinations of a Phone Number](src/main/cpp/g0001_0100/s0017_letter_combinations_of_a_phone_number/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(4^n)_Space_O(n) | 0 | 100.00
12651285
| 0015 |[3Sum](src/main/cpp/g0001_0100/s0015_3sum/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_II_Day_1_Array, Algorithm_II_Day_3_Two_Pointers, Udemy_Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 53 | 90.17
12661286
| 0011 |[Container With Most Water](src/main/cpp/g0001_0100/s0011_container_with_most_water/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Greedy, Two_Pointers, Algorithm_II_Day_4_Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 54 | 88.17
12671287
| 0010 |[Regular Expression Matching](src/main/cpp/g0001_0100/s0010_regular_expression_matching/Solution.cpp)| Hard | Top_Interview_Questions, String, Dynamic_Programming, Recursion, Udemy_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(m\*n) | 0 | 100.00
1288+
| 0009 |[Palindrome Number](src/main/cpp/g0001_0100/s0009_palindrome_number/Solution.cpp)| Easy | Math, Udemy_Integers | 0 | 100.00
1289+
| 0008 |[String to Integer (atoi)](src/main/cpp/g0001_0100/s0008_string_to_integer_atoi/Solution.cpp)| Medium | Top_Interview_Questions, String | 0 | 100.00
1290+
| 0007 |[Reverse Integer](src/main/cpp/g0001_0100/s0007_reverse_integer/Solution.cpp)| Medium | Top_Interview_Questions, Math, Udemy_Integers | 0 | 100.00
1291+
| 0006 |[Zigzag Conversion](src/main/cpp/g0001_0100/s0006_zigzag_conversion/Solution.cpp)| Medium | String | 3 | 97.69
12681292
| 0005 |[Longest Palindromic Substring](src/main/cpp/g0001_0100/s0005_longest_palindromic_substring/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Data_Structure_II_Day_9_String, Algorithm_II_Day_14_Dynamic_Programming, Dynamic_Programming_I_Day_17, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 7 | 94.72
12691293
| 0004 |[Median of Two Sorted Arrays](src/main/cpp/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.cpp)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Divide_and_Conquer, Big_O_Time_O(log(min(N,M)))_Space_O(1) | 19 | 85.75
12701294
| 0003 |[Longest Substring Without Repeating Characters](src/main/cpp/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.cpp)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Algorithm_I_Day_6_Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer, Udemy_Strings, Big_O_Time_O(n)_Space_O(1) | 5 | 86.87
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// #Medium #String #2024_05_23_Time_3_ms_(97.69%)_Space_10_MB_(97.13%)
2+
3+
#include<string>
4+
#include<vector>
5+
6+
classSolution {
7+
public:
8+
std::stringconvert(std::string s,int numRows) {
9+
intsLen = s.length();
10+
if (numRows ==1) {
11+
return s;
12+
}
13+
int maxDist = numRows *2 -2;
14+
std::string buf;
15+
buf.reserve(sLen);
16+
for (int i =0; i < numRows; i++) {
17+
int index = i;
18+
if (i ==0 || i == numRows -1) {
19+
while (index <sLen) {
20+
buf += s[index];
21+
index += maxDist;
22+
}
23+
}else {
24+
while (index <sLen) {
25+
buf += s[index];
26+
index += maxDist - i *2;
27+
if (index >=sLen) {
28+
break;
29+
}
30+
buf += s[index];
31+
index += i *2;
32+
}
33+
}
34+
}
35+
return buf;
36+
}
37+
};
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
6\. Zigzag Conversion
2+
3+
Medium
4+
5+
The string`"PAYPALISHIRING"` is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
6+
7+
P A H N A P L S I I G Y I R
8+
9+
And then read line by line:`"PAHNAPLSIIGYIR"`
10+
11+
Write the code that will take a string and make this conversion given a number of rows:
12+
13+
string convert(string s, int numRows);
14+
15+
**Example 1:**
16+
17+
**Input:** s = "PAYPALISHIRING", numRows = 3
18+
19+
**Output:** "PAHNAPLSIIGYIR"
20+
21+
**Example 2:**
22+
23+
**Input:** s = "PAYPALISHIRING", numRows = 4
24+
25+
**Output:** "PINALSIGYAHRPI"
26+
27+
**Explanation:** P I N A L S I G Y A H R P I
28+
29+
**Example 3:**
30+
31+
**Input:** s = "A", numRows = 1
32+
33+
**Output:** "A"
34+
35+
**Constraints:**
36+
37+
*`1 <= s.length <= 1000`
38+
*`s` consists of English letters (lower-case and upper-case),`','` and`'.'`.
39+
*`1 <= numRows <= 1000`
40+
41+
To solve the Zigzag Conversion problem in Java using a`Solution` class, we'll follow these steps:
42+
43+
1. Define a`Solution` class with a method named`convert`.
44+
2. Create an array of strings to represent each row of the zigzag pattern.
45+
3. Initialize variables to keep track of the current row (`row`) and the direction of traversal (`down`).
46+
4. Iterate through each character in the input string`s`.
47+
- Append the current character to the string representing the current row.
48+
- If we reach the first or last row, change the direction of traversal accordingly.
49+
- Update the current row based on the direction of traversal.
50+
5. Concatenate the strings representing each row to form the final zigzag conversion.
51+
6. Return the concatenated string.
52+
7. Handle edge cases where the number of rows is 1 or the input string is empty.
53+
54+
Here's the implementation:
55+
56+
```java
57+
publicclassSolution {
58+
59+
publicStringconvert(Strings,intnumRows) {
60+
if (numRows==1|| s.length()<= numRows) {
61+
return s;
62+
}
63+
64+
StringBuilder[] rows=newStringBuilder[numRows];
65+
for (int i=0; i< numRows; i++) {
66+
rows[i]=newStringBuilder();
67+
}
68+
69+
int row=0;
70+
boolean down=false;
71+
72+
for (char c: s.toCharArray()) {
73+
rows[row].append(c);
74+
if (row==0|| row== numRows-1) {
75+
down=!down;
76+
}
77+
row+= down?1:-1;
78+
}
79+
80+
StringBuilder result=newStringBuilder();
81+
for (StringBuilder sb: rows) {
82+
result.append(sb);
83+
}
84+
85+
return result.toString();
86+
}
87+
88+
publicstaticvoidmain(String[]args) {
89+
Solution solution=newSolution();
90+
91+
// Test cases
92+
String s1="PAYPALISHIRING";
93+
int numRows1=3;
94+
System.out.println("Example 1 Output:"+ solution.convert(s1, numRows1));
95+
96+
String s2="PAYPALISHIRING";
97+
int numRows2=4;
98+
System.out.println("Example 2 Output:"+ solution.convert(s2, numRows2));
99+
100+
String s3="A";
101+
int numRows3=1;
102+
System.out.println("Example 3 Output:"+ solution.convert(s3, numRows3));
103+
}
104+
}
105+
```
106+
107+
This implementation provides a solution to the Zigzag Conversion problem in Java.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// #Medium #Top_Interview_Questions #Math #Udemy_Integers
2+
// #2024_05_23_Time_0_ms_(100.00%)_Space_7.5_MB_(52.06%)
3+
4+
classSolution {
5+
public:
6+
intreverse(int x) {
7+
long rev =0;
8+
while (x !=0) {
9+
rev = (rev *10) + (x %10);
10+
x /=10;
11+
}
12+
if (rev > INT_MAX || rev < INT_MIN) {
13+
return0;
14+
}
15+
returnstatic_cast<int>(rev);
16+
}
17+
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
7\. Reverse Integer
2+
3+
Medium
4+
5+
Given a signed 32-bit integer`x`, return`x`_with its digits reversed_. If reversing`x` causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return`0`.
6+
7+
**Assume the environment does not allow you to store 64-bit integers (signed or unsigned).**
8+
9+
**Example 1:**
10+
11+
**Input:** x = 123
12+
13+
**Output:** 321
14+
15+
**Example 2:**
16+
17+
**Input:** x = -123
18+
19+
**Output:** -321
20+
21+
**Example 3:**
22+
23+
**Input:** x = 120
24+
25+
**Output:** 21
26+
27+
**Example 4:**
28+
29+
**Input:** x = 0
30+
31+
**Output:** 0
32+
33+
**Constraints:**
34+
35+
* <code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code>
36+
37+
To solve the Reverse Integer problem in Java using a`Solution` class, we'll follow these steps:
38+
39+
1. Define a`Solution` class with a method named`reverse`.
40+
2. Initialize variables to keep track of the reversed integer (`rev`), the sign of the input integer (`sign`), and the absolute value of the input integer (`x`).
41+
3. Iterate through each digit of the input integer`x`:
42+
- Extract the least significant digit using the modulo operator.
43+
- Update the reversed integer`rev` by multiplying it by 10 and adding the extracted digit.
44+
- Update`x` by removing the least significant digit using integer division.
45+
4. Check if the reversed integer`rev` overflows the signed 32-bit integer range. If so, return 0.
46+
5. Return the reversed integer`rev` with the appropriate sign.
47+
48+
Here's the implementation:
49+
50+
```java
51+
publicclassSolution {
52+
53+
publicintreverse(intx) {
54+
int rev=0;
55+
int sign= (x<0)?-1:1;
56+
int limit=Integer.MAX_VALUE/10;
57+
int absX=Math.abs(x);
58+
59+
while (absX>0) {
60+
int digit= absX%10;
61+
absX/=10;
62+
63+
if (rev> limit|| (rev== limit&& digit>7)) {
64+
return0;
65+
}
66+
67+
if (rev<-limit|| (rev==-limit&& digit<-8)) {
68+
return0;
69+
}
70+
71+
rev= rev*10+ digit;
72+
}
73+
74+
return rev* sign;
75+
}
76+
77+
publicstaticvoidmain(String[]args) {
78+
Solution solution=newSolution();
79+
80+
// Test cases
81+
int x1=123;
82+
System.out.println("Example 1 Output:"+ solution.reverse(x1));
83+
84+
int x2=-123;
85+
System.out.println("Example 2 Output:"+ solution.reverse(x2));
86+
87+
int x3=120;
88+
System.out.println("Example 3 Output:"+ solution.reverse(x3));
89+
90+
int x4=0;
91+
System.out.println("Example 4 Output:"+ solution.reverse(x4));
92+
}
93+
}
94+
```
95+
96+
This implementation provides a solution to the Reverse Integer problem in Java.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp