题目要求 算法分析利用栈解决.按索引遍历{ 如果栈为空,将索引入栈, 如果栈不空,判断,当前索引对应的温度是否比栈顶索引对应的温度高,{ 如果高则出栈,并计算索引差,所得结果存入返回数组对应的索引上,然后返回上一步重新比较栈顶元素和当前元素的大小 如果低则入栈。 }}代码展示(C#)publicclass Solution {publicint[] DailyTemperatures(int[] T) {int[] ret = newint[T.Length];Stack<int> stack = ne...
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重复数字,并且返回不重复数字的个数 遍历操作: 可以使用新的for循环 for (int n : nums){} 每次进行对比,并且更新第一个遇到不相等的元素的下标为i对数组进行重新赋值操作 当数组长度大于1时,ans初值为1,当数组长度为0时,返回0 参考代码 : package leetcode_50;/**** * @author pengfei_zheng* 移除有序数组...
Given a binary tree, return the preorder traversal of its nodes‘ values.
For example:
Given binary tree {1,#,2,3}, 12/3return [1,2,3].Note: Recursive solution is trivial, could you do it iteratively?/*** Definition for binary tree* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution
{
public:v...
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).Here is an example:S = "rabbbit", T = "rabbit"Return ...
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
这个题考查的是KMP算法。先求特征向量,然后再进行匹配,确实能够大大提高效率。code例如以下:class Solution {
public:char *strStr(char *haystack, char *needle) {if(strlen(haystack)==0&&strlen(needle)==0)return haystack;if(strlen(haystack)==0&&strlen(needle)!=0)return NULL;if(...
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 puzzle... ...and its solution numbers marked in red. 终于到了朝思暮想的sudoku solver了, 有了Valid Sudoku的基础,我知道了用哈希表可以很方便的判断冲突。思路还是遍历这个board,这次是找到为空的位置,然后对可能的结果集进...
回文数题目描述:给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。示例说明请见LeetCode官网。来源:力扣(LeetCode)链接:https://leetcode-cn.com/probl...著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。解法一:前后遍历如果是负数,直接返回false;否则,将整数转化为字符串...
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)The solution set must not contain duplicate triplets. For example, given array S = {-1 0 1 2 -1 -4},A solution set is:(-1, 0, 1)(-1, -1, 2)
Have you met this...
地址:https://leetcode.com/problems/two-sum/题意:给定一个数组和一个target, 如果数组中有两个数的和恰好等于target, 返回它们的下标. 题解:思路有很多, 随便搞一搞就可以比如: 先排序, 然后遍历每一个数, 二分查找另一个因为python自带字典树(dist), 所以这里给出更方便的这种做法: 先按照 d = { 值 : 下标 } 建树, 然后遍历每一个数, 直接查找另一个, 返回这两个数的下标即可代码: 1class Solution(object):2def twoSum(sel...
Palindrome NumberDetermine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:
Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, note the restriction of using extra space.You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed ...
编号145:二叉树的后序遍历给定一个二叉树,返回它的 后序 遍历。示例:
输入: [1,null,2,3] 12/3 输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?思路具体代码如下://Definition for a binary tree node.
type TreeNode struct {Val intLeft *TreeNodeRight *TreeNode
}
递归算法/*递归三部曲
1.确定递归函数的参数和返回值
2.确定终止条件
3.确定单层递归的逻辑
*///1.确定递归函数的参数和返回值:因为要打...
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty() -- Return whether the queue is empty.Notes:You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.Depending on your ...
颠倒给定的 32 位无符号整数的二进制位进阶: 如果多次调用这个函数,你将如何优化你的算法?address#!/usr/bin/python
# -*- coding:utf-8 -*-# 方法1
def reverseBits(n: int) -> int:result = 0for i in range(32):# 此处+优先符高于&result = (result << 1) + (n & 1)n >>= 1return resultif __name__ == '__main__':message = 43261596print(reverseBits(message))
Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain.Input: candies = [1,1,2,2,3,3]
Output: 3
Explanation:
There are three different ki...
102. Binary Tree Level Order TraversalGiven 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
/ 15 7return its level order traversal as:[
[3],
[9,20],
[15,7]
]解题思路:采用双队列来处理。用当前队列current来处理本层的所有节点,将本层信息记录在vector中...