![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
diu_brother
阿里巴巴集团 javacoder
展开
-
leetcode 172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.看到这道题,一开始没怎么想就直接搞的解法如下:public class Solution { public int trailingZeroes(原创 2016-07-12 21:27:37 · 280 阅读 · 0 评论 -
java 实现单链表的反转
题目:Reverse a singly linked list.实现算法:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class原创 2016-03-20 11:38:16 · 355 阅读 · 0 评论 -
java 求连续子数组的最大和
题目:输入一个整型数组,里面有正数也有负数,数组中一个或连续多个整数组成一个子数组,求所有子数组和的最大值。要求时间复杂度为O(n)解题思路:从头到尾遍历整个数组,用一个整数记录子数组和的最大值,如果子数组和大于这个值,则更新最大值,如果子数组和为负,子数组和清零,然后继续往后遍历。考虑数组全是负数、数组为空等边界情况即可。算法如下:public class Solution { publi原创 2016-03-25 10:49:17 · 1886 阅读 · 0 评论 -
java 蛙跳台阶问题解决算法
题目:一只青蛙可以一次跳上一级台阶,也可以一次跳上二级,求该青蛙跳上一个n级台阶总共有多少种跳法解题思路:我们把n级台阶的跳法看成是n的函数,记为f(n),当n>2时,第一次调的时候就有两种不同的选择,一是第一次跳一级,则此时的跳法数目等于后面剩下的n-1级台阶的跳法数目,即为f(n-1);另一种选择是一次跳2级,此时跳法数目等于后面剩下的n-2级台阶跳法数目,即为f(n-2).因此n级台阶不同跳法原创 2016-03-20 21:57:35 · 1468 阅读 · 0 评论 -
java 判断整数二进制中1的个数
题目:请实现一个函数,输入一个整数,输出该数二进制中1的个数。解法1:考虑到输入的整数可能是负数,所以不能使用右移运算,这里可以采取左移运算。这个解法中循环的次数等于整数二进制的位数。比如java int类字节为32位,则要循环32次public int numberOf1(int n){ int count = 0; int flag = 1; wh原创 2016-03-20 22:55:30 · 5550 阅读 · 1 评论 -
java 二叉搜索树寻找最小跟结点
题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betwee原创 2016-03-21 16:37:43 · 778 阅读 · 0 评论 -
从1到n整数中1出现的次数
题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数,例如输入12,则从1到12这些整数中包含1的数字有1,10,11和12,1一共出现了5次。解法一:编程之美中给出的解法如下public class Solution { public int NumberOf1Between1AndN_Solution(int n) { /**编程之美上给出的规律原创 2016-03-27 16:11:39 · 344 阅读 · 0 评论 -
java 简单的“add digits”算法
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digi原创 2016-03-16 00:12:48 · 575 阅读 · 0 评论 -
java “数石子”游戏算法
题目: You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be原创 2016-03-16 00:10:51 · 741 阅读 · 0 评论 -
java 找出n个元素数组中重复次数最多的数(假设出现次数大于n/2)
题目:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element alwa原创 2016-03-19 00:37:33 · 3715 阅读 · 2 评论 -
java 识别数组中是否具有重复元素
题目:Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element原创 2016-03-18 23:39:20 · 2854 阅读 · 0 评论 -
java实现二叉树已知先序遍历和中序遍历求后序遍历
分三步走 (1)确定树的根节点。树根是当前树中所有元素在先序遍历中最先出现的元素,即先序遍历的第一个结点就是二叉树的跟 (2)求解树的子树。找到根在中序遍历的位置,位置左边就是二叉树的左孩子,位置右边是二叉树的右孩子,如果跟结点左边或右边为空,那么该方向子树为空;如果根节点左边和右边都为空,那么根节点已经为叶子节点。 (3)对二叉树的左、右孩子分别进行步骤(1)(2),直到求出二叉树的结构为止原创 2016-03-18 20:46:37 · 4676 阅读 · 0 评论 -
leetcode 102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 /原创 2016-07-11 23:00:28 · 297 阅读 · 0 评论 -
java数组去重
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place w原创 2016-07-09 22:47:16 · 443 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).For example: Given binary tree {3,9,20,#,#,15,7}, 3原创 2016-05-14 11:03:45 · 272 阅读 · 0 评论 -
java 比较两个二叉树是否相等
题目: Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 解决算法:/** * De原创 2016-03-16 11:50:42 · 3338 阅读 · 0 评论 -
java 判断二维数组中是否存在某个数
题目:给定一个二维数组,判断其中是否存在某个数 例如:给定数组{{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}},判断7是否存在于数组中我的解决算法如下:public boolean find(int[][] matrix,int rows,int columns,int number){ if(matrix!=null&&rows>0&&原创 2016-03-17 00:17:45 · 5057 阅读 · 2 评论 -
java 删除LinkedList链表中的结点(只给出一个结点)
题目:Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. 实现:/** * Definition for singly-linked list. * public class ListNode { * int val;原创 2016-03-16 10:48:15 · 1756 阅读 · 0 评论 -
java 求二叉树最大深度算法
题目:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.下面是我的算法实现:/** * Definition for a binary原创 2016-03-16 00:14:52 · 3594 阅读 · 0 评论 -
java 不复制字符串前提下替换字符串中的空格算法实现O(n)
场景:在不复制字符串前提下,将某一字符串中空格替换为”%20”思路:在不复制字符串前提下,如果采取从前往后遍历字符串,那么除了第一个空格之外,后面的空格的移动次数都不止一次。所以采取从后往前遍历的方式,先计算出字符串空格数,然后对字符串进行扩容,使用两个指针进行一前一后遍历即可实现算法实现如下/** * 将字符串中空格替换为"%20"算法 * @param string原创 2016-03-17 15:52:48 · 686 阅读 · 0 评论 -
java 反转二叉树算法
题目:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1下面是我给出的算法:/** * Definition for a binary tree node. * public class TreeNode {原创 2016-03-16 00:07:48 · 5943 阅读 · 0 评论 -
java 移动数字“0”算法
题目: Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your f原创 2016-03-16 00:04:48 · 1138 阅读 · 0 评论 -
java 实现给定一个excel表中的列标题,返回相应的行号
题目:Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ... Z -> 26原创 2016-03-16 20:54:57 · 2491 阅读 · 0 评论 -
java 爬n阶楼梯问题
题目: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? 解题思路:爬楼梯一次可以爬1阶或2阶,通项公式是f(n) = f(原创 2016-04-05 13:35:31 · 2245 阅读 · 1 评论 -
java 判断一个数是否为“开心数”
题目:Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares o原创 2016-04-06 15:41:37 · 1736 阅读 · 0 评论 -
java 判断从0到n的二进制中‘1’的个数并返回数组
题目:Given a non negative integer number num.For every numbers i in the range 0<=i<=num calculate the number of 1's in their binary representation and return them as an array.Example :for num = 5 you sh原创 2016-04-11 20:36:48 · 1197 阅读 · 0 评论 -
java 动态规划判断股票最大盈利问题
题目:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), d原创 2016-04-23 16:07:38 · 3622 阅读 · 1 评论 -
java 合并两个排序的链表
题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.算法如下:/** * Definition for singly-linked list. * public原创 2016-04-23 16:53:26 · 1286 阅读 · 0 评论 -
java 交换链表中所有相邻结点的顺序
题目: Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may原创 2016-04-23 17:28:55 · 1441 阅读 · 0 评论 -
java 动态规划问题(2)
题目: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent h原创 2016-04-27 11:51:39 · 450 阅读 · 0 评论 -
java 判断一个二叉树是不是平衡二叉树
题目:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ原创 2016-04-27 17:48:46 · 2446 阅读 · 1 评论 -
java 删除linkedlist链表中重复元素
题目:Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 算法如下:/** * Definition for sin原创 2016-04-05 12:49:35 · 5588 阅读 · 0 评论 -
java 找出数组中只出现一次的数字
题目:一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。 算法如下:import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;//num1,num2分别为长度为1的数组。传出参数//将num1[0],num2[0]设置为返回原创 2016-04-01 21:00:58 · 3662 阅读 · 0 评论 -
java 求二叉树的深度
题目:输入一个二叉树的根节点,求该数的深度。此题比较简单,主要考察递归思想,算法实现如下:/**public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }原创 2016-03-29 10:48:34 · 2775 阅读 · 0 评论 -
java 判断两个String 是否是相同字母异序词
题目:Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false. 下面是我的实现算法:public class So原创 2016-03-16 18:32:09 · 5096 阅读 · 1 评论 -
java 实现String 的contains方法
下面是我的实现算法:public class Solution { public boolean isAnagram(String s, String t) { char[] array1 = s.toCharArray(); char[] array2 = t.toCharArray(); int j = 0; for(int原创 2016-03-16 18:05:46 · 4254 阅读 · 2 评论 -
java 把数组排成最小的数
题目: 输入一个正整数数组,把数组中所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个,例如输入数组{3,32,321},则打印出这3个数字中能排成的最小数字321323算法如下:public class Solution { public String PrintMinNumber(int [] numbers) { StringBuffer buffer =原创 2016-03-27 19:18:16 · 554 阅读 · 0 评论 -
java 求解第n个丑数
题目:我们把只包含因子2,3和5的数称为丑数(Ugly Number),求从小到大的顺序第n的丑数,例如6,8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当作第1个丑数第一种解法:最简单粗暴野蛮也是效率最低的一种解法是:public class Solution { public int GetUglyNumber_Solution(int index) { int原创 2016-03-27 21:27:41 · 5023 阅读 · 2 评论 -
java 求解字符串中第一次出现的字符的位置
题目:在字符串中找出第一个只出现一次的字符的位置,如输入“abaccdeff”,则输出“2”。解法:以空间换时间,借助Hashmap实现import java.util.HashMap;import java.util.Map;import java.util.Set;public class Solution { public int FirstNotRepeatingChar(Str原创 2016-03-27 22:45:10 · 15423 阅读 · 1 评论 -
Java 求解数组中的逆序对
题目:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。 解题思路:本题最直观的方法是使用双重循环遍历数组,每遍历一个值便将其与后面的元素比较,这种解法的算法复杂度为O(n^2),下面提供一种复杂度为O(n)的算法public class Solution { int count = 0; public int原创 2016-03-28 18:22:35 · 1670 阅读 · 0 评论