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 阅读(3062) 评论(0)  编辑  收藏 所属分类: 杂文


只有注册用户登录后才能发表评论。


网站导航: