<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 閱讀(3129) 評論(0)  編輯  收藏 所屬分類: 雜文

    主站蜘蛛池模板: 亚洲avav天堂av在线不卡| 中文字幕免费观看全部电影| 无码囯产精品一区二区免费| 亚洲黄片毛片在线观看| 看免费毛片天天看| 高清国语自产拍免费视频国产| 色老板亚洲视频免在线观| 成人在线免费看片| 亚洲人成电影在线观看青青| 4444www免费看| 亚洲一区二区三区久久久久| 免费不卡视频一卡二卡| 日韩亚洲国产高清免费视频| 狠狠久久永久免费观看| 边摸边吃奶边做爽免费视频网站| 免费一级大黄特色大片| 一级成人毛片免费观看| 黑人精品videos亚洲人| 久久久久免费看成人影片| 亚洲成a人片在线观看播放| 免费电视剧在线观看| 朝桐光亚洲专区在线中文字幕| 免费A级毛片无码A| 中国性猛交xxxxx免费看| 亚洲AV日韩精品久久久久| 成人福利免费视频| 狼人大香伊蕉国产WWW亚洲| 国产亚洲?V无码?V男人的天堂| 在线毛片片免费观看| 亚洲婷婷综合色高清在线| 永久免费视频v片www| 国产免费AV片在线观看播放| 久久久久亚洲精品无码系列| 最新欧洲大片免费在线 | 色偷偷亚洲第一综合| 久久久久一级精品亚洲国产成人综合AV区 | 亚洲精品国产成人99久久| 无人在线观看免费高清视频| 国产亚洲视频在线播放大全| 久久九九亚洲精品| 免费羞羞视频网站|