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

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

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

    posts - 61,  comments - 2033,  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)

     

     

     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}
    posted on 2005-11-28 10:38 魚上游 閱讀(1225) 評論(4)  編輯  收藏 所屬分類: 爪哇世界探險(xiǎn)


    FeedBack:
    # re: GOOGLE挑戰(zhàn)賽練習(xí)題3及答案(1000分)
    2005-11-29 16:08 | 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));
    }

    }
    這道題和你做的幾乎一樣,效率上應(yīng)該沒有太大差別
    但我的了920多分,我覺的是時(shí)間的原因,這道題我做的很快就提交了  回復(fù)  更多評論
      
    # re: GOOGLE挑戰(zhàn)賽練習(xí)題3及答案(1000分)
    2005-11-29 16:13 | 胡子魚
    呵,是的,時(shí)間也是評分的一個(gè)重要因素。   回復(fù)  更多評論
      
    # re: GOOGLE挑戰(zhàn)賽練習(xí)題3及答案(1000分)
    2005-12-02 14:50 | space
    不行阿,all the same  回復(fù)  更多評論
      
    # re: GOOGLE挑戰(zhàn)賽練習(xí)題3及答案(1000分)
    2005-12-09 12:34 | emu
    沒有優(yōu)化。其實(shí)可以直接定位到最后一次出現(xiàn)的“H”或者“E”的位置開始計(jì)算的。  回復(fù)  更多評論
      
    <2005年11月>
    303112345
    6789101112
    13141516171819
    20212223242526
    27282930123
    45678910

    常用鏈接

    留言簿(82)

    隨筆分類(59)

    文章分類(21)

    相冊

    收藏夾(40)

    GoodSites

    搜索

    •  

    積分與排名

    • 積分 - 1267321
    • 排名 - 22

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 亚洲AV无码久久精品成人| 男女午夜24式免费视频| 亚洲一级片内射网站在线观看| 91嫩草亚洲精品| 国产免费人人看大香伊| 国产在线观a免费观看| 亚洲三级中文字幕| 久久影院亚洲一区| 欧美好看的免费电影在线观看| 亚洲人成片在线观看| 亚洲阿v天堂在线2017免费| 日本在线免费观看| 精品久久久久亚洲| 亚洲AV无码一区二区三区国产| 无码一区二区三区免费| 久久亚洲精品成人| 国产真人无遮挡作爱免费视频| 久久一区二区三区免费播放| 香港一级毛片免费看| 亚洲欧洲国产精品久久| 国产AV无码专区亚洲AV手机麻豆 | 亚洲日韩精品国产一区二区三区| 国产成人亚洲影院在线观看| 好大好深好猛好爽视频免费| 亚欧免费一级毛片| 日韩精品无码免费视频| 亚洲国产成人精品激情| 久久精品国产精品亚洲艾| 免费人成无码大片在线观看| 亚洲国产精品免费观看| 精品一区二区三区免费| 深夜免费在线视频| 欧美色欧美亚洲另类二区| 国产gav成人免费播放视频| 国产国产人免费视频成69堂| 两个人看www免费视频| 美女视频免费看一区二区| 亚洲 欧洲 日韩 综合在线| 亚洲精品视频在线免费| 亚洲国产第一站精品蜜芽| 亚洲一级特黄大片在线观看|