Skip to content

Commit

Permalink
😁 stu
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed Mar 25, 2019
1 parent c7d6d17 commit 06479cc
Show file tree
Hide file tree
Showing 5 changed files with 452 additions and 0 deletions.
130 changes: 130 additions & 0 deletions 150.evaluate-reverse-polish-notation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@


## 题目地址
https://leetcode.com/problems/evaluate-reverse-polish-notation/description/

## 题目描述

```
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Note:
Division between two integers should truncate toward zero.
The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.
```
## 思路
逆波兰表达式又叫做后缀表达式。在通常的表达式中,二元运算符总是置于与之相关的两个运算对象之间,这种表示法也称为`中缀表示`

波兰逻辑学家J.Lukasiewicz于1929年提出了另一种表示表达式的方法,按此方法,每一运算符都置于其运算对象之后,故称为`后缀表示`

> 逆波兰表达式是一种十分有用的表达式,它将复杂表达式转换为可以依靠简单的操作得到计算结果的表达式。例如(a+b)*(c+d)转换为ab+cd+*

## 关键点

1. 栈的基本用法

2. 如果你用的是JS的话,需要注意/ 和 其他很多语言是不一样的

3. 如果你用的是JS的话,需要先将字符串转化为数字。否则有很多意想不到的结果

4. 操作符的顺序应该是 先出栈的是第二位,后出栈的是第一位。 这在不符合交换律的操作中很重要, 比如减法和除法。

## 代码

```js
/*
* @lc app=leetcode id=150 lang=javascript
*
* [150] Evaluate Reverse Polish Notation
*
* https://leetcode.com/problems/evaluate-reverse-polish-notation/description/
*
* algorithms
* Medium (31.43%)
* Total Accepted: 153.3K
* Total Submissions: 485.8K
* Testcase Example: '["2","1","+","3","*"]'
*
* Evaluate the value of an arithmetic expression in Reverse Polish Notation.
*
* Valid operators are +, -, *, /. Each operand may be an integer or another
* expression.
*
* Note:
*
*
* Division between two integers should truncate toward zero.
* The given RPN expression is always valid. That means the expression would
* always evaluate to a result and there won't be any divide by zero
* operation.
*
*
* Example 1:
*
*
* Input: ["2", "1", "+", "3", "*"]
* Output: 9
* Explanation: ((2 + 1) * 3) = 9
*
*
* Example 2:
*
*
* Input: ["4", "13", "5", "/", "+"]
* Output: 6
* Explanation: (4 + (13 / 5)) = 6
*
*
* Example 3:
*
*
* Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
* Output: 22
* Explanation:
* ⁠ ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
* = ((10 * (6 / (12 * -11))) + 17) + 5
* = ((10 * (6 / -132)) + 17) + 5
* = ((10 * 0) + 17) + 5
* = (0 + 17) + 5
* = 17 + 5
* = 22
*
*
*/
/**
* @param {string[]} tokens
* @return {number}
*/
var evalRPN = function(tokens) {
const stack = [];

for (let index = 0; index < tokens.length; index++) {
const token = tokens[index];
if (!Number.isNaN(Number(token))) {
stack.push(token);
} else {
const a = Number(stack.pop());
const b = Number(stack.pop());
if (token === "*") {
stack.push(a * b);
} else if (token === "/") {
stack.push(b / a > 0 ? Math.floor(b / a) : Math.ceil(b / a));
} else if (token === "+") {
stack.push(a + b);
} else if (token === "-") {
stack.push(b - a);
}
}
}

return stack.pop();
};

```



100 changes: 100 additions & 0 deletions 203.remove-linked-list-elements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
## 题目地址
https://leetcode.com/problems/remove-linked-list-elements/description/

## 题目描述
```
Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
```

## 思路
这个一个链表基本操作的题目,思路就不多说了。
## 关键点解析

- 链表的基本操作(删除指定节点)
- 虚拟节点dummy 简化操作

