Skip to content

Commit 9c0a9f5

Browse files
committed
leetcode
1 parent ce0aba7 commit 9c0a9f5

File tree

7 files changed

+166
-0
lines changed

7 files changed

+166
-0
lines changed

Create-File.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Param(
2+
[Parameter(Mandatory=$true)]
3+
[string]$FileName
4+
)
5+
6+
# Convert the file name to lowercase
7+
$NewFileName = $FileName.ToLower()
8+
9+
# Replace spaces with underscores
10+
$NewFileName = $NewFileName -replace '\s', '_'
11+
12+
# Check if the file already exists
13+
if (Test-Path -Path $NewFileName -PathType Leaf) {
14+
Write-Output "File '$NewFileName' already exists."
15+
# You can choose to skip the creation of the new file or rename the existing file here.
16+
}
17+
else {
18+
# Create a new file with the modified name
19+
$NewFile = New-Item -ItemType File -Path $NewFileName -ErrorAction Stop
20+
# Output the new file name
21+
Write-Output "New file created: $NewFileName"
22+
}

Create-Folder.ps1

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
param (
2+
[Parameter(Mandatory=$true)]
3+
[string]$folderName
4+
)
5+
6+
# Function to capitalize the first letter of each word
7+
function Capitalize-FirstLetter {
8+
param (
9+
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
10+
[string]$string
11+
)
12+
13+
$cultureInfo = (Get-Culture).TextInfo
14+
$cultureInfo.ToTitleCase($string.ToLower())
15+
}
16+
17+
# Split the folder name into individual words
18+
$words = $folderName -split ' '
19+
20+
# Remove spaces from each word and capitalize the first letter
21+
$capitalizedWords = foreach ($word in $words) {
22+
$word = $word -replace '\s', ''
23+
Capitalize-FirstLetter -string $word
24+
}
25+
26+
# Join the capitalized words back into a single string
27+
$folderName = $capitalizedWords -join ''
28+
29+
# Check if the folder already exists
30+
if (Test-Path $folderName) {
31+
Write-Host "Folder '$folderName' already exists."
32+
exit 1
33+
}
34+
35+
# Create the folder
36+
New-Item -ItemType Directory -Path $folderName -Force | Out-Null
37+
38+
Write-Host "Folder '$folderName' created successfully."

FizzBuzz/fizz_buzz.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,20 @@ func fizzBuzz2(n int) []string {
3535
}
3636
return answer
3737
}
38+
39+
func fizzBuzz3(n int) []string {
40+
answer := make([]string, n)
41+
for i := 1; i <= n; i++ {
42+
switch {
43+
case i%3 == 0 && i%5 == 0:
44+
answer[i-1] = "FizzBuzz"
45+
case i%3 == 0:
46+
answer[i-1] = "Fizz"
47+
case i%5 == 0:
48+
answer[i-1] = "Buzz"
49+
default:
50+
answer[i-1] = fmt.Sprintf("%d", i)
51+
}
52+
}
53+
return answer
54+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
228228
- [**744.** Find Smallest Letter Greater Than Target](FindSmallestLetterGreaterThanTarget/find_smallest_letter_greater_than_target.dart)
229229
- [**1802.** Maximum Value at a Given Index in a Bounded Array](MaximumValueAtAGivenIndexInABoundedArray/maximum_value_at_a_given_index_in_a_bounded_array.dart)
230230
- [**412.** Fizz Buzz](FizzBuzz/fizz_buzz.dart)
231+
- [**414.** Third Maximum Number](ThirdMaximumNumber/third_maximum_number.dart)
231232

232233
## Reach me via
233234

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
3+
-* 414. Third Maximum Number *-
4+
5+
Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.
6+
7+
8+
9+
Example 1:
10+
11+
Input: nums = [3,2,1]
12+
Output: 1
13+
Explanation:
14+
The first distinct maximum is 3.
15+
The second distinct maximum is 2.
16+
The third distinct maximum is 1.
17+
Example 2:
18+
19+
Input: nums = [1,2]
20+
Output: 2
21+
Explanation:
22+
The first distinct maximum is 2.
23+
The second distinct maximum is 1.
24+
The third distinct maximum does not exist, so the maximum (2) is returned instead.
25+
Example 3:
26+
27+
Input: nums = [2,2,3,1]
28+
Output: 1
29+
Explanation:
30+
The first distinct maximum is 3.
31+
The second distinct maximum is 2 (both 2's are counted together since they have the same value).
32+
The third distinct maximum is 1.
33+
34+
35+
Constraints:
36+
37+
1 <= nums.length <= 104
38+
-231 <= nums[i] <= 231 - 1
39+
40+
41+
Follow up: Can you find an O(n) solution?
42+
43+
44+
*/
45+
46+
class Solution {
47+
int thirdMax(List<int> nums) {
48+
Set<int> unique = Set<int>()..addAll(nums..sort((a, b) => a - b));
49+
if (unique.length < 3) {
50+
return unique.elementAt(unique.length - 3);
51+
}
52+
return unique.elementAt(unique.length - 3);
53+
}
54+
}
55+
56+
void main() {
57+
final Solution sol = Solution();
58+
print(sol.thirdMax([2, 2, 3, 1]));
59+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import "sort"
4+
5+
func thirdMax(nums []int) int {
6+
// Create a HashSet using a map[int]bool
7+
unique := make(map[int]bool)
8+
9+
// Add elements to the HashSet
10+
for _, num := range nums {
11+
unique[num] = true
12+
}
13+
14+
// Create a slice to store unique numbers
15+
uniqueNums := make([]int, 0, len(unique))
16+
for num := range unique {
17+
uniqueNums = append(uniqueNums, num)
18+
}
19+
20+
// Sort the unique numbers in ascending order
21+
sort.Ints(uniqueNums)
22+
23+
// Check if the unique numbers have at least 3 elements
24+
if len(uniqueNums) < 3 {
25+
return uniqueNums[len(uniqueNums)-1]
26+
}
27+
28+
return uniqueNums[len(uniqueNums)-3]
29+
}

ThirdMaximumNumber/third_maximum_number.md

Whitespace-only changes.

0 commit comments

Comments
 (0)