Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 15 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
9ac50a9
chore: Add new solution to LeetCode 203
tan-i-ham May 16, 2022
08147d1
添加 0232.用栈实现队列.md Scala版本
wzqwtt May 16, 2022
f4d98a7
添加 0225.用队列实现栈.md Scala版本
wzqwtt May 16, 2022
8df1b4b
Update 0739.每日温度.md
huangyebiaoke May 16, 2022
61f5d92
添加 0020.有效的括号.md Scala版本
wzqwtt May 16, 2022
98bdccb
添加 1047.删除字符串中的所有相邻重复项.md Scala版本
wzqwtt May 16, 2022
3493833
增加KMP php版本代码
fmtvar May 17, 2022
0281b82
增加php版本
fmtvar May 16, 2022
bdb357e
Merge pull request #1337 from tan-i-ham/master
youngyangyang04 Jun 6, 2022
17a818c
Merge pull request #1338 from wzqwtt/patch13
youngyangyang04 Jun 6, 2022
41eaab2
Merge branch 'master' into offer58-2
youngyangyang04 Jun 6, 2022
c53082e
Merge pull request #1339 from fmtvar/offer58-2
youngyangyang04 Jun 6, 2022
49a5d83
Merge pull request #1340 from huangyebiaoke/master
youngyangyang04 Jun 6, 2022
c054218
Merge pull request #1341 from wzqwtt/patch14
youngyangyang04 Jun 6, 2022
6d5cb71
Merge pull request #1342 from fmtvar/0028
youngyangyang04 Jun 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions problems/0203.移除链表元素.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,27 @@ public ListNode removeElements(ListNode head, int val) {
}
return head;
}
/**
* 不添加虚拟节点and pre Node方式
* 时间复杂度 O(n)
* 空间复杂度 O(1)
* @param head
* @param val
* @return
*/
public ListNode removeElements(ListNode head, int val) {
while(head!=null && head.val==val){
head = head.next;
}
ListNode curr = head;
while(curr!=null){
while(curr.next!=null && curr.next.val == val){
curr.next = curr.next.next;
}
curr = curr.next;
}
return head;
}
```

Python:
Expand Down
83 changes: 83 additions & 0 deletions problems/0225.用队列实现栈.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,89 @@ class MyStack {
}
}
```
Scala:

使用两个队列模拟栈:
```scala
import scala.collection.mutable

class MyStack() {

val queue1 = new mutable.Queue[Int]()
val queue2 = new mutable.Queue[Int]()

def push(x: Int) {
queue1.enqueue(x)
}

def pop(): Int = {
var size = queue1.size
// 将queue1中的每个元素都移动到queue2
for (i <- 0 until size - 1) {
queue2.enqueue(queue1.dequeue())
}
var res = queue1.dequeue()
// 再将queue2中的每个元素都移动到queue1
while (!queue2.isEmpty) {
queue1.enqueue(queue2.dequeue())
}
res
}

def top(): Int = {
var size = queue1.size
for (i <- 0 until size - 1) {
queue2.enqueue(queue1.dequeue())
}
var res = queue1.dequeue()
while (!queue2.isEmpty) {
queue1.enqueue(queue2.dequeue())
}
// 最终还需要把res送进queue1
queue1.enqueue(res)
res
}

def empty(): Boolean = {
queue1.isEmpty
}
}
```
使用一个队列模拟:
```scala
import scala.collection.mutable

class MyStack() {

val queue = new mutable.Queue[Int]()

def push(x: Int) {
queue.enqueue(x)
}

def pop(): Int = {
var size = queue.size
for (i <- 0 until size - 1) {
queue.enqueue(queue.head) // 把头添到队列最后
queue.dequeue() // 再出队
}
queue.dequeue()
}

def top(): Int = {
var size = queue.size
var res = 0
for (i <- 0 until size) {
queue.enqueue(queue.head) // 把头添到队列最后
res = queue.dequeue() // 再出队
}
res
}

def empty(): Boolean = {
queue.isEmpty
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
41 changes: 40 additions & 1 deletion problems/0232.用栈实现队列.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,45 @@ void myQueueFree(MyQueue* obj) {
obj->stackOutTop = 0;
}
```

Scala:
```scala
class MyQueue() {
import scala.collection.mutable
val stackIn = mutable.Stack[Int]() // 负责出栈
val stackOut = mutable.Stack[Int]() // 负责入栈

// 添加元素
def push(x: Int) {
stackIn.push(x)
}

// 复用代码,如果stackOut为空就把stackIn的所有元素都压入StackOut
def dumpStackIn(): Unit = {
if (!stackOut.isEmpty) return
while (!stackIn.isEmpty) {
stackOut.push(stackIn.pop())
}
}

// 弹出元素
def pop(): Int = {
dumpStackIn()
stackOut.pop()
}

// 获取队头
def peek(): Int = {
dumpStackIn()
val res: Int = stackOut.pop()
stackOut.push(res)
res
}

// 判断是否为空
def empty(): Boolean = {
stackIn.isEmpty && stackOut.isEmpty
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
5 changes: 5 additions & 0 deletions problems/剑指Offer58-II.左旋转字符串.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) {
}
```


### PHP

```php
Expand All @@ -310,6 +311,9 @@ function reverse(&$s, $start, $end) {
}
```


Scala:

```scala
object Solution {
def reverseLeftWords(s: String, n: Int): String = {
Expand Down Expand Up @@ -340,5 +344,6 @@ object Solution {




-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

AltStyle によって変換されたページ (->オリジナル) /