【LeetCode刷题】机器人走路最大距离:妙解

这题主要学习巧用C++语言,内联函数、结构体排序、C++书写方式

  • static int fast_streams = []() {
  • std::ios::sync_with_stdio(false);
  • std::cin.tie(nullptr);
  • std::cout.tie(nullptr);
  • return 0;
  • }();
  • struct Int2 { int x, y; };
  • inline bool operator==(Int2 a, Int2 b) { return a.x == b.x && a.y == b.y; }
  • inline Int2 operator+(Int2 a, Int2 b) { return {a.x + b.x, a.y + b.y}; }
  • inline bool isFree(Int2 p, const vector<Int2>& obstacles) {
  • for (Int2 o : obstacles) {
  • if (p == o)
  • return false;
  • }
  • return true;
  • }
  • inline Int2 turnLeft(Int2 h) { return {-h.y, h.x}; }
  • inline Int2 turnRight(Int2 h) { return {h.y, -h.x}; }
  • class Solution {
  • public:
  • int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
  • const int division = 50;
  • vector<vector<Int2>> obs(60001 / division);
  • for (const vector<int>& o : obstacles)
  • obs[(o[0] + 30000) / division].push_back(Int2{o[0], o[1]});
  • Int2 p = {0, 0};
  • Int2 h = {0, 1};
  • int maxDistance = 0;
  • for (int command : commands) {
  • if (command == -2)
  • h = turnLeft(h);
  • else if (command == -1)
  • h = turnRight(h);
  • else {
  • for (int step = 1; step <= command; ++step) {
  • const Int2 n = p + h;
  • if (isFree(n, obs[(n.x + 30000) / division])) {
  • p = n;
  • maxDistance = max(maxDistance, p.x * p.x + p.y * p.y);
  • }
  • else
  • break;
  • }
  • }
  • }
  • return maxDistance;
  • }
  • };  

    来自 <http://www.planetb.ca/projects/syntaxHighlighter/popup.php>

原文地址:https://www.cnblogs.com/xukaiae86/p/12047328.html

时间: 2024-11-06 10:02:12

【LeetCode刷题】机器人走路最大距离:妙解的相关文章

leetcode 刷题之路 94 N-Queens

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of

leetcode 刷题之路 95 N-Queens I

Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. N皇后问题的变种,要求直接输出N皇后的解法数目.这道题可以在N-Queens I的基础上增加计数功能,在每求得一个成功的解时(行数为N时)使计数变量递增即可.题目不要求输出具体的解法,因此可以做一点优化,使用position数组用来表示皇后的位置,p

【leetcode刷题笔记】Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题解: 所谓的anagrams,只若干个词,它们包含的字母的个数和种类完全一样,只是字符的顺序不一样.比如说bus,usb,sub就是一组angrams.同一组angrams具有排序后相同的特点,比如对上述三个单词按字典序排序分别得到bsu,bsu,bsu.我们用这一点

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t