mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -455,6 +455,45 @@ var restoreIpAddresses = function(s) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## TypeScript
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function isValidIpSegment(str: string): boolean {
|
||||||
|
let resBool: boolean = true;
|
||||||
|
let tempVal: number = Number(str);
|
||||||
|
if (
|
||||||
|
str.length === 0 || isNaN(tempVal) ||
|
||||||
|
tempVal > 255 || tempVal < 0 ||
|
||||||
|
(str.length > 1 && str[0] === '0')
|
||||||
|
) {
|
||||||
|
resBool = false;
|
||||||
|
}
|
||||||
|
return resBool;
|
||||||
|
}
|
||||||
|
function restoreIpAddresses(s: string): string[] {
|
||||||
|
const resArr: string[] = [];
|
||||||
|
backTracking(s, 0, []);
|
||||||
|
return resArr;
|
||||||
|
function backTracking(s: string, startIndex: number, route: string[]): void {
|
||||||
|
let length: number = s.length;
|
||||||
|
if (route.length === 4 && startIndex >= length) {
|
||||||
|
resArr.push(route.join('.'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (route.length === 4 || startIndex >= length) return;
|
||||||
|
let tempStr: string = '';
|
||||||
|
for (let i = startIndex + 1; i <= Math.min(length, startIndex + 3); i++) {
|
||||||
|
tempStr = s.slice(startIndex, i);
|
||||||
|
if (isValidIpSegment(tempStr)) {
|
||||||
|
route.push(s.slice(startIndex, i));
|
||||||
|
backTracking(s, i, route);
|
||||||
|
route.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
## Go
|
## Go
|
||||||
|
|
||||||
回溯(对于前导 0的IP(特别注意s[startIndex]=='0'的判断,不应该写成s[startIndex]==0,因为s截取出来不是数字))
|
回溯(对于前导 0的IP(特别注意s[startIndex]=='0'的判断,不应该写成s[startIndex]==0,因为s截取出来不是数字))
|
||||||
|
@ -185,6 +185,36 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
双指针:
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public static boolean backspaceCompare(String s, String t) {
|
||||||
|
char[] sarray = s.toCharArray();
|
||||||
|
char[] tarray = t.toCharArray();
|
||||||
|
return generate(sarray).equals(generate(tarray));
|
||||||
|
}
|
||||||
|
public static String generate(char[] a){
|
||||||
|
int slow = -1;
|
||||||
|
int fast = 0;
|
||||||
|
if(a.length == 1){
|
||||||
|
return new String(a);
|
||||||
|
} else{
|
||||||
|
for(fast = 0; fast < a.length; fast++){
|
||||||
|
if(a[fast] != '#')
|
||||||
|
a[++slow] = a[fast];
|
||||||
|
else{
|
||||||
|
if(slow >= 0)
|
||||||
|
slow--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new String(a,0,slow + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### python
|
### python
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user