Skip to content

150. Evaluate Reverse Polish Notation#9

Open
yamashita-ki wants to merge 1 commit intomainfrom
leetcode/150
Open

150. Evaluate Reverse Polish Notation#9
yamashita-ki wants to merge 1 commit intomainfrom
leetcode/150

Conversation

@yamashita-ki
Copy link
Owner

https://leetcode.com/problems/evaluate-reverse-polish-notation/description/

You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.

Evaluate the expression. Return an integer that represents the value of the expression.

Note that:

  • The valid operators are '+', '-', '*', and '/'.
  • Each operand may be an integer or another expression.
  • The division between two integers always truncates toward zero.
  • There will not be any division by zero.
  • The input represents a valid arithmetic expression in a reverse polish notation.
  • The answer and all the intermediate calculations can be represented in a 32-bit integer.


public int evalRPN(String[] tokens) {
ArrayDeque<Integer> numStack = new ArrayDeque<>();
Set<String> operators = new HashSet<>(Arrays.asList("*", "-", "+", "/"));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set.of() のほうがシンプルに書けると思います。
https://docs.oracle.com/javase/jp/11/docs/api/java.base/java/util/Set.html

Set<String> operators = Set.of("*", "-", "+", "/");

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご確認ありがとうございます!
勉強になりますmm

```java
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArrayDeque のほうが高速なため、 ArrayDeque を優先的に使用したほうが良いと思います。

https://docs.oracle.com/javase/jp/8/docs/api/java/util/ArrayDeque.html

このクラスは通常、スタックとして使われるときはStackよりも高速で、キューとして使われるときはLinkedListよりも高速です。

Stack<Integer> stack = new Stack<>();
for (String c : tokens) {
if (c.equals("+")) {
stack.push(stack.pop() + stack.pop());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的には step1 のように 4 種類の演算を同じようなロジックで書いたほうが、読み手にとってパターンマッチングで読めるので読みやすく、かつ書き間違えにくいと思います。

return dfs(tokenList);
}

private int dfs(List<String> tokens) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

関数名には、内部の実装の詳細を表す言葉より、入力に対してどのような処理を行うのか、どのような値を返すかが分かるものを付けたほうが、読み手にとって分かりやすいと思います。
parse() はいかがでしょうか?これなら、入力データに対して構文解析を行い、その結果を返すことが読み手に伝わりやすいと思います。

ArrayDeque<Integer> stack = new ArrayDeque<>();
Set<String> operators = new HashSet<>(Arrays.asList("-", "+", "*", "/"));

for(String token : tokens) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for 文の後にスペースを空けることをおすすめします。

参考までにスタイルガイドへのリンクを貼ります。

https://google.github.io/styleguide/javaguide.html#s4.6.2-horizontal-whitespace

Separating any keyword, such as if, for or catch, from an open parenthesis (() that follows it on that line

上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。

if (operators.contains(token)) {
int rightNum = numStack.pollLast();
int leftNum = numStack.pollLast();
numStack.add(this.calculateFromOperator(leftNum, rightNum, token));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メソッド呼び出しの this. は省略してよいと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants