<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 魚上游 閱讀(1227) 評論(4)  編輯  收藏 所屬分類: 爪哇世界探險


    FeedBack:
    # re: GOOGLE挑戰賽練習題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));
    }

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

    常用鏈接

    留言簿(82)

    隨筆分類(59)

    文章分類(21)

    相冊

    收藏夾(40)

    GoodSites

    搜索

    •  

    積分與排名

    • 積分 - 1267907
    • 排名 - 22

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 久久精品亚洲日本佐佐木明希| 国产精品免费看久久久香蕉| 中文有码亚洲制服av片| 69影院毛片免费观看视频在线| 免费观看美女用震蛋喷水的视频 | 亚洲精品亚洲人成在线观看下载| 国产成人免费片在线视频观看| 亚洲av综合avav中文| 免费夜色污私人影院网站电影| 国产午夜亚洲精品不卡免下载| 免费看黄的成人APP| 亚洲中文字幕丝袜制服一区| 亚洲成AV人片在线观看WWW| 亚洲国产一区二区三区| 天天操夜夜操免费视频| 18禁美女黄网站色大片免费观看| 男女作爱免费网站| 日本高清免费网站| 亚洲精品欧美综合四区| 最近最新高清免费中文字幕| 国产成人亚洲综合网站不卡| 免费h视频在线观看| 亚洲av日韩av激情亚洲| 亚洲综合久久夜AV | 国产真实伦在线视频免费观看| 黄色一级视频免费观看| 亚洲性线免费观看视频成熟| 久久久久亚洲精品中文字幕| 亚洲国产综合AV在线观看| 亚州免费一级毛片| 亚洲一区电影在线观看| 91制片厂制作传媒免费版樱花| 亚洲春色在线视频| 国产亚洲精品免费视频播放| jjzz亚洲亚洲女人| 无码毛片一区二区三区视频免费播放 | 久久国产乱子伦精品免费看| 亚洲精品tv久久久久久久久| 一区二区三区免费视频观看| 亚洲国产精品一区二区第四页| 四虎影视久久久免费|