国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

【LeetCode】657. Judge Route Circle

Shihira / 2312人閱讀

摘要:題目描述解決方案解題思路設(shè)置初始坐標(biāo)為根據(jù)上下左右指示調(diào)整坐標(biāo)判斷最后坐標(biāo)的位置是否為起始位置。加強(qiáng)版循環(huán)使用比判斷快方法計(jì)算向左和向右的次數(shù)是否相同,計(jì)算向上和向下的次數(shù)相同。若都相同,則回到原地。

題目描述

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
解決方案 - C++
class Solution {
public:
    bool judgeCircle(string moves) {
        int x = 0, y = 0;
        for(int i = 0; i < moves.length(); i++){
            if(moves.substr(i, 1) == "R"){
                x--;
            }
            if(moves.substr(i, 1) == "L"){
                x++;
            }
            if(moves.substr(i, 1) == "U"){
                y++;
            }
            if(moves.substr(i, 1) == "D"){
                y--;
            }
        }
        if(x == 0 && y == 0){
            return true;
        }else{
            return false;
        }
    } 
};

解題思路
設(shè)置初始坐標(biāo)為(0,0),根據(jù)上UDLR指示調(diào)整坐標(biāo),判斷最后坐標(biāo)的位置是否為起始位置。

Submission Details

62 / 62 test cases passed.
Status: Accepted
Runtime: 39 ms
[C++] [Java] Clean Code

Tips:

1.加強(qiáng)版for循環(huán)
2.使用switchif判斷快
3.Java toCharArray()方法

C++

class Solution {
public:
    bool judgeCircle(string moves) {
        int v = 0;
        int h = 0;
        for (char ch : moves) {
            switch (ch) {
                case "U" : v++; break;
                case "D" : v--; break;
                case "R" : h++; break;
                case "L" : h--; break;
            }
        }
        return v == 0 && h == 0;
    }
};

Submission Details

62 / 62 test cases passed.
Status: Accepted
Runtime: 19 ms

Java

class Solution{
    public boolean judgeCircle(String moves) {
        int v = 0, h = 0;
        for (char move : moves.toCharArray()) {
            switch (move) {
                case "U": v++; break;
                case "D": v--; break;
                case "R": h++; break;
                case "L": h--; break;
            }
        }
        return v == 0 && h == 0;
    } 
}

Submission Details

62 / 62 test cases passed.
Status: Accepted
Runtime: 11 ms 
Python one liner
class Solution(object):
    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """ 
        return moves.count("L") == moves.count("R") and moves.count("U") == moves.count("D")

Submission Details

62 / 62 test cases passed.
Status: Accepted
Runtime: 42 ms 

計(jì)算向左和向右的次數(shù)是否相同,計(jì)算向上和向下的次數(shù)相同。若都相同,則回到原地。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/67723.html

相關(guān)文章

  • Leetcode PHP題解--D9 657. Robot Return to Origin

    摘要:題目鏈接題目分析輸入一串指令操作機(jī)器人,判斷執(zhí)行完指令后,能否回到原點(diǎn)。思路判斷向上移動的次數(shù)是否等于向下移動的次數(shù),且向左次數(shù)是否等于向右次數(shù)。但是,如果在指令中沒有出現(xiàn)所有種方向的話,在判斷時會獲取不到數(shù)值。 657. Robot Return to Origin 題目鏈接 657. Robot Return to Origin 題目分析 輸入一串指令操作機(jī)器人,判斷執(zhí)行完指令后,...

    Paul_King 評論0 收藏0
  • #yyds干貨盤點(diǎn)#“愚公移山”的方法解atoi,自以為巧妙!

    摘要:若函數(shù)不能執(zhí)行有效的轉(zhuǎn)換,返回。如果數(shù)值超過可表示的范圍,則返回或。示例輸入輸出解釋轉(zhuǎn)換截止于數(shù)字,因?yàn)樗南乱粋€字符不為數(shù)字。 這是我參與11月更文挑戰(zhàn)的第12天。一、寫在前面LeetCode 第一題兩數(shù)之和傳輸門:聽說你還在寫雙層for循環(huán)解兩數(shù)之和?LeetCode 第二題兩數(shù)之和傳輸門:兩個排序數(shù)組的中...

    番茄西紅柿 評論0 收藏2637
  • LeetCode 520 檢測大寫字母[模擬] HERODING的LeetCode之路

    摘要:解題思路模擬題就按照題目意思來做題目分三種情況那我們也分三種情況通過設(shè)置用來標(biāo)志第一位是大寫還是小寫小寫的話直接通過后面的都不能大寫來判斷大寫的話用記錄后面的大寫的個數(shù)為或者為字符串長度才符合要求代碼如下代碼 ...

    番茄西紅柿 評論0 收藏2637
  • [Leetcode] Gray Code 格雷碼

    摘要:找規(guī)律復(fù)雜度時間空間思路仔細(xì)觀察格雷碼當(dāng)時當(dāng)時當(dāng)時可以發(fā)現(xiàn),的格雷碼,就是的格雷碼,再加上它們的逆序前面多一個。 Grey Code The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n re...

    Code4App 評論0 收藏0
  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評論0 收藏0

發(fā)表評論

0條評論

Shihira

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<