mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@ -270,6 +270,26 @@ func searchInsert(nums []int, target int) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
|
||||||
|
let mut left = 0;
|
||||||
|
let mut right = nums.len();
|
||||||
|
while left < right {
|
||||||
|
let mid = (left + right) / 2;
|
||||||
|
match nums[mid].cmp(&target) {
|
||||||
|
Ordering::Less => left = mid + 1,
|
||||||
|
Ordering::Equal => return ((left + right) / 2) as i32,
|
||||||
|
Ordering::Greater => right = mid,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
((left + right) / 2) as i32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
|
@ -368,7 +368,6 @@ class Solution {
|
|||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
|
||||||
Python2:
|
|
||||||
```python
|
```python
|
||||||
class Solution(object):
|
class Solution(object):
|
||||||
def combine(self, n, k):
|
def combine(self, n, k):
|
||||||
|
@ -234,11 +234,13 @@ Java:
|
|||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
public boolean wordBreak(String s, List<String> wordDict) {
|
public boolean wordBreak(String s, List<String> wordDict) {
|
||||||
|
HashSet<String> set = new HashSet<>(wordDict);
|
||||||
boolean[] valid = new boolean[s.length() + 1];
|
boolean[] valid = new boolean[s.length() + 1];
|
||||||
valid[0] = true;
|
valid[0] = true;
|
||||||
|
|
||||||
for (int i = 1; i <= s.length(); i++) {
|
for (int i = 1; i <= s.length(); i++) {
|
||||||
for (int j = 0; j < i; j++) {
|
for (int j = 0; j < i && !valid[i]; j++) {
|
||||||
if (wordDict.contains(s.substring(j,i)) && valid[j]) {
|
if (set.contains(s.substring(j, i)) && valid[j]) {
|
||||||
valid[i] = true;
|
valid[i] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -248,6 +250,26 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 另一种思路的背包算法
|
||||||
|
class Solution {
|
||||||
|
public boolean wordBreak(String s, List<String> wordDict) {
|
||||||
|
boolean[] dp = new boolean[s.length() + 1];
|
||||||
|
dp[0] = true;
|
||||||
|
|
||||||
|
for (int i = 1; i <= s.length(); i++) {
|
||||||
|
for (String word : wordDict) {
|
||||||
|
int len = word.length();
|
||||||
|
if (i >= len && dp[i - len] && word.equals(s.substring(i - len, i))) {
|
||||||
|
dp[i] = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dp[s.length()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 回溯法+记忆化
|
// 回溯法+记忆化
|
||||||
class Solution {
|
class Solution {
|
||||||
private Set<String> set;
|
private Set<String> set;
|
||||||
@ -285,7 +307,7 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
```python3
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
|
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
|
||||||
'''排列'''
|
'''排列'''
|
||||||
|
@ -220,7 +220,8 @@ class Solution:
|
|||||||
|
|
||||||
(版本二)左闭右开区间
|
(版本二)左闭右开区间
|
||||||
|
|
||||||
```class Solution:
|
```python
|
||||||
|
class Solution:
|
||||||
def search(self, nums: List[int], target: int) -> int:
|
def search(self, nums: List[int], target: int) -> int:
|
||||||
if nums is None or len(nums)==0:
|
if nums is None or len(nums)==0:
|
||||||
return -1
|
return -1
|
||||||
@ -516,6 +517,7 @@ impl Solution {
|
|||||||
|
|
||||||
**C:**
|
**C:**
|
||||||
```c
|
```c
|
||||||
|
// (版本一) 左闭右闭区间 [left, right]
|
||||||
int search(int* nums, int numsSize, int target){
|
int search(int* nums, int numsSize, int target){
|
||||||
int left = 0;
|
int left = 0;
|
||||||
int right = numsSize-1;
|
int right = numsSize-1;
|
||||||
@ -541,6 +543,29 @@ int search(int* nums, int numsSize, int target){
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
```C
|
||||||
|
// (版本二) 左闭右开区间 [left, right)
|
||||||
|
int search(int* nums, int numsSize, int target){
|
||||||
|
int length = numsSize;
|
||||||
|
int left = 0;
|
||||||
|
int right = length; //定义target在左闭右开的区间里,即:[left, right)
|
||||||
|
int middle = 0;
|
||||||
|
while(left < right){ // left == right时,区间[left, right)属于空集,所以用 < 避免该情况
|
||||||
|
int middle = left + (right - left) / 2;
|
||||||
|
if(nums[middle] < target){
|
||||||
|
//target位于(middle , right) 中为保证集合区间的左闭右开性,可等价为[middle + 1,right)
|
||||||
|
left = middle + 1;
|
||||||
|
}else if(nums[middle] > target){
|
||||||
|
//target位于[left, middle)中
|
||||||
|
right = middle ;
|
||||||
|
}else{ // nums[middle] == target ,找到目标值target
|
||||||
|
return middle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//未找到目标值,返回-1
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
**PHP:**
|
**PHP:**
|
||||||
```php
|
```php
|
||||||
|
@ -358,7 +358,7 @@ class MyLinkedList {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ListNode pred = head;
|
ListNode pred = head;
|
||||||
for (int i = 0; i < index - 1; i++) {
|
for (int i = 0; i < index ; i++) {
|
||||||
pred = pred.next;
|
pred = pred.next;
|
||||||
}
|
}
|
||||||
pred.next = pred.next.next;
|
pred.next = pred.next.next;
|
||||||
@ -366,92 +366,101 @@ class MyLinkedList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//双链表
|
//双链表
|
||||||
class MyLinkedList {
|
|
||||||
class ListNode{
|
class ListNode{
|
||||||
int val;
|
int val;
|
||||||
ListNode next,prev;
|
ListNode next,prev;
|
||||||
ListNode(int x) {val = x;}
|
ListNode() {};
|
||||||
|
ListNode(int val){
|
||||||
|
this.val = val;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int size;
|
|
||||||
ListNode head,tail;//Sentinel node
|
|
||||||
|
|
||||||
/** Initialize your data structure here. */
|
class MyLinkedList {
|
||||||
|
|
||||||
|
//记录链表中元素的数量
|
||||||
|
int size;
|
||||||
|
//记录链表的虚拟头结点和尾结点
|
||||||
|
ListNode head,tail;
|
||||||
|
|
||||||
public MyLinkedList() {
|
public MyLinkedList() {
|
||||||
size = 0;
|
//初始化操作
|
||||||
head = new ListNode(0);
|
this.size = 0;
|
||||||
tail = new ListNode(0);
|
this.head = new ListNode(0);
|
||||||
|
this.tail = new ListNode(0);
|
||||||
|
//这一步非常关键,否则在加入头结点的操作中会出现null.next的错误!!!
|
||||||
head.next=tail;
|
head.next=tail;
|
||||||
tail.prev=head;
|
tail.prev=head;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
|
|
||||||
public int get(int index) {
|
public int get(int index) {
|
||||||
if(index < 0 || index >= size){return -1;}
|
//判断index是否有效
|
||||||
ListNode cur = head;
|
if(index<0 || index>=size){
|
||||||
|
return -1;
|
||||||
// 通过判断 index < (size - 1) / 2 来决定是从头结点还是尾节点遍历,提高效率
|
}
|
||||||
if(index < (size - 1) / 2){
|
ListNode cur = this.head;
|
||||||
for(int i = 0; i <= index; i++){
|
//判断是哪一边遍历时间更短
|
||||||
cur = cur.next;
|
if(index >= size / 2){
|
||||||
|
//tail开始
|
||||||
|
cur = tail;
|
||||||
|
for(int i=0; i< size-index; i++){
|
||||||
|
cur = cur.prev;
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
cur = tail;
|
for(int i=0; i<= index; i++){
|
||||||
for(int i = 0; i <= size - index - 1; i++){
|
cur = cur.next;
|
||||||
cur = cur.prev;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cur.val;
|
return cur.val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
|
|
||||||
public void addAtHead(int val) {
|
public void addAtHead(int val) {
|
||||||
ListNode cur = head;
|
//等价于在第0个元素前添加
|
||||||
ListNode newNode = new ListNode(val);
|
addAtIndex(0,val);
|
||||||
newNode.next = cur.next;
|
|
||||||
cur.next.prev = newNode;
|
|
||||||
cur.next = newNode;
|
|
||||||
newNode.prev = cur;
|
|
||||||
size++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Append a node of value val to the last element of the linked list. */
|
|
||||||
public void addAtTail(int val) {
|
public void addAtTail(int val) {
|
||||||
ListNode cur = tail;
|
//等价于在最后一个元素(null)前添加
|
||||||
ListNode newNode = new ListNode(val);
|
addAtIndex(size,val);
|
||||||
newNode.next = tail;
|
|
||||||
newNode.prev = cur.prev;
|
|
||||||
cur.prev.next = newNode;
|
|
||||||
cur.prev = newNode;
|
|
||||||
size++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
|
|
||||||
public void addAtIndex(int index, int val) {
|
public void addAtIndex(int index, int val) {
|
||||||
if(index > size){return;}
|
//index大于链表长度
|
||||||
if(index < 0){index = 0;}
|
if(index>size){
|
||||||
ListNode cur = head;
|
return;
|
||||||
for(int i = 0; i < index; i++){
|
}
|
||||||
cur = cur.next;
|
//index小于0
|
||||||
|
if(index<0){
|
||||||
|
index = 0;
|
||||||
}
|
}
|
||||||
ListNode newNode = new ListNode(val);
|
|
||||||
newNode.next = cur.next;
|
|
||||||
cur.next.prev = newNode;
|
|
||||||
newNode.prev = cur;
|
|
||||||
cur.next = newNode;
|
|
||||||
size++;
|
size++;
|
||||||
|
//找到前驱
|
||||||
|
ListNode pre = this.head;
|
||||||
|
for(int i=0; i<index; i++){
|
||||||
|
pre = pre.next;
|
||||||
|
}
|
||||||
|
//新建结点
|
||||||
|
ListNode newNode = new ListNode(val);
|
||||||
|
newNode.next = pre.next;
|
||||||
|
pre.next.prev = newNode;
|
||||||
|
newNode.prev = pre;
|
||||||
|
pre.next = newNode;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Delete the index-th node in the linked list, if the index is valid. */
|
|
||||||
public void deleteAtIndex(int index) {
|
public void deleteAtIndex(int index) {
|
||||||
if(index >= size || index < 0){return;}
|
//判断索引是否有效
|
||||||
ListNode cur = head;
|
if(index<0 || index>=size){
|
||||||
for(int i = 0; i < index; i++){
|
return;
|
||||||
cur = cur.next;
|
|
||||||
}
|
}
|
||||||
cur.next.next.prev = cur;
|
//删除操作
|
||||||
cur.next = cur.next.next;
|
|
||||||
size--;
|
size--;
|
||||||
|
ListNode pre = this.head;
|
||||||
|
for(int i=0; i<index; i++){
|
||||||
|
pre = pre.next;
|
||||||
|
}
|
||||||
|
pre.next.next.prev = pre;
|
||||||
|
pre.next = pre.next.next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -296,7 +296,7 @@ public:
|
|||||||
### Java
|
### Java
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
```
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.result = []
|
self.result = []
|
||||||
|
Reference in New Issue
Block a user