每日一道编程题
每日一道编程题
舜绪
以兴趣为驱动
展开
-
59 大数加法
leetcode上不了,做一下牛客网的题目以字符串的形式读入两个数字,再以字符串的形式输出两个数字的和。输入描述:输入两行,表示两个数字a和b,-109 <= a , b <= 109 ,用双引号括起。输出描述:输出a+b的值,用双引号括起。输入例子1:“-26”“100”输出例子1:“74”分析思路一:定义String变量str1和str2分别存储输入...原创 2020-03-25 00:00:46 · 307 阅读 · 0 评论 -
58 Balance a Binary Search Tree
题目Given a binary search tree, return a balanced binary search tree with the same node values.A binary search tree is balanced if and only if the depth of the two subtrees of every node never differ ...原创 2020-03-23 16:16:32 · 158 阅读 · 0 评论 -
57 Find the Distance Value Between Two Arrays
题目Given two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays.The distance value is defined as the number of elements arr1[i] such that there is not a...原创 2020-03-22 22:05:28 · 218 阅读 · 0 评论 -
56 Height Checker
题目Students are asked to stand in non-decreasing order of heights for an annual photo.Return the minimum number of students that must move in order for all students to be standing in non-decreasing o...原创 2020-03-21 22:39:49 · 151 阅读 · 0 评论 -
55 Construct Binary Search Tree from Preorder Traversal
题目Return the root node of a binary search tree that matches the given preorder traversal.(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a va...原创 2020-03-20 20:19:41 · 123 阅读 · 0 评论 -
54 Minimum Number of Steps to Make Two Strings Anagram
题目Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character.Return the minimum number of steps to make t an anagram of s.An Anagram o...原创 2020-03-19 21:20:42 · 116 阅读 · 0 评论 -
53 Lucky Numbers in a Matrix
题目Given a m * n matrix of distinct numbers, return all lucky numbers in the matrix in any order.A lucky number is an element of the matrix such that it is the minimum element in its row and maximum ...原创 2020-03-18 19:04:11 · 134 阅读 · 0 评论 -
52 Generate a String With Characters That Have Odd Counts
题目Given an integer n, return a string with n characters such that each character in such string occurs an odd number of times.The returned string must contain only lowercase English letters. If ther...原创 2020-03-17 12:10:46 · 148 阅读 · 0 评论 -
51 Sort the Matrix Diagonally
题目分析题意:给一个m*n矩阵,按对角线进行升序排序。将矩阵下标标出来解题关键在于如何找到“对角线”。我们人是可以直观看出来的,但是计算机不能。通过观察发现,对角线的元素必有:i-j相同。根据这个特性,实现对角遍历,将每个对角线视为一个数组算法创建一个Map遍历矩阵的每行每列 如果在Map中不存在i-j这个key,就将其加入Map中 将当前值加入到i-j这个数组中对M...原创 2020-03-16 12:04:08 · 135 阅读 · 0 评论 -
50 Maximum Binary Tree
题目Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:The root is the maximum number in the array.The left subtree is the maximum tree constructed ...原创 2020-03-15 21:25:28 · 91 阅读 · 0 评论 -
49 Encode and Decode TinyURL
题目Note: This is a companion problem to the System Design problem: Design TinyURL.TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and i...原创 2020-03-14 15:05:53 · 245 阅读 · 0 评论 -
48 Insert into a Binary Search Tree
题目Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that...原创 2020-03-13 14:35:34 · 94 阅读 · 0 评论 -
47 Find a Corresponding Node of a Binary Tree in a Clone of That Tree
题目Given two binary trees original and cloned and given a reference to a node target in the original tree.The cloned tree is a copy of the original tree.Return a reference to the same node in the cl...原创 2020-03-12 17:00:22 · 241 阅读 · 0 评论 -
46 Count Negative Numbers in a Sorted Matrix
题目Given a m * n matrix grid which is sorted in non-increasing order both row-wise and column-wise.Return the number of negative numbers in grid.Example 1:Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,...原创 2020-03-11 20:22:44 · 112 阅读 · 0 评论 -
45 Increasing Decreasing String
题目Given a string s. You should re-order the string using the following algorithm:Pick the smallest character from s and append it to the result.Pick the smallest character from s which is greater t...原创 2020-03-10 16:03:45 · 190 阅读 · 0 评论 -
44 Binary Search Tree to Greater Sum Tree
题目Given the root of a binary search tree with distinct values, modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node....原创 2020-03-09 18:03:28 · 102 阅读 · 0 评论 -
43 Max Increase to Keep City Skyline
题目In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts ...原创 2020-03-07 13:15:06 · 107 阅读 · 0 评论 -
42 Deepest Leaves Sum
题目Given a binary tree, return the sum of values of its deepest leaves.Constraints:The number of nodes in the tree is between 1 and 10^4.The value of nodes is between 1 and 100./** * Definition...原创 2020-03-06 09:17:43 · 113 阅读 · 0 评论 -
41 Group the People Given the Group Size They Belong To
题目There are n people whose IDs go from 0 to n - 1 and each person belongs exactly to one group. Given the array groupSizes of length n telling the group size each person belongs to, return the groups...原创 2020-03-05 12:12:42 · 159 阅读 · 0 评论 -
40 How Many Numbers Are Smaller Than the Current Number
题目Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j’s such that j != i and nums[j] &...原创 2020-03-04 15:43:17 · 290 阅读 · 0 评论 -
39 Sum of Nodes with Even-Valued Grandparent
为啥没有38?因为38没过审。今天点了十张图,登陆上了。题目Given a binary tree, return the sum of values of nodes with even-valued grandparent. (A grandparent of a node is the parent of its parent, if it exists.)If there ar...原创 2020-03-03 13:12:07 · 143 阅读 · 0 评论 -
37 Number of Steps to Reduce a Number to Zero
题目Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.Example 1:I...原创 2020-02-29 13:05:20 · 185 阅读 · 0 评论 -
36 Sort Integers by The Number of 1 Bits
题目Given an integer array arr. You have to sort the integers in the array in ascending order by the number of 1’s in their binary representation and in case of two or more integers have the same numbe...原创 2020-02-28 20:23:56 · 181 阅读 · 0 评论 -
35 Delete Columns to Make Sorted
题目We are given an array A of N lowercase letter strings, all of the same length.Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.Fo...原创 2020-02-27 20:10:03 · 111 阅读 · 0 评论 -
34 N-ary Tree Preorder Traversal
题目跟上一篇N叉树后序遍历基本一致,区别就在于本题要求使用前序遍历。分析还是老办法,只需要利用遍历“最小元”的思想进行递归即可。后序遍历: n叉树后序遍历的最小元:先遍历其他节点-->再遍历根节点从题意可知,根节点是root,其他节点是root.children中的节点。因此算法就是,先递归遍历root.children的所有节点,再遍历根节点前序遍历:先遍历根节点--&g...原创 2020-02-27 19:27:22 · 81 阅读 · 0 评论 -
33 N-ary Tree Postorder Traversal
题目Given an n-ary tree, return the postorder traversal of its nodes’ values.Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the nul...原创 2020-02-26 16:01:40 · 82 阅读 · 0 评论 -
32 Number of Recent Calls
题目Write a class RecentCounter to count recent requests.It has only one method: ping(int t), where t represents some time in milliseconds.Return the number of pings that have been made from 3000 mil...原创 2020-02-25 20:46:12 · 107 阅读 · 0 评论 -
31 Search in a Binary Search Tree
关注 每天一道编程题 专栏,一起学习进步。题目Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node’s value equals the given value. Return the subtree rooted w...原创 2020-02-24 11:01:53 · 103 阅读 · 0 评论 -
30 Peak Index in a Mountain Array
关注 每天一道编程题 专栏,一起学习进步。题目Let’s call an array A a mountain if the following properties hold:A.length >= 3There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] <...原创 2020-02-23 14:52:53 · 97 阅读 · 0 评论 -
29 DI String Match
题目Given a string S that only contains “I” (increase) or “D” (decrease), let N = S.length.Return any permutation A of [0, 1, …, N] such that for all i = 0, …, N-1:If S[i] == "I", then A[i] < A[i+...原创 2020-02-21 15:32:15 · 118 阅读 · 2 评论 -
28 Array Partition I
题目Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as pos...原创 2020-02-20 14:21:30 · 84 阅读 · 0 评论 -
27 Hamming Distance
题目The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note:0 ≤ x, y < 2...原创 2020-02-19 11:47:51 · 74 阅读 · 0 评论 -
26 Unique Number of Occurrences
题目Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique.Example 1:Input: arr = [1,2,2,1,1,3]Output: true...原创 2020-02-19 11:26:27 · 136 阅读 · 0 评论 -
25 Squares of a Sorted Array
题目Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.Example 1:Input: [-4,-1,0,3,10]Output: [0,1,9,16,1...原创 2020-02-17 22:22:31 · 100 阅读 · 0 评论 -
24 Merge Two Binary Trees
题目Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tr...原创 2020-02-15 13:32:06 · 78 阅读 · 0 评论 -
23 Robot Return to Origin
题目There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.The move sequence is repr...原创 2020-02-14 20:59:08 · 147 阅读 · 0 评论 -
22 Self Dividing Numbers
题目A self-dividing number is a number that is divisible by every digit it contains.For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.Also, a self-dividi...原创 2020-02-13 13:35:27 · 103 阅读 · 0 评论 -
21 N-Repeated Element in Size 2N Array
题目In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times.Return the element repeated N times.Example 1:Input: [1,2,3,3]Output: 3Example 2:...原创 2020-02-11 12:59:20 · 128 阅读 · 0 评论 -
20 Sort Array By Parity
题目Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.You may return any answer array that satisfies this condi...原创 2020-02-10 13:00:47 · 116 阅读 · 0 评论 -
19 Flipping an Image
题目Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.To flip an image horizontally means that each row of the image is reversed. For exa...原创 2020-02-09 21:26:31 · 95 阅读 · 0 评论