<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    將Java進(jìn)行到底
    將Java進(jìn)行到底
    posts - 15,  comments - 66,  trackbacks - 0

    Problem Statement

         When editing a single line of text, there are four keys that can be used to move the cursor: end, home, left-arrow and right-arrow. As you would expect, left-arrow and right-arrow move the cursor one character left or one character right, unless the cursor is at the beginning of the line or the end of the line, respectively, in which case the keystrokes do nothing (the cursor does not wrap to the previous or next line). The home key moves the cursor to the beginning of the line, and the end key moves the cursor to the end of the line.

    You will be given a int, N, representing the number of character in a line of text. The cursor is always between two adjacent characters, at the beginning of the line, or at the end of the line. It starts before the first character, at position 0. The position after the last character on the line is position N. You should simulate a series of keystrokes and return the final position of the cursor. You will be given a String where characters of the String represent the keystrokes made, in order. 'L' and 'R' represent left and right, while 'H' and 'E' represent home and end.

    Definition

        
    Class: CursorPosition
    Method: getPosition
    Parameters: String, int
    Returns: int
    Method signature: int getPosition(String keystrokes, int N)
    (be sure your method is public)
        

    Constraints

    - keystrokes will be contain between 1 and 50 'L', 'R', 'H', and 'E' characters, inclusive.
    - N will be between 1 and 100, inclusive.

    Examples

    0)
        
    "ERLLL"
    10
    Returns: 7
    First, we go to the end of the line at position 10. Then, the right-arrow does nothing because we are already at the end of the line. Finally, three left-arrows brings us to position 7.
    1)
        
    "EHHEEHLLLLRRRRRR"
    2
    Returns: 2
    All the right-arrows at the end ensure that we end up at the end of the line.
    2)
        
    "ELLLELLRRRRLRLRLLLRLLLRLLLLRLLRRRL"
    10
    Returns: 3
    3)
        
    "RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL"
    19
    Returns: 12

    This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

    答案:


     1
     2public class CursorPosition {
     3    public int getPosition(String keystrokes, int N){
     4        int position = 0;
     5        String s = "";
     6        for(int i = 0; i < keystrokes.length(); i++){
     7            s = keystrokes.substring(i, i+1);
     8            if("L".equals(s)){
     9                if(position == 0continue;
    10                position--;
    11            }

    12            if("R".equals(s)){
    13                if(position == N) continue;
    14                position++;
    15            }

    16            if("H".equals(s)){
    17                position = 0;
    18            }

    19            if("E".equals(s)){
    20                position = N;
    21            }

    22
    23        }

    24
    25        return position;
    26
    27    }

    28    /**
    29     * @param args
    30     */

    31    public static void main(String[] args) {
    32        CursorPosition cursorPosition = new CursorPosition();
    33        int cursor = cursorPosition.getPosition("ERLLL"10);
    34        System.out.println("cursor:" + cursor);
    35    }

    36
    37}

    38
    posted on 2005-11-27 23:42 風(fēng)蕭蕭 閱讀(936) 評(píng)論(2)  編輯  收藏 所屬分類: 雜談

    FeedBack:
    # re: GOOGLE挑戰(zhàn)賽練習(xí)題3及答案
    2005-11-29 13:13 | jackj
    應(yīng)該可以做一些優(yōu)化,如,
    輸入字符串從后往前查,如果碰到'E'或'H',則以后的就不用執(zhí)行了。
    例如'ERLLLHRL',查到倒數(shù)第三個(gè)字符是'H',在前面的'ERLLL'都可以忽略。  回復(fù)  更多評(píng)論
      
    # re: GOOGLE挑戰(zhàn)賽練習(xí)題3及答案
    2005-11-29 13:53 | 風(fēng)蕭蕭
    有道理,說(shuō)得對(duì)。
    謝謝jackj.  回復(fù)  更多評(píng)論
      

    <2005年11月>
    303112345
    6789101112
    13141516171819
    20212223242526
    27282930123
    45678910

    常用鏈接

    留言簿(8)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊(cè)

    收藏夾

    myfriends

    opensource

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 免费视频成人片在线观看| 亚洲国产精品一区二区成人片国内| 999zyz**站免费毛片| 亚洲高清毛片一区二区| 亚洲高清在线视频| 久久亚洲色一区二区三区| 国外成人免费高清激情视频| 久久久久久免费一区二区三区| 噜噜噜亚洲色成人网站| 亚洲中文字幕一二三四区苍井空 | 91天堂素人精品系列全集亚洲| 国产成人无码免费视频97 | 99亚洲精品高清一二区| 伊伊人成亚洲综合人网7777| 国产99视频免费精品是看6| 114一级毛片免费| 57pao国产成永久免费视频| 中文字幕无码毛片免费看| 未满十八私人高清免费影院| 最新亚洲人成网站在线观看 | 成人au免费视频影院| 成年人免费的视频| 91视频免费网址| 一级毛片免费毛片一级毛片免费| 国产精品九九久久免费视频| 牛牛在线精品免费视频观看| 亚洲精品亚洲人成在线| 91在线亚洲综合在线| 国产成人亚洲综合一区| 亚洲日韩AV一区二区三区四区 | 大地资源在线资源免费观看| 一级午夜免费视频| 国产裸体美女永久免费无遮挡 | 中文字幕亚洲综合久久菠萝蜜| 亚洲精品动漫人成3d在线| 亚洲一区二区三区在线视频| 亚洲av再在线观看| 亚洲最大激情中文字幕| 亚洲精品无码专区久久久| 亚洲av无码国产精品夜色午夜 | 污视频在线观看免费|