Merge branch 'youngyangyang04:master' into master

This commit is contained in:
极客学伟
2021-09-01 11:25:10 +08:00
committed by GitHub
6 changed files with 252 additions and 1 deletions

View File

@ -221,6 +221,30 @@ class SolutionDP2:
```
Go
```go
func numDistinct(s string, t string) int {
dp:= make([][]int,len(s)+1)
for i:=0;i<len(dp);i++{
dp[i] = make([]int,len(t)+1)
}
// 初始化
for i:=0;i<len(dp);i++{
dp[i][0] = 1
}
// dp[0][j] 为 0默认值因此不需要初始化
for i:=1;i<len(dp);i++{
for j:=1;j<len(dp[i]);j++{
if s[i-1] == t[j-1]{
dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
}else{
dp[i][j] = dp[i-1][j]
}
}
}
return dp[len(dp)-1][len(dp[0])-1]
}
```
Javascript:
```javascript

View File

@ -467,6 +467,85 @@ function reverse(strArr, start, end) {
}
```
Swift:
```swift
func reverseWords(_ s: String) -> String {
var stringArr = removeSpace(s)
reverseString(&stringArr, startIndex: 0, endIndex: stringArr.count - 1)
reverseWord(&stringArr)
return String(stringArr)
}
/// 1、移除多余的空格前后所有的空格中间只留一个空格
func removeSpace(_ s: String) -> [Character] {
let ch = Array(s)
var left = 0
var right = ch.count - 1
// 忽略字符串前面的所有空格
while ch[left] == " " {
left += 1
}
// 忽略字符串后面的所有空格
while ch[right] == " " {
right -= 1
}
// 接下来就是要处理中间的多余空格
var lastArr = Array<Character>()
while left <= right {
// 准备加到新字符串当中的字符
let char = ch[left]
// 新的字符串的最后一个字符;或者原字符串中,准备加到新字符串的那个字符;这两个字符当中,只要有一个不是空格,就可以加到新的字符串当中
if char != " " || lastArr[lastArr.count - 1] != " " {
lastArr.append(char)
}
left += 1
}
return lastArr
}
/// 2、反转整个字符串
func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) {
var start = startIndex
var end = endIndex
while start < end {
(s[start], s[end]) = (s[end], s[start])
start += 1
end -= 1
}
}
/// 3、再次将字符串里面的单词反转
func reverseWord(_ s: inout [Character]) {
var start = 0
var end = 0
var entry = false
for i in 0..<s.count {
if !entry {
start = i
entry = true
}
if entry && s[i] == " " && s[i - 1] != " " {
end = i - 1
entry = false
reverseString(&s, startIndex: start, endIndex: end)
}
if entry && (i == s.count - 1) && s[i] != " " {
end = i
entry = false
reverseString(&s, startIndex: start, endIndex: end)
}
}
}
```

View File

@ -144,6 +144,75 @@ public:
## Java
```java
// 方法一,使用数组
class Solution {
public boolean isPalindrome(ListNode head) {
int len = 0;
// 统计链表长度
ListNode cur = head;
while (cur != null) {
len++;
cur = cur.next;
}
cur = head;
int[] res = new int[len];
// 将元素加到数组之中
for (int i = 0; i < res.length; i++){
res[i] = cur.val;
cur = cur.next;
}
// 比较回文
for (int i = 0, j = len - 1; i < j; i++, j--){
if (res[i] != res[j]){
return false;
}
}
return true;
}
}
// 方法二,快慢指针
class Solution {
public boolean isPalindrome(ListNode head) {
// 如果为空或者仅有一个节点返回true
if (head == null && head.next == null) return true;
ListNode slow = head;
ListNode fast = head;
ListNode pre = head;
while (fast != null && fast.next != null){
pre = slow; // 记录slow的前一个结点
slow = slow.next;
fast = fast.next.next;
}
pre.next = null; // 分割两个链表
// 前半部分
ListNode cur1 = head;
// 后半部分。这里使用了反转链表
ListNode cur2 = reverseList(slow);
while (cur1 != null){
if (cur1.val != cur2.val) return false;
// 注意要移动两个结点
cur1 = cur1.next;
cur2 = cur2.next;
}
return true;
}
ListNode reverseList(ListNode head){
// 反转链表
ListNode tmp = null;
ListNode pre = null;
while (head != null){
tmp = head.next;
head.next = pre;
pre = head;
head = tmp;
}
return pre;
}
}
```
## Python
@ -209,11 +278,13 @@ class Solution:
## Go
```go
```
## JavaScript
```js
```

View File

@ -203,6 +203,25 @@ const isSubsequence = (s, t) => {
};
```
Go
```go
func isSubsequence(s string, t string) bool {
dp := make([][]int,len(s)+1)
for i:=0;i<len(dp);i++{
dp[i] = make([]int,len(t)+1)
}
for i:=1;i<len(dp);i++{
for j:=1;j<len(dp[i]);j++{
if s[i-1] == t[j-1]{
dp[i][j] = dp[i-1][j-1] +1
}else{
dp[i][j] = dp[i][j-1]
}
}
}
return dp[len(s)][len(t)]==len(s)
}
```

View File

@ -147,8 +147,38 @@ class Solution:
```
Go
```go
func minDistance(word1 string, word2 string) int {
dp := make([][]int, len(word1)+1)
for i := 0; i < len(dp); i++ {
dp[i] = make([]int, len(word2)+1)
}
//初始化
for i := 0; i < len(dp); i++ {
dp[i][0] = i
}
for j := 0; j < len(dp[0]); j++ {
dp[0][j] = j
}
for i := 1; i < len(dp); i++ {
for j := 1; j < len(dp[i]); j++ {
if word1[i-1] == word2[j-1] {
dp[i][j] = dp[i-1][j-1]
} else {
dp[i][j] = min(min(dp[i-1][j]+1, dp[i][j-1]+1), dp[i-1][j-1]+2)
}
}
}
return dp[len(dp)-1][len(dp[0])-1]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
```
Javascript
```javascript
const minDistance = (word1, word2) => {

View File

@ -214,6 +214,34 @@ var reverseLeftWords = function (s, n) {
};
```
Swift:
```swift
func reverseLeftWords(_ s: String, _ n: Int) -> String {
var ch = Array(s)
let len = ch.count
// 反转区间[0, n - 1]
reverseString(&ch, startIndex: 0, endIndex: n - 1)
// 反转区间[n, len - 1]
reverseString(&ch, startIndex: n, endIndex: len - 1)
// 反转区间[0, len - 1],也就是整个字符串反转
reverseString(&ch, startIndex: 0, endIndex: len - 1)
return String(ch)
}
func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) {
var start = startIndex
var end = endIndex
while start < end {
(s[start], s[end]) = (s[end], s[start])
start += 1
end -= 1
}
}
```