LeetCode-897-递增顺序搜索树
题目
给你一棵二叉搜索树,请你 按中序遍历 将其重新排列为一棵递增顺序搜索树,使树中最左边的节点成为树的根节点,并且每个节点没有左子节点,只有一个右子节点。
示例 1:输入:root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
输出:[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
示例 2:输入:root = [5,1,7]
输出:[1,null,5,null,7]
提示:
树中节点数的取值范围是 [1, 100]...
很有意思的一道题,值得好好思考,虽然难度只有Mid,但是个人觉得不比Hard简单题目描述
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例 1:输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]示例 2:输入:nums = []
输出:[]示例 3:输入:nums = [0]
输出:[]提示:0 <= nums.leng...
知识点:数组操作
类似题目:
901 股票价格跨度 3 31.4% 中等
121 买卖股票的最佳时机 46 50.4% 简单
122 买卖股票的最佳时机 II 43 55.0% 简单
123 买卖股票的最佳时机 III 17 39.0% 困难
309 最佳买卖股票时机含冷冻期 13 49.3% 中等
188 买卖股票的最佳时机 IV 8 28.0% 困难
714 买卖股票的最佳...
Binary Tree Preorder TraversalGiven a binary tree, return the preorder traversal of its nodes‘ values.For example:Given binary tree {1,#,2,3}, 12/3
return [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), righ...
题目
给定一个 没有重复 数字的序列,返回其所有可能的全排列。
示例:输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]链接:https://leetcode-cn.com/problems/permutations
思路
用DFS求解
import java.util.ArrayList;
import java.util.List;
class Solution {public List<List<Integer>> permute(int[] nums) {int len = nums.length;List<List<Integer>> result = new ArrayList<>();List<Int...
第一期编程题(3道)
1、给定一个整数,编写一个函数来判断它是否是 2 的幂次方
示例 1: 输入: 1 输出: true 解释: 20 = 1 示例 2: 输入: 16 输出: true 解释: 24 = 16 示例 3: 输入: 218 输出: false
思路:
一、观察2的幂的整数,看它们的二进制位表示 (假设用8位二进制表示)。 1、不用管负指数。指数为负结果就是小数而不是整数。 2、不用管负数。指数为正,结果一定是正整数。 二、可以发现,2的幂的整数的二进制位表示只有一个...
问题:编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。 示例 1:输入:["h","e","l","l","o"]输出:["o","l","l","e","h"]示例 2:输入:["H","a","n","n","a","h"]输出:["h","a","n","n","a","H"]来源:力扣(LeetCode)链接:...
int findPeakElement(int* nums, int numsSize){int l = 0, r = numsSize - 1;while (l < r) {int mid = (l + r) / 2;if (nums[mid] > nums[mid + 1])r = mid;elsel = mid + 1;}return l;
} 原文:https://www.cnblogs.com/ganxiang/p/14183363.html
思路:
注意到给定字符串的长度是偶数,所以无论如何操作,所有可能的字符串也只有10 * 10 * n(n是字符串的长度)种,可以暴力枚举。
实现: 1 class Solution2 {3 public:4 string findLexSmallestString(string s, int a, int b)5 {6 int n = s.length();7 queue<string> q;8 q.push(s);9 unordered_set<string> st;
10 string res = s;
11 while (!q.empty())
12 ...
LeetCode笔记
leetcode第一题就被虐翻,在此记录失败历程
num.size() …nums的长度 return{i,j} …返回i,j return{} …返回空 unordered_map<int,int> …创建哈希表 auto …自动定义 hashtable.find(number) …hashtable中number的地址 hashtable.end() …hashtable尾项的地址 it->second …it所指的东西 if(a+b==c) …if后有括号
leetcode:#206 反转链表
题目详情
java实现
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution { //头插法public ListNode reverseList(ListNode head) {if (head == null) {return null;}ListNode revers...
抽象为把序列分成D段,求和最大的段的最小值
下界为最大值,上界为序列和,二分结果,每次去验证是否合适即可class Solution {
public:int shipWithinDays(vector<int>& weights, int D) {int total = 0;int max_v = weights[0];for(int i = 0; i < weights.size(); i++) max_v = max(max_v, weights[i]), total += weights[i];int x = max_v, y = total;int cnt = 1, cnt_s;while(x < y){cnt_s = 0;cnt = 1;int mid = (x + y) / 2...
题目:(Stack)Given n non-negative integers representing the histogram‘s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest rectangle is shown in the shaded area, which has area = 10 unit. For example,Given height = [2,1,5,6,2,3],return 10. 题解:参考http://w...
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), design an algorithm to find the maximum profit.解题思路:本题原来做错的原因是我原以为只要找到最小值和最大值,相减就能得到最大的利润。但股票买卖是有顺序的,先买后卖。因此最小值必须是左边的,然后比较右边的...
LeetCode House Robber II题目思路思路来源于Discuss。
一是在Robber这题中的O(n)解法;
二是在Robber这题中,我们只需要分别考虑包含了nums[0]和nums[n-1]的情况即可。
注意这里的包含并不是说Robber一定要偷num[0]或nums[n-1],只是说考虑进去的意思。代码#define max(a, b) ((a)>(b)?(a):(b))int rob(int* nums, int numsSize) {if (numsSize <= 0) return0;if (numsSize == 1) return nums[0];int evenMax, ooddMax, includ...