Merge pull request #78 from LehiChiang/master

添加700.二叉搜索树中的搜索 Java/Python版本;添加701.二叉搜索树中的插入操作 Java/Python版本;添加0704.二分查找Java版本;添加1143.最长公共子序列 Java/Python版本
This commit is contained in:
Carl Sun
2021-05-14 09:26:28 +08:00
committed by GitHub
5 changed files with 170 additions and 12 deletions

View File

@ -147,6 +147,7 @@ C++测试用例有超过两个树相加超过int的数据所以需要在if里
Java
```Java
class Solution {
public int combinationSum4(int[] nums, int target) {
@ -163,10 +164,23 @@ class Solution {
}
}
```
Python
```python
class Solution:
def combinationSum4(self, nums, target):
dp = [0] * (target + 1)
dp[0] = 1
for i in range(1, target+1):
for j in nums:
if i >= j:
dp[i] += dp[i - j]
return dp[-1]
```
Go
@ -177,4 +191,4 @@ Go
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

View File

@ -140,21 +140,61 @@ public:
## 其他语言版本
Java
递归法:
```java
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) return null;
if (root.val == val) return root;
if (root.val > val) return searchBST(root.left, val);
return searchBST(root.right, val);
else if (root.val > val) return searchBST(root.left, val);
else return searchBST(root.right, val);
}
}
```
迭代法:
```java
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
while (root != null)
if (val < root.val) root = root.left;
else if (val > root.val) root = root.right;
else return root;
return root;
}
}
```
Python
递归法:
```python
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
if root is None:
return None
if val < root.val: return self.searchBST(root.left, val)
elif val > root.val: return self.searchBST(root.right, val)
else: return root
```
迭代法:
```python
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
while root is not None:
if val < root.val: root = root.left
elif val > root.val: root = root.right
else: return root
return root
```
Go

View File

@ -206,7 +206,6 @@ public:
## 其他语言版本
Java
```java
class Solution {
@ -233,9 +232,43 @@ class Solution {
}
```
递归法
```java
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
return buildTree(root, val);
}
public TreeNode buildTree(TreeNode root, int val){
if (root == null) // 如果当前节点为空也就意味着val找到了合适的位置此时创建节点直接返回。
return new TreeNode(val);
if (root.val < val){
root.right = buildTree(root.right, val); // 递归创建右子树
}else if (root.val > val){
root.left = buildTree(root.left, val); // 递归创建左子树
}
return root;
}
}
```
Python
递归法
```python
class Solution:
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
if root is None:
return TreeNode(val) # 如果当前节点为空也就意味着val找到了合适的位置此时创建节点直接返回。
if root.val < val:
root.right = self.insertIntoBST(root.right, val) # 递归创建右子树
if root.val > val:
root.left = self.insertIntoBST(root.left, val) # 递归创建左子树
return root
```
Go

View File

@ -23,7 +23,7 @@
输入: nums = [-1,0,3,5,9,12], target = 2
输出: -1
解释: 2 不存在 nums 中因此返回 -1
 
提示:
* 你可以假设 nums 中的所有元素是不重复的。
@ -146,11 +146,50 @@ public:
## 其他语言版本
Java
(版本一)左闭右闭区间
```java
class Solution {
public int search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] > target)
right = mid - 1;
}
return -1;
}
}
```
(版本二)左闭右开区间
```java
class Solution {
public int search(int[] nums, int target) {
int left = 0, right = nums.length;
while (left < right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] > target)
right = mid;
}
return -1;
}
}
```
Python
```python3
class Solution:
def search(self, nums: List[int], target: int) -> int:
@ -178,4 +217,4 @@ Go
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

View File

@ -31,7 +31,7 @@
输入text1 = "abc", text2 = "def"
输出0
解释:两个字符串没有公共子序列,返回 0。
 
提示:
* 1 <= text1.length <= 1000
* 1 <= text2.length <= 1000
@ -126,12 +126,44 @@ public:
## 其他语言版本
Java
```java
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int[][] dp = new int[text1.length() + 1][text2.length() + 1]; // 先对dp数组做初始化操作
for (int i = 1 ; i <= text1.length() ; i++) {
char char1 = text1.charAt(i - 1);
for (int j = 1; j <= text2.length(); j++) {
char char2 = text2.charAt(j - 1);
if (char1 == char2) { // 开始列出状态转移方程
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[text1.length()][text2.length()];
}
}
```
Python
```python
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
len1, len2 = len(text1)+1, len(text2)+1
dp = [[0 for _ in range(len1)] for _ in range(len2)] # 先对dp数组做初始化操作
for i in range(1, len2):
for j in range(1, len1): # 开始列出状态转移方程
if text1[j-1] == text2[i-1]:
dp[i][j] = dp[i-1][j-1]+1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
return dp[-1][-1]
```
Go
@ -142,4 +174,4 @@ Go
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>