![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Algorithm
文章平均质量分 55
Daisy_HJL
无论到哪里,都要记得带上自己的阳光!
展开
-
有N个灯放在一排,N个人进行操作,求灯泡最后的状态
题:有N个灯放在一排,从1到N依次顺序编号。有N个人,也从1到N顺序编号。1号将灯全部关闭,2号将凡是2的倍数的灯全部打开;3号将3的倍数的灯全部作相反操作(该灯如为打开,则将它关闭;如关闭,则将灯打开)。以后的人,都和3号操作一样,将凡是自己序号倍数的灯作相反操作。编程实现:第N个人操作后,按顺序输出灯的状态(1表示等打开,0表示灯关闭)。输入:N(1输出原创 2017-03-24 14:08:05 · 5099 阅读 · 3 评论 -
Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.代码如下:package math;public class Sqrt { public static int sqrt(int x){ long r = x; //注意使用long类型,因为如果r是int类型的话,r*r超出了最大整数原创 2017-06-21 22:21:31 · 304 阅读 · 0 评论 -
Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1]原创 2017-06-09 17:09:29 · 171 阅读 · 0 评论 -
Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Note: Given n will be a原创 2017-06-20 22:29:39 · 212 阅读 · 0 评论 -
Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]原创 2017-06-20 09:43:13 · 254 阅读 · 0 评论 -
Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return[ ["aa","b"],原创 2017-06-04 15:08:31 · 172 阅读 · 0 评论 -
Combination Sum (without Duplication)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combina原创 2017-06-04 15:06:16 · 320 阅读 · 0 评论 -
Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen原创 2017-06-04 15:01:12 · 194 阅读 · 0 评论 -
Permutations
Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1原创 2017-06-04 14:56:27 · 191 阅读 · 0 评论 -
Subsets II (contains duplicates)
Given a collection of integers that might contain duplicates, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example,If nums = [1,2,2], a sol原创 2017-06-04 14:51:10 · 198 阅读 · 0 评论 -
Subsets (backtracking)
Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example,If nums = [1,2,3], a solution is:[ [3], [1],原创 2017-06-04 14:48:29 · 367 阅读 · 0 评论 -
Add Binary
以字符串形式给出两个二进制数字,使这两个数字相加得到一个和(和仍为二进制数字)如:“11”“1”和为:“100”代码如下:public class AddBinary { public static String addBinary(String a,String b){ StringBuilder sb = new StringBuilder()原创 2017-06-19 22:09:32 · 211 阅读 · 0 评论 -
Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Return原创 2017-06-07 22:33:35 · 195 阅读 · 0 评论 -
Jump Game II
代码如下:public class JumpGame { public boolean jumpGame(int[] nums){ int i=0; for(int reach=0;i<=nums.length&&i<=reach;i++){ reach=Math.max(reach, nums[i]+i); } return i==nums.length; }原创 2017-06-07 21:11:17 · 214 阅读 · 0 评论 -
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a原创 2017-06-07 21:09:33 · 189 阅读 · 0 评论 -
Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal i原创 2017-06-07 09:19:24 · 248 阅读 · 0 评论 -
Multiply Strings
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.Note:The length of both num1 and num2 is Both num1 and num2 contains only digits原创 2017-06-06 17:07:00 · 323 阅读 · 0 评论 -
Plus One
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digi原创 2017-06-09 22:22:18 · 289 阅读 · 0 评论 -
面试题——关灯游戏
n个灯泡,从左排到右排成一行,初始时,有些灯泡是亮的,有些是熄灭的。接下来,Alice和Bob轮流进行操作,Alice先开始。每次操作中,轮到操作的人需要选择一个点亮的灯泡,然后把它以及它之后的所有灯泡的状态都变换一次。如果在某个人操作之后,所有的灯泡都熄灭了,那么这个人就赢得了游戏。Alice和Bob都想赢得这个游戏,在他们足够聪明的情况下,最后谁会赢?输入:第一行包含一个整数n,表示灯原创 2017-09-15 09:56:04 · 3049 阅读 · 1 评论 -
Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is原创 2017-06-09 21:19:24 · 230 阅读 · 0 评论 -
笔试——牛羊吃草
最初有n份青草,牛牛和羊羊依次吃草,牛牛先开始吃,每次吃草的份数必须是4的x次幂,比如1,4,16,64等等。谁最后吃完谁胜。假定牛牛和羊羊都是按照最佳方法进行游戏,请输出胜利者的名字。输入包括t+1行。第一行包括一个整数t(1接下来t行,每一行一个n(1输出:对于每一个n,牛牛胜输出“niu”,羊羊胜输出“yang”。样例:3123输出:niu原创 2017-10-14 21:30:29 · 2043 阅读 · 3 评论 -
笔试——字符串价值
有一种有趣的字符串价值计算方式:统计字符串中每种字符出现的次数,然后求所有字符次数的平方和作为字符串的价值。例如:字符串“abacaba”,里面包括4个‘a’,2个‘b’,1个‘c’,于是这个字符串的价值为:4*4+2*2+1*1=21;牛牛有一个字符串s,并且允许你从s中移除最多k个字符,你的目标是让得到的字符串的价值最小。输入描述:输入包括两行:第一行一个字符串s,长度leng原创 2017-10-14 19:52:41 · 1119 阅读 · 0 评论 -
笔试——环形公交
有一环形公交站,包含N(N>=3)个站点{站点序号为:1~N},每个站点停留5分钟,相邻两个站点之间行驶时间都是10分钟,循环形式,已知第一圈第一个站点进入时间为X,现在公交形式时间为Y,请计算当前公交圈数,以及处在哪个站点之间M-N(如果停在某个站点Z,则输出Z-Z)输入:一行字符串:3,2017-07-25 06:00:00,2017-07-25 06:40:00【整数、时间字符串、时原创 2017-10-14 16:51:14 · 1236 阅读 · 1 评论 -
给定正整数N和集合K,求不大于N的,且每一位数字都在K中的最大值M,比如N=297,K={2,5,8},则返回288
思想:从高到低位开始排,在序列中找遇到有相等的,就加到M*10(往左移一位)中,一直进行这样的循环;直到没有相等的,就取当前位的前一位,自此之后的所有位数都取数组K中最大的数。代码:import java.util.Arrays;import java.util.Scanner;public class MaxM { public static void main(String[]原创 2017-10-19 13:42:26 · 2918 阅读 · 3 评论 -
迅雷2018笔试——有符号整数反转
输入一个有符号整数,输出该整数的反转值输入:一个整数输出:一个整数示例1:输入:123输出:321示例2:输入:-123输出:-321代码如下:import java.util.Scanner;public class ReverseNumber { public static void main(Str原创 2017-09-19 22:30:49 · 2524 阅读 · 0 评论 -
迅雷2018笔试——整数求和
给定整数n,取若干个1到n的整数可求和等于m,编程求出所有组合的个数。比如当n=6,m=8时,有四种组合:[2,6],[3,5],[1,2,5],[1,3,4]。限定n和m小于120输入:整数n和m输出:求和等于m的所有组合的个数示例1:输入:6 8输出:4代码:import java.util.ArrayList;impor原创 2017-09-19 22:33:21 · 2107 阅读 · 2 评论 -
2018笔试题——求一元一次方程的解
给出一个没有简化的一元一次方程的解,如果方程没有解,返回“No solution”;如果方程有无穷解,返回“Infinite solutions”。如果这个方程只有一个解,返回“x=?”,其中?代表解的数值,题目保证方程的解是整数。示例1:输入:2x+5-3+x=6+x-2输出:x=1示例2:输入:-2x=x-3x+7输出:No solution原创 2017-09-19 21:53:44 · 5018 阅读 · 0 评论 -
笔试题——回文数组
对于一个给定的正整数数组a[],如果将a的倒序仍然是自己,那它就是一个回文数组。现输入一个正整数数组,要求插入一些数字,使其变为回文数组,且数组中的所有数字的和尽可能小。输出这个插入后,数组的元素总和。例如:[1,2,3,1,2]插入两个1,得到[1,2,1,3,1,2,1]即可,和为11。输入描述:输入数据由两行组成:第一行包含一个整数L,表示数组的长度;第二行输入数组元素。输出描述原创 2017-09-17 20:42:32 · 3641 阅读 · 7 评论 -
小米2018笔试题——食堂里的长桌
食堂有一张靠墙的长桌(只有面向墙的一排座位),独自来吃饭的同学都喜欢坐在这里,这长桌有一个不成文的规定:来的人都喜欢独处,不喜欢挨着其他人吃饭;现在长桌上有一些各自独处的同学,这时又来一群人过来用餐,不知道新来的同学能不能都遵循这条规定,找到左右无人的座位?输入:第一行:一个字符串table,表示长桌(由字符‘1’和‘0’组成,每个字符代表一个座位,‘1’表示座位有人,tabl原创 2017-09-19 08:24:12 · 939 阅读 · 1 评论 -
小米2018笔试——三角形
设计一个整形三角形,三角形的每一行都比上一行多出一个数,每个数都等于它上方(如果有的话)与左上方两个数字之和,如下面所示:11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 1。。。。。。现在给你一个数,请算出它最开始出现在第几行。输入:一个正长整型数,保证为长整型的整数输出:这个数最开始出现的行数样原创 2017-09-18 22:31:50 · 1232 阅读 · 0 评论 -
笔试题——东南西北(ESWN)按任一方向走N步
小云被困在在一个没有方向的幻境,只要走出N步即可脱离幻境,此N步不考虑时间也考虑方向,但是在不同时刻向不同方向走所耗费的体力不同;现已知道不同时刻在某个方向跨出一步所需要耗费的体力,请找出最省体力的方法。输入:有多个输入样例,输入的第一行是样例个数T(1输出:第一行输出需要的最小体力值。第二行行走方案分别用符号ESWN表示东南西北。样例输入:141原创 2017-09-26 20:52:24 · 3230 阅读 · 0 评论 -
小米2018笔试题——公司饮料太便宜
公司中的自动售货机有7种饮料,小雷买了很多瓶各种饮料,每天喝能一瓶,但是对于其中某种饮料(比如喝了会长胖的可乐),喝完之后需要隔几天才能喝相同的饮料,请问小明最快多久能喝完这些饮料?输入:第一行:一个字符串drinks,其中每个字符代表一瓶某种饮料(0第二行:天数n(0输出:最快能将饮料数喝完的天数样例输入:AAAFFF2ABCDE1原创 2017-09-18 21:40:43 · 1066 阅读 · 0 评论 -
笔试题——团队活动分组
现有n个人,每个人有一个唯一的数字作为标签,标签数字的范围:1~n;为了统计分组情况,有人提议有分组意愿的人提交一个数字,表示其会和以该数字为标签的人分到一组。现在知道每个人的选择,您能统计出一共有多少个小组吗?注意:1和2一组,2和3一组,那么1,2,3属于一组。默认为自己一定和自己一组。输入:第一行:n(表示n个人,1第二行:n个数(1输出:输出一个原创 2017-09-18 17:38:46 · 2305 阅读 · 0 评论 -
笔试题——Unix路径简化
简化Unix风格的路径,需要考虑的包括“/../”,“//”,“/./”等情况输入描述:一个字符串,表示Unix风格的路径输出描述:简化后的Unix风格的路径示例1:输入:"/home/"输出:"/home"示例2:输入:"/a/./b/../../c/"输出:"/c"分析:“..”表示返回路径的上级目录(如果当前是根原创 2017-09-17 21:22:24 · 4159 阅读 · 0 评论 -
伪随机数
有一个随机数生成器:首先输入一个正整数n表示要生成随机数的位数,然后它会生成一个n位的不包含前导数为0的正十进制数,生成器还有一个性质,就是它生成的第i位上(从左到右)的数字永远不等于i。比如生成的位数长度为1的数,那它不能生成0和1,;如果位数为2,不能生成10,11,12,13,14,15,16,17,18,19,22,32,42,54,62,72,82,92求给定输入n,它一共能生成原创 2017-09-15 10:52:17 · 526 阅读 · 0 评论 -
最长漂亮子串
有一小写英文字符构成的字符串,想得到一美丽子串,美丽子串的定义为:不能有连续有限个相同字符的的串。每个字符的个数用Ca,Cb。。。。Cz代替,字符串中。输入:第一行包含一个字符串s,1<=s的长度<=10^5第二行包含26个整数表示0输出:输出最长美丽子串的长度样例输入:aabaacaa10 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0原创 2017-09-15 10:40:03 · 746 阅读 · 0 评论 -
Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as原创 2017-06-02 08:37:10 · 219 阅读 · 0 评论 -
Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.A sudoku原创 2017-06-01 21:41:36 · 204 阅读 · 0 评论 -
Palidrome 判断某数字是否为回文
Determine whether an integer is a palindrome. Do this without extra space.提示:取数字后半部分的倒序,和数字的前半部分比较代码如下:public class PalindromeNumber { public boolean isPalindrome(int x){ if(x<0 || x!原创 2017-05-16 18:50:36 · 585 阅读 · 0 评论 -
Implement atoi to convert a string to an integer.
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca原创 2017-05-16 17:14:23 · 464 阅读 · 0 评论