Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
Note that 1 is typically treated as an ugly number.题意:判断一个数是不是丑数。只能被2,3,5因子的。public class Solution {
public boolean isUgly(int num) {
...
Given a 2D binary matrix filled with 0‘s and 1‘s, find the largest square containing all 1‘s and return its area.For example, given the following matrix:1 0 1 0 0
1 0 11 1
1 1 11 1
1 0 0 1 0
Return 4.解题思路:dp问题,用一个dp[i][j]保存matrix[i][j]作为右下节点的时候的最大矩形的边长,JAVA实现如下: public int maximalSquare(char[][] matrix) {if (matrix.length == 0 || matrix[0].length == 0)retu...
1. Description:2. Examples:3.Solutions: 1/** 2 * Created by sheepcore on 2019-05-073 * Definition for singly-linked list.4 * public class ListNode {5 * int val;6 * ListNode next;7 * ListNode(int x) { val = x; }8 * }9*/10class Solution {
11public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
12 ListNode head = new ListNode(-1);
13 ListNode p = l1, q = l2, tail = hea...
题目描述:Description:Count the number of prime numbers less than a non-negative number, n.解题思路:Let‘s start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrimefunction would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better?As we know the nu...
题目描述:Given an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion?解题思路:对给定的数求以3为底的对数,然后再将结果用于3的次幂,看是否与原来的数相同。代码如下:public class Solution {public boolean isPowerOfThree(int n) {return n <= 0 ? false : n == Math.pow(3, Math.round(Math.log(n) / Math.log(3)));}
}
原文:http://www...
<p>题目:</p><p><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">Given an integer array of size </span><span style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">n</span><span style="color: rgb(51, 51, 51); font-fa...
Sort a linked list using insertion sort.A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.With each iteration one element (red) is removed from the input data and inserted in-place into the sorted listAlgorithm of Insertion Sort:1Insertion sort iterates, consuming one input element each repetition, and growing a sorted outp...
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for ‘?‘ and ‘*‘.‘?‘ Matches any single character.
‘*‘ Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).Note:s could be empty and contains only lowercase letters a-z.p could be empty and contains only lowercase letters...
题目:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given 1->4->3->2->5->2 and x = 3,return 1->2->2->4->3->5.题意及分析:给出一个链表和一个值x,将链表中值小于x的点移动到值大于等于x的点之前,分别需要保持保持两部分中点的...
题意:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.题目:给定一个有序链表,删除所有重复的节点,剩余都是原链表中的不相同的节点元素。比如,给定1->2->3->3->4->4->5 ,返回1->2->5.给定1->1->1->2->3 ,返回2->3.算法分析:设置前后双指针,...
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.You may imagine that num[-1] = num[n] = -∞.For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number...
【009-Palindrome Number(回文数)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题 Determine whether an integer is a palindrome. Do this without extra space. 题目大意 判断一个数字是否是回访字数,不要使用额外的空间。 解题思路 为了不使用额外的空间,参考了其它的解决,那些解法看起来在isPalindrome方法中没有使用额外参数,但是却使用了方法调用,这个比一个整数消耗的空间更多 ,并没有达到...
描述Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.分析无代码 1publicclass IntegerToRoman {2 3publicstaticvoid main(String[] args) {4// TODO Auto-generated method stub 5int num = 187;6 System.out.println(intToRoman(num));7 }8 9publicstatic String intToRoman(int num) {
10int radix[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9,...
1. 具体题目给定两个整数 n 和 k,你需要实现一个数组,这个数组包含从 1 到 n 的 n 个不同整数,同时满足以下条件:① 如果这个数组是 [a1, a2, a3, ... , an] ,那么数组 [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] 中应该有且仅有 k 个不同整数;.② 如果存在多种答案,你只需实现并返回其中任意一种.示例 1: 输入: n = 3, k = 1 输出: [1, 2, 3] 解释: [1, 2, 3] 包含 3 个范围在 1-3 的不同整数, 并且 ...
Binary Tree Level Order Traversal IIGiven 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/ 9 20/ 15 7
return its bottom-up level order traversal as:[[15,7],[9,20],[3]
]https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ 与...