-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday20.java
More file actions
86 lines (66 loc) · 1.85 KB
/
day20.java
File metadata and controls
86 lines (66 loc) · 1.85 KB
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
//ques1:451. Sort Characters By Frequency
//link:https://leetcode.com/problems/sort-characters-by-frequency/description/
class Solution
{
public String frequencySort(String s)
{
StringBuilder sb = new StringBuilder();
int[] freq = new int[128];
for(char c : s.toCharArray())
{
freq[c-'0']++;
}
for(int k = 0; k < s.length(); k++)
{
int max = 0;
int ind = 0;
for(int j = 0; j < freq.length; j++)
{
if(freq[j] != 0 && freq[j] > max)
{
max = freq[j];
ind = j;
}
}
for(int i = 0; i < max; i++)
{
sb.append((char)('0'+ind));
}
freq[ind] = 0;
k += --max;
}
return sb.toString();
}
}
//TC:O(n)
//SC:O(1)
//ques2:8. String to Integer (atoi)
//link:https://leetcode.com/problems/string-to-integer-atoi/description/
class Solution2 {
public int myAtoi(String s) {
int i = 0;
int sign = 1;
int result = 0;
while (i < s.length() && s.charAt(i) == ' ') {
i++;
}
if (i < s.length() && (s.charAt(i) == '+' || s.charAt(i) == '-')) {
sign = (s.charAt(i) == '-') ? -1 : 1;
i++;
}
while (i < s.length()) {
char ch = s.charAt(i);
if (ch < '0' || ch > '9') {
break;
}
if (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && ch - '0' > Integer.MAX_VALUE % 10)) {
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
result = result * 10 + (ch - '0');
i++;
}
return result * sign;
}
}
//TC:O(n)
//SC:O(1)