Codewars题记 :Take a Ten Minute Walk

1、题目:

You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones -- everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). You always walk only a single block in a direction and you know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don't want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.

Note: you will always receive a valid array containing a random assortment of direction letters ('n', 's', 'e', or 'w' only). It will never give you an empty array (that's not a walk, that's standing still!).

 

2、我的解决方案

 1 class TenMinWalk {
 2     public static boolean isValid(char[] walk) {
 3 
 4         if (walk.length!=10) {
 5             return false;
 6         }
 7         
 8         Map<Character, Integer> countMap = new HashMap<Character, Integer>();
 9         
10         countMap.put('n', 0);
11         countMap.put('s', 0);
12         countMap.put('w', 0);
13         countMap.put('e', 0);
14         
15         for (char c : walk) {
16             countMap.put(c, countMap.get(c)+1);
17         }
18         
19         return countMap.get('n').equals(countMap.get('s'))&&countMap.get('w').equals(countMap.get('e'));
20     }
21 }

 

3、最佳解决方案

 1 public class TenMinWalk {
 2   public static boolean isValid(char[] walk) {
 3     if (walk.length != 10) {
 4       return false;
 5     }
 6     int x = 0, y = 0;
 7     for (int i = 0; i < 10; i++) {
 8       switch (walk[i]) {
 9         case 'n':
10           y++;
11           break;
12         case 'e':
13           x++;
14           break;
15         case 's':
16           y--;
17           break;
18         case 'w':
19           x--;
20           break;
21       }
22     }
23     return x == 0 && y == 0;
24   }
25 }

4、总结

  我的解决方案好奇葩啊,哈哈。

  在最佳解决方案中,只新增了三个int变量,内存占用少。

转载于:https://www.cnblogs.com/RivenLw/p/11278720.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值