Skip to content

Commit 52097be

Browse files
committed
new solution
1 parent f01944c commit 52097be

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

415.AddStrings.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// 415. Add Strings
2+
// Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2
3+
// as a string.
4+
// You must solve the problem without using any built-in library for handling large integers
5+
// (such as BigInteger). You must also not convert the inputs to integers directly.
6+
7+
// Example 1:
8+
// Input: num1 = "11", num2 = "123"
9+
// Output: "134"
10+
// Example 2:
11+
// Input: num1 = "456", num2 = "77"
12+
// Output: "533"
13+
// Example 3:
14+
// Input: num1 = "0", num2 = "0"
15+
// Output: "0"
16+
17+
// Constraints:
18+
// 1 <= num1.length, num2.length <= 104
19+
// num1 and num2 consist of only digits.
20+
// num1 and num2 don't have any leading zeros except for the zero itself.
21+
22+
23+
//Not working for long long inputs
24+
public class Solution {
25+
public string AddStrings(string num1, string num2) {
26+
return (long.Parse(num1) + long.Parse(num2)).ToString();
27+
}
28+
}
29+
30+
public class Solution {
31+
public string AddStrings(string num1, string num2) {
32+
StringBuilder result = new StringBuilder();
33+
int carry = 0;
34+
int p1 = num1.Length - 1;
35+
int p2 = num2.Length - 1;
36+
while (p1 >= 0 || p2 >= 0) {
37+
int x1 = p1 >= 0 ? num1[p1] - '0' : 0;
38+
int x2 = p2 >= 0 ? num2[p2] - '0' : 0;
39+
int sum = x1 + x2 + carry;
40+
result.Insert(0, sum % 10);
41+
carry = sum / 10;
42+
p1--;
43+
p2--;
44+
}
45+
if (carry > 0) {
46+
result.Insert(0, carry);
47+
}
48+
return result.ToString();
49+
}
50+
}

452.FindMinArrowShots.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
// -231 <= xstart < xend <= 231 - 1
2525

2626

27+
//The intuition behind the solution is to sort the intervals (balloons) by their end points.
28+
// Then, iterate through the sorted list and shoot an arrow at the end point of the current balloon.
29+
// This arrow will also burst all other balloons whose start point is less than or equal to the current
30+
// balloon's end point.
31+
// This is because the sorted order ensures that all such balloons will overlap with the current balloon.
2732
public class Solution {
2833
public int FindMinArrowShots(int[][] points) {
2934
points = points.OrderBy(x => x[1]).ThenBy(x => x[0]).ToArray();

482.LicenseKeyFormatting.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// 482. License Key Formatting
2+
// You are given a license key represented as a string s that consists of only alphanumeric characters
3+
// and dashes. The string is separated into n + 1 groups by n dashes. You are also given an integer k.
4+
5+
// We want to reformat the string s such that each group contains exactly k characters,
6+
// except for the first group, which could be shorter than k but still must contain at least one character.
7+
// Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.
8+
9+
// Return the reformatted license key.
10+
11+
// Example 1:
12+
// Input: s = "5F3Z-2e-9-w", k = 4
13+
// Output: "5F3Z-2E9W"
14+
// Explanation: The string s has been split into two parts, each part has 4 characters.
15+
// Note that the two extra dashes are not needed and can be removed.
16+
// Example 2:
17+
// Input: s = "2-5g-3-J", k = 2
18+
// Output: "2-5G-3J"
19+
// Explanation: The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
20+
21+
// Constraints:
22+
// 1 <= s.length <= 105
23+
// s consists of English letters, digits, and dashes '-'.
24+
// // 1 <= k <= 104
25+
26+
public class Solution {
27+
public string LicenseKeyFormatting(string s, int k) {
28+
s = s.Replace("-", "").ToUpper();
29+
//intution: insert '-' from the end of the string with k interval.
30+
var sb = new StringBuilder(s);
31+
for(int i = s.Length - k; i > 0; i -= k){
32+
sb.Insert(i, '-');
33+
}
34+
return sb.ToString();
35+
}
36+
}
37+
38+
39+
public class Solution {
40+
public string LicenseKeyFormatting(string s, int k) {
41+
42+
s = s.Replace("-", "").ToUpper();
43+
string res = "";
44+
for(int i = s.Length; i >= 0; i -= k){
45+
string temp = "";
46+
for(int j = Math.Max(i - k, 0); j < i; j++ ){
47+
temp += s[j];
48+
}
49+
res = temp + res;
50+
if(i - k > 0)
51+
res = "-" + res;
52+
}
53+
return res;
54+
}
55+
}

541.ReverseStr.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 541. Reverse String II
2+
// Given a string s and an integer k, reverse the first k characters for every 2k characters counting
3+
// from the start of the string.
4+
5+
// If there are fewer than k characters left, reverse all of them.
6+
// If there are less than 2k but greater than or equal to k characters, then reverse the first k
7+
// characters and leave the other as original.
8+
9+
// Example 1:
10+
// Input: s = "abcdefg", k = 2
11+
// Output: "bacdfeg"
12+
// Example 2:
13+
// Input: s = "abcd", k = 2
14+
// Output: "bacd"
15+
16+
// Constraints:
17+
// 1 <= s.length <= 104
18+
// s consists of only lowercase English letters.
19+
// 1 <= k <= 104
20+
21+
public class Solution {
22+
public string ReverseStr(string s, int k) {
23+
char[] arr = s.ToCharArray();
24+
//intution: reverse the first k characters for every 2k characters counting from the start of the string.
25+
//incrementing i by 2*k because we need to reverse the first k characters for every 2k characters
26+
for (int i = 0; i < arr.Length; i += 2 * k) {
27+
int left = i;
28+
int right = Math.Min(i + k - 1, arr.Length - 1);
29+
while (left < right) {
30+
(arr[left], arr[right]) = ((arr[right], arr[left]));
31+
left++;
32+
right--;
33+
}
34+
}
35+
return new string(arr);
36+
}
37+
}

0 commit comments

Comments
 (0)