> 其实设置dummy节点就是为了处理特殊位置(头节点),这这道题就是如果头节点是给定的需要删除的节点呢?
为了保证代码逻辑的一致性,即不需要为头节点特殊定制逻辑,才采用的虚拟节点。

- 如果连续两个节点都是要删除的节点,这个情况容易被忽略。
eg:

```js
// 只有下个节点不是要删除的节点才更新current
if (!next || next.val !== val) {
current = next;
}

```


## 代码

```js



/*
* @lc app=leetcode id=203 lang=javascript
*
* [203] Remove Linked List Elements
*
* https://leetcode.com/problems/remove-linked-list-elements/description/
*
* algorithms
* Easy (35.32%)
* Total Accepted: 211.9K
* Total Submissions: 598.6K
* Testcase Example: '[1,2,6,3,4,5,6]\n6'
*
* Remove all elements from a linked list of integers that have value val.
*
* Example:
*
*
* Input: 1->2->6->3->4->5->6, val = 6
* Output: 1->2->3->4->5
*
*
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} val
* @return {ListNode}
*/
var removeElements = function(head, val) {
const dummy = {
next: head
}
let current = dummy;

while(current && current.next) {
let next = current.next;
if (next.val === val) {
current.next = next.next;
next = next.next;
}

if (!next || next.val !== val) {
current = next;
}
}

return dummy.next;
};

```

102 changes: 102 additions & 0 deletions 219.contains-duplicate-ii.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

## 题目地址
https://leetcode.com/problems/contains-duplicate-ii/description/

## 题目描述


```
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
```

## 思路

由于题目没有对空间复杂度有求,用一个hashmap 存储已经访问过的数字即可,
每次访问都会看hashmap中是否有这个元素,有的话拿出索引进行比对,是否满足条件(相隔不大于k),如果满足返回true即可。


## 关键点解析



## 代码

```js
/*
* @lc app=leetcode id=219 lang=javascript
*
* [219] Contains Duplicate II
*
* https://leetcode.com/problems/contains-duplicate-ii/description/
*
* algorithms
* Easy (34.75%)
* Total Accepted: 187.3K
* Total Submissions: 537.5K
* Testcase Example: '[1,2,3,1]\n3'
*
* Given an array of integers and an integer k, find out whether there are two
* distinct indices i and j in the array such that nums[i] = nums[j] and the
* absolute difference between i and j is at most k.
*
*
* Example 1:
*
*
* Input: nums = [1,2,3,1], k = 3
* Output: true
*
*
*
* Example 2:
*
*
* Input: nums = [1,0,1,1], k = 1
* Output: true
*
*
*
* Example 3:
*
*
* Input: nums = [1,2,3,1,2,3], k = 2
* Output: false
*
*
*
*
*
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var containsNearbyDuplicate = function(nums, k) {
const visited = {};
for(let i = 0; i < nums.length; i++) {
const num = nums[i];
if (visited[num] !== undefined && i - visited[num] <= k) {
return true;
}
visited[num] = i;
}
return false
};
```

38 changes: 38 additions & 0 deletions 279.perfect-squares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* @lc app=leetcode id=279 lang=javascript
*
* [279] Perfect Squares
*
* https://leetcode.com/problems/perfect-squares/description/
*
* algorithms
* Medium (40.98%)
* Total Accepted: 168.2K
* Total Submissions: 408.5K
* Testcase Example: '12'
*
* Given a positive integer n, find the least number of perfect square numbers
* (for example, 1, 4, 9, 16, ...) which sum to n.
*
* Example 1:
*
*
* Input: n = 12
* Output: 3
* Explanation: 12 = 4 + 4 + 4.
*
* Example 2:
*
*
* Input: n = 13
* Output: 2
* Explanation: 13 = 4 + 9.
*/
/**
* @param {number} n
* @return {number}
*/
var numSquares = function(n) {

};

Loading

0 comments on commit 06479cc

Please sign in to comment.