1. 程式人生 > >【LeetCode】Judge Route Circle 判斷路徑是否成環

【LeetCode】Judge Route Circle 判斷路徑是否成環

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

Example 1:
Input: “UD”
Output: true

Example 2:
Input: “LL”
Output: false

/**
 * @param {string} moves
 * @return {boolean}
 */
var judgeCircle = function(moves) {
    var result = moves.match(new RegExp('L', 'g'));
    var countL = !result ? 0 : result.length;
    result = moves.match(new RegExp('R'
, 'g')); var countR = !result ? 0 : result.length; result = moves.match(new RegExp('U', 'g')); var countU = !result ? 0 : result.length; result = moves.match(new RegExp('D', 'g')); var countD = !result ? 0 : result.length; return countL == countR && countU ==countD; };

相關推薦

LeetCodeJudge Route Circle 判斷路徑是否

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it m

[LeetCode] Judge Route Circle 判斷路線繞圈

script initial mov tco view 判斷 lee out clas Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if

Judge Route Circle --判斷圓路線

ive move else class down out posit pre false Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this

LeetCode 657. Judge Route Circle

方式 刷題 這一 pan right 標簽 pla rdquo tar Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robo

LeetCode657 Judge Route Circle

解法 bool judgeCircle(string moves) { int x = 0, y = 0; for(int i = 0; i < moves.len

一次過Lintcode 1104. Judge Route Circle

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves ba

LeetcodeDP-二維陣列 63. Unique Paths II / 不同路徑2(帶障礙)

給定一個二維陣列,每格為0/1值,1代表無法通過。求從左上到右下的不同路徑數。只能往右/下走。 Input: [   [0,0,0],   [0,1,0],   [0,0,0] ] Output: 2 Explanation: There is one obstacl

LeetcodeDP-二維陣列 62. Unique Paths / 不同路徑

給一個形狀為m x n的矩陣,求從左上角到右下角的不同路徑的個數。行進時只能往右/下移動。 方法一:使用二維陣列儲存每個位置的dp值 稍作畫圖分析即可得到dp式子:dp [i] [j] = dp [i-1] [j] + dp [i] [j-1] (

LeetcodeDP-二維陣列 64. Minimum Path Sum / 最小路徑

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum

LeetCodePath Sum(路徑總和)

這道題是LeetCode裡的第112道題。是我在學資料結構——二叉樹的時候碰見的題。 題目要求: 給定一個二叉樹和一個目標和,判斷該樹中是否存在根節點到葉子節點的路徑,這條路徑上所有節點值相加等於目標和。 說明: 葉子節點是指沒有子節點的節點。 示例: 

Leetcode71. 簡化路徑

題目 給定一個文件 (Unix-style) 的完全路徑,請進行路徑簡化。 例如,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" 邊界情況: 你是否考慮了 路徑 = "/../"

LeetCode478. Generate Random Point in a Circle 解題報告(Python)

題目描述: Given the radius and x-y positions of the center of a circle, write a function randPoint which generates a uniform random

leetcode#陣列Python64. Minimum Path Sum 最小路徑

連結: 題目: 給定一個包含非負整數的 m x n 網格,請找出一條從左上角到右下角的路徑,使得路徑上的數字總和為最小。 說明:每次只能向下或者向右移動一步。 示例: 輸入: [ [1,3,

LeetCode64 最小路徑和 (C++)

題目描述: 給定一個包含非負整數的 m x n 網格,請找出一條從左上角到右下角的路徑,使得路徑上的數字總和為最小。 說明:每次只能向下或者向右移動一步。 示例: 輸入: [   [1,3,1],   [1,5,1],   [4,2,1] ] 輸出: 7 解釋:

LeetCode#124二叉樹中的最大路徑和(Binary Tree Maximum Path Sum)

【LeetCode】#124二叉樹中的最大路徑和(Binary Tree Maximum Path Sum) 題目描述 給定一個非空二叉樹,返回其最大路徑和。 本題中,路徑被定義為一條從樹中任意節點出發,達到任意節點的序列。該路徑至少包含一個節點,且不一定經過根節點。 示例

leetcode#陣列Python120. Triangle 三角形最小路徑

連結: 題目: 給定一個三角形,找出自頂向下的最小路徑和。每一步只能移動到下一行中相鄰的結點上。 例如,給定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自頂向下的最小路徑和為 11(即,2

LeetCode392 判斷子序列 (C++)

題目描述: 給定字串 s 和 t ,判斷 s 是否為 t 的子序列。 你可以認為 s 和 t 中僅包含英文小寫字母。字串 t 可能會很長(長度 ~= 500,000),而 s 是個短字串(長度 <=100)。 字串的一個子序列是原始字串刪除一些(也可以不刪除)

LeetCode---Judge Route Circle

菜鳥初試,在這裡記錄每日練習和學習筆記。 ## **題意** ## 以原點為起點,給一個字串序列,其中字元作為移動方向,最後判斷是否迴歸原點。 ## 解題思路 ## 以X、Y座標變化表示位置,最後判斷X

LeetCode(657)Judge Route Circle

題目 Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back

LeetCodeSymmetric Tree 判斷一棵樹是否是映象的

<span style="font-size:18px;"><span style="font-size:18px;">/**LeetCode Symmetric Tree 對稱的樹 * 思路:判斷一棵樹是否對稱,1.有左子樹就要有右子樹 *