GOOGLE挑战赛练习题3及答案(1000分)

博客涉及Google以及C、C++、C#相关信息技术内容,但具体内容缺失。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

 

 

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

16InBlock.gif                        else if(c=='H')
17ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
18InBlock.gif                                pos = 0;
19ExpandedSubBlockEnd.gif                        }

20InBlock.gif                        else if(c=='L')
21ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
22InBlock.gif                                if(pos>0)
23ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
24InBlock.gif                                  pos--;
25ExpandedSubBlockEnd.gif                            }

26ExpandedSubBlockEnd.gif                        }

27InBlock.gif                        else if(c=='R')
28ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
29InBlock.gif                                if(pos<len)
30ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
31InBlock.gif                                  pos++;
32ExpandedSubBlockEnd.gif                            }

33InBlock.gif
34ExpandedSubBlockEnd.gif                        }

35ExpandedSubBlockEnd.gif                }

36InBlock.gif
37InBlock.gif        return pos;
38ExpandedSubBlockEnd.gif        }

39InBlock.gif
40ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值