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

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

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

    Vincent.Chan‘s Blog

    常用鏈接

    統計

    積分與排名

    網站

    最新評論

    GOOGLE挑戰賽練習題3及答案(1000分)

    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)

     

     

     1public class CursorPosition
     2{
     3
     4        public int getPosition(String ks,int len)
     5        {
     6                int pos = 0;
     7                char c ;
     8                for(int i=0;i<ks.length();i++)
     9                {
    10                        c = ks.charAt(i);
    11
    12                        if(c=='E')
    13                        {
    14                                pos = len;
    15                        }

    16                        else if(c=='H')
    17                        {
    18                                pos = 0;
    19                        }

    20                        else if(c=='L')
    21                        {
    22                                if(pos>0)
    23                                {
    24                                  pos--;
    25                            }

    26                        }

    27                        else if(c=='R')
    28                        {
    29                                if(pos<len)
    30                                {
    31                                  pos++;
    32                            }

    33
    34                        }

    35                }

    36
    37        return pos;
    38        }

    39
    40}

    Feedback

    # re: GOOGLE挑戰賽練習題3及答案(1000分)   回復   

    2005-11-29 16:08 by superwu
    public class CursorPosition {
    public int getPosition(String keystrokes, int N){
    int current=0;
    int end=N;
    int start=0;


    for(int i=0;i<keystrokes.length();i++){
    char c=keystrokes.charAt(i);
    switch(c){
    case 'L':
    if(current!=start)current--;break;
    case 'R':
    if(current!=end)current++;break;
    case 'H':
    current=0;break;
    case 'E':
    current=N;break;
    }
    }
    return current;
    }
    public static void main(String[] args) {
    CursorPosition c=new CursorPosition2();
    System.out.println(c.getPosition("ERLLL",10));
    }

    }
    這道題和你做的幾乎一樣,效率上應該沒有太大差別
    但我的了920多分,我覺的是時間的原因,這道題我做的很快就提交了

    posted on 2006-02-13 21:38 Vincent.Chen 閱讀(3136) 評論(0)  編輯  收藏 所屬分類: 雜文

    主站蜘蛛池模板: 国产精品久免费的黄网站| 91成年人免费视频| 亚洲国产主播精品极品网红 | a级毛片毛片免费观看久潮喷| 国产成人精品高清免费| 亚洲人成色77777在线观看| 成人免费看片又大又黄| 99久久国产亚洲综合精品| 成视频年人黄网站免费视频| 亚洲一本之道高清乱码| 毛片免费在线观看网址| 亚洲Av永久无码精品一区二区| 免费无码又爽又刺激高潮的视频| 亚洲乱理伦片在线观看中字| 午夜男人一级毛片免费| 免费精品久久久久久中文字幕| 亚洲免费一区二区| a级毛片高清免费视频| 77777_亚洲午夜久久多人| 嘿嘿嘿视频免费网站在线观看| 亚洲人成电影院在线观看| 成人午夜免费福利| 成人a毛片视频免费看| 国产v亚洲v天堂无码网站| 麻花传媒剧在线mv免费观看 | 黄网站色成年片大免费高清| 亚洲精品WWW久久久久久 | 黄色一级毛片免费| 亚洲综合无码精品一区二区三区| 日本高清高色视频免费| 亚洲免费黄色网址| 亚洲av无码成人精品区在线播放| 国产精品黄页免费高清在线观看| 亚洲精品狼友在线播放| 91精品视频免费| 免费人成又黄又爽的视频在线电影| 亚洲精品国产字幕久久不卡 | 成年女人色毛片免费看| 久久久免费观成人影院| 91亚洲国产成人久久精品| 亚洲av无码国产精品色在线看不卡 |