java算法题
发布于2019-10-06 07:37:27,更新于2021-03-17 13:04:03,标签:algorithm 文章会持续修订,转载请注明来源地址:https://meethigher.top/blogCodewars算法记录
1. 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. |
思路
判断一个数组长度是否为10,里面n的数量等于s,w的数量等于s,则返回true,否则false
实现
我的解决方案
1 | public class TenMinWalk { |
大佬的解决方案
1 | public class TenMinWalk { |
2. Find the odd int
描述
1 | Given an array, find the int that appears an odd number of times. |
思路
map集合,key存储数字,value存储次数。遍历集合,将value为奇数的key输出
实现
我的解决方案
1 | import java.util.HashMap; |
大佬的解决方案
1 | public class FindOdd { |
异或的运算规则:相同为0,不同为1
异或要将运算的两个数字转换为二进制进行运算
1 | // 比方说 1⊕2⊕1=2 |
3. Simple Encryption #1 - Alternating Split
描述
1 | For building the encrypted string: |
思路
1 | 加密:将字符串转换成字符数组,然后String1存储every 2nd,剩下的给String2,然后将String1跟String2拼接起来。 |
实现
我的解决方案
1 | public class Kata { |
大佬的解决方案
1 | public class Kata { |
StringBuilder是一个可变的字符序列
4. Maximum subarray sum
[^时间]: 2019-10-13 周日
描述
1 | The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers: |
思路
1 | 解决方案: |
实现
我的解决方案
1 | public static int sequence(int[] arr) { |
大佬的解决方案
1 | public class Max { |
5. Double Cola
[^时间]: 2019-10-13 周日
描述
1 | Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. |
思路
1 | 每到一个人,喝下后会拥有分身术,并且移动到末尾 |
实现
我的解决方案
1 | public static String WhoIsNext(String[] names, int n) { |
大佬的解决方案
1 | public class Line { |
6. Scramblies
[^时间]: 2019-10-13 周日
描述
1 | Complete the function scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false. |
思路
1 | 判断str1中是否有str2里面的字符。如果有,就把str1中的删掉。没有就直接false |
实现
我的解决方案
1 | public class Scramblies { |
大佬的解决方案
1 | public class Scramblies { |
7. Number of trailing zeros of N!
[^时间]: 2019-10-13 周日
描述
1 | Write a program that will calculate the number of trailing zeros in a factorial of a given number. |
思路
1 | 这个题一个司马的坑! |
实现
我的解决方案
1 | public class Solution { |
大佬的解决方案
1 | public class Solution { |
8. Range Extraction
[^时间]: 2019-10-19 周六
描述
1 | A format for expressing an ordered list of integers is to use a comma separated list of either |
思路
1 | 这个题,刚开始理解的时候,有误区。以为连续的数字像3,2,1这种的也算,其实不是,只要1,2,3这种的才算连续的数字 |
实现
我的解决方案
1 | public static String rangeExtraction(int[] arr) { |
大佬的解决方案
1 | static String rangeExtraction(final int[] array) { |
9. Weight for weight
[^时间]: 2019-10-19 周六
描述
1 | My friend John and I are members of the "Fat to Fit Club (FFC)". John is worried because each month a list with the weights of members is published and each month he is the last on the list which means he is the heaviest. |
思路
1 | 老子不会做! |
实现
我的解决方案
1 | public static String orderWeight(String string) { |
大佬的解决方案
1 | public static String orderWeight(String string) { |
10. Is my friend cheating?
[^时间]: 2019-10-19 周六
描述
1 | A friend of mine takes a sequence of numbers from 1 to n (where n > 0). |
思路
1 | 从1-n的n个数中,找到2个数a,b,使得除去a,b,剩下的数之和与a,b之积相等 |
实现
我的解决方案
1 | package source; |
大佬的解决方案
1 | import java.util.LinkedList; |
我居然发现我的思路,跟大佬的思路是一样的,看来我也是大佬了,嘎嘎嘎
11. Longest Common Subsequence
[^时间]: 2019-10-27 周日
描述
1 | Write a called LCS that accepts two sequences and returns the longest subsequence to the passed in sequences. |
思路
1 | 这个问题,soeasy!就是两个字符串比较共有的最长的子串呗 |
实现
我的解决方案
1 |
大佬的解决方案
1 |
12. Is my friend cheating?
[^时间]: 2019-10-27 周日
描述
1 |
思路
1 |
实现
我的解决方案
1 |
大佬的解决方案
1 |
13. Is my friend cheating?
[^时间]: 2019-10-27 周日
描述
1 |
思路
1 |
实现
我的解决方案
1 |
大佬的解决方案
1 |
招聘算法题
反转链表
输入一个链表,反转链表后,输出新链表的表头。
1 | public class Solution { |
打赏