Java
文章平均质量分 82
1000sakura
https://home.cnblogs.com/u/1000sakura/
展开
-
【Codewars】Bouncing Balls
Codewars里的 6kyu Kata。题目说明:A child is playing with a ball on the nth floor of a tall building. The height of this floor,h, is known.He drops the ball out of the window. The ball bounces (for ex...原创 2019-05-31 21:00:19 · 1018 阅读 · 1 评论 -
【Codewars】Multi-tap Keypad Text Entry on an Old Mobile Phone
Codewars里的 6kyu Kata。题目说明:Description:Prior to having fancy iPhones, teenagers would wear out their thumbs sending SMS messages on candybar-shaped feature phones with 3x4 numeric keypads.---...原创 2019-05-27 16:02:19 · 523 阅读 · 0 评论 -
【Codewars】Multiply
Codewars里的注册题。没有完成这道题就不能注册账号。题目说明:The code does not execute properly. Try to figure out why.解题方法:public class Multiply { public static Double multiply(Double a, Double b) { ret...原创 2019-05-03 21:45:02 · 454 阅读 · 0 评论 -
【Codewars】All Inclusive?
Codewars里的 7kyu Kata。题目说明:Description:Input:a stringstrng an array of stringsarrOutput of functioncontain_all_rots(strng, arr) (or containAllRots or contain-all-rots):a booleantrueif...原创 2019-05-27 15:23:25 · 338 阅读 · 0 评论 -
【Codewars】Integers: Recreation One
Codewars里的 5kyu Kata。题目说明:Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42. These divisors squared are: 1, 4, 9, 36, 49, 196, 441, 1764. The sum of the squared divisors is 2500 which is 50 * 50, a ...原创 2019-05-27 15:16:30 · 306 阅读 · 0 评论 -
【Codewars】Maximum subarray sum
Codewars里的 5kyu Kata。题目说明:The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:Max.sequence(new int[]{-2, 1, -3, 4, ...原创 2019-05-20 17:11:13 · 377 阅读 · 0 评论 -
【Codewars】Folding your way to the moon
Codewars里的 7kyu Kata。题目说明:Have you heard about the myth thatif you fold a paper enough times, you can reach the moon with it? Sure you do, but exactly how many? Maybe it's time to write a progra...原创 2019-05-20 16:57:50 · 267 阅读 · 0 评论 -
【LeetCode】House Robber II(打家劫舍 II)
这道题是LeetCode里的第213道题。题目描述:你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。...原创 2019-05-19 14:58:10 · 236 阅读 · 0 评论 -
【LeetCode】House Robber(打家劫舍)
这道题是LeetCode里的第198道题。题目描述:你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。示例 1:输入: [1,2,3,1]输出...原创 2019-05-19 14:35:59 · 157 阅读 · 0 评论 -
【LeetCode】Pascal's Triangle(杨辉三角)
这道题是LeetCode里的第118道题。题目描述:给定一个非负整数numRows,生成杨辉三角的前numRows行。在杨辉三角中,每个数是它左上方和右上方的数的和。示例:输入: 5输出:[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]题目虽然看起来简单,而且大部分人都做过,但是...原创 2019-04-30 22:39:48 · 119 阅读 · 0 评论 -
【LeetCode】Valid Palindrome(验证回文串)
这道题是LeetCode里的第125道题。题目描述:给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。说明:本题中,我们将空字符串定义为有效的回文串。示例 1:输入: "A man, a plan, a canal: Panama"输出: true示例 2:输入: "race a car"输出: false三个步骤:小...原创 2019-04-30 22:27:42 · 103 阅读 · 0 评论 -
【LeetCode】Min Stack(最小栈)
这道题是LeetCode里的第155道题。题目描述:设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。push(x)-- 将元素 x 推入栈中。 pop()-- 删除栈顶的元素。 top()-- 获取栈顶元素。 getMin() -- 检索栈中的最小元素。示例:MinStack minStack = new MinStack();...原创 2019-04-30 21:51:35 · 334 阅读 · 0 评论 -
【LeetCode】Count Primes(计数质数)
这道题是LeetCode里的第204道题。题目描述:统计所有小于非负整数n的质数的数量。示例:输入: 10输出: 4解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。这道题很有意思,使用的是 厄拉多塞筛法,基本思想是用一个记录数组记录是否为质,质数为 0,反之为合数。可以确定两点:x 是素数,x 的倍数肯定不是素数 若 x 不是素数...原创 2019-04-30 20:58:04 · 248 阅读 · 0 评论 -
【LeetCode】Sqrt(x)(x 的平方根)
这道题是LeetCode里的第69道题。题目描述:实现int sqrt(int x)函数。计算并返回x的平方根,其中x是非负整数。由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。示例 1:输入: 4输出: 2示例 2:输入: 8输出: 2说明: 8 的平方根是 2.82842..., 由于返回类型是整数,小数部分将被...原创 2019-04-30 20:39:16 · 131 阅读 · 0 评论 -
【LeetCode】Single Number(只出现一次的数字)
这道题是LeetCode里的第136道题。题目描述:给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。说明:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?示例 1:输入: [2,2,1]输出: 1示例2:输入: [4,1,2,1,2]输出: 4方法一:使用哈希表,建立一个哈希数...原创 2019-04-30 20:29:29 · 310 阅读 · 0 评论 -
【Codewars】Data Reverse
Codewars里的 6kyu Kata。题目说明:Description:A stream of data is received and needs to be reversed.Each segment is 8 bits long, meaning the order of these segments needs to be reversed, for example:...原创 2019-05-27 18:28:37 · 299 阅读 · 0 评论 -
【Codewars】Shortest Word
Codewars里的 7kyu Kata。题目说明:Simple, given a string of words, return the length of the shortest word(s).String will never be empty and you do not need to account for different data types.使用双指针解...原创 2019-05-18 15:22:33 · 389 阅读 · 0 评论 -
【Codewars】Josephus Permutation
Codewars里的 5kyu Kata。题目说明:This problem takes its name by arguably the most important event in the life of the ancient historian Josephus: according to his tale, he and his 40 soldiers were trappe...原创 2019-05-31 20:32:48 · 386 阅读 · 0 评论 -
【Codewars】Dubstep
Codewars里的 6kyu Kata。题目说明:Description:Polycarpus works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of ol...原创 2019-05-31 20:14:06 · 425 阅读 · 0 评论 -
【Codewars】Pick peaks
Codewars里的 5kyu Kata。题目说明:In this kata, you will write a function that returns the positions and the values of the "peaks" (or local maxima) of a numeric array.For example, the arrayarr = [0, ...原创 2019-05-30 22:17:48 · 343 阅读 · 0 评论 -
【Codewars】Is a number prime?
Codewars里的 6kyu Kata。题目说明:Description:Define a function that takes an integer argument and returns logical valuetrueorfalsedepending on if the integer is a prime.Per Wikipedia, a prime nu...原创 2019-05-30 22:12:35 · 805 阅读 · 0 评论 -
【Codewars】Find The Parity Outlier
Codewars里的 6kyu Kata。题目说明:You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or e...原创 2019-05-30 22:02:21 · 269 阅读 · 0 评论 -
【Codewars】Simple Pig Latin
Codewars里的 5kyu Kata。题目说明:Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.ExamplespigIt('Pig latin is cool'); // ...原创 2019-05-30 21:55:40 · 482 阅读 · 0 评论 -
【LeetCode】Factorial Trailing Zeroes(阶乘后的零)
这道题是LeetCode里的第172道题。题目描述:给定一个整数n,返回n! 结果尾数中零的数量。示例 1:输入: 3输出: 0解释:3! = 6, 尾数中没有零。示例2:输入: 5输出: 1解释:5! = 120, 尾数中有 1 个零.说明:你算法的时间复杂度应为O(logn)。0 来源于 2 * 5,因为在阶乘中,因子 2 肯定...原创 2019-05-18 19:55:50 · 136 阅读 · 0 评论 -
【LeetCode】Majority Element(求众数)
这道题是LeetCode里的第169道题。题目描述:给定一个大小为n的数组,找到其中的众数。众数是指在数组中出现次数大于⌊ n/2 ⌋的元素。你可以假设数组是非空的,并且给定的数组总是存在众数。示例1:输入: [3,2,3]输出: 3示例2:输入: [2,2,1,1,1,2,2]输出: 2三种方法:摩尔投票法 排序后去中间数 使用哈...原创 2019-05-18 19:44:05 · 80 阅读 · 0 评论 -
【LeetCode】Excel Sheet Column Title(Excel表列名称)
这道题是LeetCode里的第168道题。题目描述:给定一个正整数,返回它在 Excel 表中相对应的列名称。例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ...示例 1:输入: 1输出: "A"...原创 2019-05-18 18:34:23 · 189 阅读 · 0 评论 -
【LeetCode】Two Sum II - Input array is sorted(两数之和 II - 输入有序数组)
这道题是LeetCode里的第167道题。题目描述:给定一个已按照升序排列的有序数组,找到两个数使得它们相加之和等于目标数。函数应该返回这两个下标值index1 和 index2,其中 index1必须小于index2。说明:返回的下标值(index1 和 index2)不是从零开始的。 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。示例:...原创 2019-05-18 17:14:15 · 97 阅读 · 0 评论 -
【LeetCode】Best Time to Buy and Sell Stock II(买卖股票的最佳时机 II)
这道题是LeetCode里的第122道题。题目描述:给定一个数组,它的第i个元素是一支给定股票第i天的价格。设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。示例 1:输入: [7,1,5,3,6,4]输出: 7解释: 在第 2 天(股票价格 = 1)...原创 2019-05-18 16:40:11 · 113 阅读 · 0 评论 -
【LeetCode】Best Time to Buy and Sell Stock(买卖股票的最佳时机)
这道题是LeetCode里的第121道题。题目描述:给定一个数组,它的第i个元素是一支给定股票第i天的价格。如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。注意你不能在买入股票前卖出股票。示例 1:输入: [7,1,5,3,6,4]输出: 5解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价...原创 2019-05-18 16:20:50 · 165 阅读 · 0 评论 -
【Codewars】Valid Braces
Codewars里的 6kyu Kata。题目说明:Write a function that takes a string of braces, and determines if the order of the braces is valid. It should returntrueif the string is valid, andfalseif it's inval...原创 2019-05-18 16:12:30 · 409 阅读 · 0 评论 -
【Codewars】The Supermarket Queue
Codewars里的 6kyu Kata。题目说明:There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!...原创 2019-05-18 15:52:50 · 730 阅读 · 0 评论 -
【LeetCode】Pascal's Triangle II(杨辉三角 II)
这道题是LeetCode里的第119道题。题目描述:给定一个非负索引k,其中k≤33,返回杨辉三角的第k行。在杨辉三角中,每个数是它左上方和右上方的数的和。示例:输入: 3输出: [1,3,3,1]进阶:你可以优化你的算法到O(k) 空间复杂度吗?直接把 118 的拿过来改改就行了呗!偷懒可以,但是该学的算法还是得学啊!其实这题可以算...原创 2019-05-03 23:23:46 · 131 阅读 · 0 评论 -
【Codewars】Build a pile of Cubes
Codewars里的 6kyu Kata。题目说明:Your task is to construct a building which will be a pile of n cubes. The cube at the bottom will have a volume of n^3, the cube above will have volume of (n-1)^3 and so...原创 2019-05-03 22:17:56 · 537 阅读 · 0 评论 -
【Codewars】Abbreviate a Two Word Name
Codewars里的 8kyu Kata。题目说明:Write a function to convert a name into initials. This kata strictly takes two words with one space in between them.The output should be two capital letters with a dot...原创 2019-05-03 21:53:06 · 408 阅读 · 0 评论 -
【LeetCode】Merge Two Sorted Lists(合并两个有序链表)
这道题是LeetCode里的第21道题。题目描述:将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。示例:输入:1->2->4, 1->3->4输出:1->1->2->3->4->4这道题需要考虑的地方挺多的,首先是头节点的处理,还有尾节点的链接问题。对于头节点,我的想法是...原创 2019-04-13 12:55:20 · 144 阅读 · 0 评论 -
【LeetCode】Implement strStr()(实现strStr())
这道题是LeetCode里的第28道题。题目描述:实现strStr()函数。给定一个haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回-1。示例 1:输入: haystack = "hello", needle = "ll"输出: 2示例 2:...原创 2019-04-13 11:50:36 · 80 阅读 · 0 评论 -
【LeetCode】移除元素(Remove Element)
这道题是LeetCode里的第27道题。题目描述:给定一个数组nums和一个值val,你需要原地移除所有数值等于val的元素,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。示例 1:给定 nums = [3,2,2,3], val =...原创 2019-04-13 10:55:33 · 140 阅读 · 0 评论 -
【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)
这道题是LeetCode里的第26道题。题目描述:给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。示例1:给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为你不需要...原创 2019-04-13 10:40:43 · 129 阅读 · 0 评论 -
【LeetCode】Longest Common Prefix(最长公共前缀)
这道题是LeetCode里的第14道题。题目描述:编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串""。示例1:输入: ["flower","flow","flight"]输出: "fl"示例2:输入: ["dog","racecar","car"]输出: ""解释: 输入不存在公共前缀。说明:所有输入只包含小...原创 2019-04-05 15:42:49 · 129 阅读 · 0 评论 -
【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题。题目描述:给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。示例1:输入: 123输出: 321示例 2:输入: -123输出: -321示例 3:输入: 120输出: 21注意:假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为[−231, 231− 1...原创 2019-04-05 15:10:50 · 107 阅读 · 0 评论