GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/Recursion/679.24-Game at master · wisdompeak/LeetCode

Expand file tree

/

679.24-Game

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

679.24-Game

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
679.24-Game_v1.cpp
679.24-Game_v1.cpp
 
 
679.24-Game_v2.cpp
679.24-Game_v2.cpp
 
 
679.24-Game_v3.cpp
679.24-Game_v3.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

679.24-Game

此题和LC 241 Different Ways to Add Parentheses与LC 2019 The Score of Students Solving Math Expression类似的套路。

我们遍历这个四个数字的permutaion。对于任意一种permuation A B C D,我们本质上需要做的就是任意添加运算符(加减乘除)和括号(改变运算顺序),查看整个式子eval的结果是否包含24.

具体的做法就是递归。我们将某个permutation拆解为两部分分别递归处理,再将两个部分eval出来的结果两两组合,配上加减乘除,就得到对于这个permutation我们所能计算出的所有可能值。大致的代码是

for x: eval(AB)
  for y: eval(CD)
    rets.insert({x+y, x-y, x*y, x/y});

需要注意,所有的牌必须处理成浮点,因为涉及到除法。

Leetcode Link

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.