剑指 Offer 20. 表示数值的字符串
class Solution {
public boolean isNumber(String s
) {
if (s
== null
|| s
.length() == 0) return false;
s
= s
.trim();
boolean numFlag
= false;
boolean dotFlag
= false;
boolean eFlag
= false;
for (int i
= 0; i
< s
.length(); i
++) {
if (s
.charAt(i
) >= '0' && s
.charAt(i
) <= '9') {
numFlag
= true;
} else if (s
.charAt(i
) == '.' && !dotFlag
&& !eFlag
) {
dotFlag
= true;
} else if ((s
.charAt(i
) == 'e' || s
.charAt(i
) == 'E') && !eFlag
&& numFlag
) {
eFlag
= true;
numFlag
= false;
} else if ((s
.charAt(i
) == '+' || s
.charAt(i
) == '-') && (i
== 0 || s
.charAt(i
- 1) == 'e' || s
.charAt(i
- 1) == 'E')) {
} else {
return false;
}
}
return numFlag
;
}
}
剑指 Offer 49. 丑数
class Solution {
public int nthUglyNumber(int n
) {
int[] dp
= new int[n
];
dp
[0] = 1;
int id2
= 0,id3
= 0,id5
= 0;
for (int i
= 1; i
< n
; i
++) {
dp
[i
] = Math
.min(Math
.min(2*dp
[id2
],3*dp
[id3
]),5*dp
[id5
]);
if (dp
[id2
]*2 == dp
[i
]){
id2
++;
}
if (dp
[id3
]*3 == dp
[i
]){
id3
++;
}
if (dp
[id5
]*5 == dp
[i
]){
id5
++;
}
}
return dp
[n
-1];
}
}
剑指 Offer 31. 栈的压入、弹出序列
class Solution {
public boolean validateStackSequences(int[] pushed
, int[] popped
) {
Stack
<Integer> stack
= new Stack<>();
int j
= 0;
for(int it
: pushed
){
stack
.push(it
);
while(j
< popped
.length
&& !stack
.isEmpty() && stack
.peek()==popped
[j
]){
stack
.pop();
j
++;
}
}
return j
== popped
.length
;
}
}
你知道的越多,你不知道的越多。
转载请注明原文地址:https://tech.qufami.com/read-832.html