-
Notifications
You must be signed in to change notification settings - Fork 0
/
introduction.go
99 lines (84 loc) · 2.1 KB
/
introduction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package fun_with_arrays
// findMaxConsecutiveOnes Problem: Max Consecutive Ones.
//
// LeetCode: https://leetcode.com/explore/learn/card/fun-with-arrays/521/introduction/3238/
//
// Hint 1:
// You need to think about two things as far as any window is concerned.
// One is the starting point for the window. How do you detect that a new window of 1s has started?
// The next part is detecting the ending point for this window.
// How do you detect the ending point for an existing window?
// If you figure these two things out, you will be able to detect the windows of consecutive ones.
// All that remains afterward is to find the longest such window and return the size.
func findMaxConsecutiveOnes(nums []int) int {
max := 0
current := 0
for i := 0; i < len(nums); i++ {
if current > max {
max = current
}
if nums[i] == 1 {
current++
continue
}
if nums[i] == 0 {
current = 0
}
}
if current > max {
max = current
current = 0
}
return max
}
// findNumbers Problem: Find Numbers with Even Number of Digits.
//
// LeetCode: https://leetcode.com/explore/learn/card/fun-with-arrays/521/introduction/3237/
//
// Hint 1:
// How to compute the number of digits of a number ?
// Hint 2:
// Divide the number by 10 again and again to get the number of digits.
func findNumbers(nums []int) int {
even := 0
for _, v := range nums {
digit := v
numberOfDigits := 0
for !(digit < 1) {
numberOfDigits++
digit = digit / 10
}
if numberOfDigits%2 == 0 {
even++
}
}
return even
}
// sortedSquares Problem: Squares of a Sorted Array.
//
// LeetCode: https://leetcode.com/explore/learn/card/fun-with-arrays/521/introduction/3240/
func sortedSquares(nums []int) []int {
newArr := make([]int, len(nums))
for i := 0; i < len(nums); i++ {
newArr[i] = nums[i] * nums[i]
}
if len(newArr) == 1 {
return newArr
}
// Buble sort implementation
for i := 1; i < len(newArr); i++ {
f := 0
for k := 0; k < len(newArr)-i; k++ {
if newArr[k] > newArr[k+1] {
tmp := newArr[k+1]
newArr[k+1] = newArr[k]
newArr[k] = tmp
f = 1
}
}
if f == 0 {
break
}
}
return newArr
}