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

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

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

    將Java進行到底
    將Java進行到底
    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 風蕭蕭 閱讀(940) 評論(2)  編輯  收藏 所屬分類: 雜談

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

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

    常用鏈接

    留言簿(8)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊

    收藏夾

    myfriends

    opensource

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲精品乱码久久久久久V| 亚洲最大在线观看| 中文字幕亚洲不卡在线亚瑟| 亚洲 欧洲 视频 伦小说| 免费AA片少妇人AA片直播| 亚洲精品在线不卡| 四虎在线成人免费网站| 亚洲欧洲国产视频| 97人妻无码一区二区精品免费| 亚洲视频免费在线播放| 67194成手机免费观看| 亚洲无成人网77777| 香蕉97超级碰碰碰免费公| tom影院亚洲国产一区二区| 一二三四影视在线看片免费 | 久久综合亚洲色HEZYO国产| 深夜福利在线视频免费| 37pao成人国产永久免费视频| 亚洲综合一区二区精品导航| 美女黄频免费网站| 4hu四虎最新免费地址| 亚洲国产日产无码精品| 大地影院MV在线观看视频免费| 国产亚洲AV无码AV男人的天堂| 亚洲av成人一区二区三区观看在线| 精品国产免费观看久久久| 亚洲精品中文字幕乱码三区| 久久精品国产免费一区| 亚洲午夜久久久久久尤物| 最近免费中文字幕视频高清在线看| 国产AV无码专区亚洲Av| 久视频精品免费观看99| 亚洲AV永久无码天堂影院| 亚洲中文字幕无码爆乳av中文| 亚洲国产一区二区三区在线观看| 国产免费131美女视频| 亚洲JLZZJLZZ少妇| 综合亚洲伊人午夜网| 四虎影视久久久免费| 久久av无码专区亚洲av桃花岛| 免费无码又爽又刺激高潮|