358.K 距离间隔重排字符串 知识点:哈希表:贪心:堆:队列 题目描述 给你一个非空的字符串 s 和一个整数 k,你要将这个字符串中的字母进行重新排列,使得重排后的字符串中相同字母的位置间隔距离至少为 k. 所有输入的字符串都由小写字母组成,如果找不到距离至少为 k 的重排结果,请返回一个空字符串…
Given a non-empty string str and an integer k, rearrange the string such that the same characters are at least distance k from each other. All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empt…
Given a non-empty string str and an integer k, rearrange the string such that the same characters are at least distance k from each other. All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empt…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计奇数字符出现次数 日期 题目地址:https://leetcode-cn.com/problems/construct-k-palindrome-strings/ 题目描述 给你一个字符串 s 和一个整数 k .请你用 s 字符串中 所有字符 构造 k 个非空 回文串 . 如果你可以用 s 中所有字符构造 k 个回文字符串,那么请你返回 True…
LeetCode初级算法的Python实现--字符串 # 反转字符串 def reverseString(s): return s[::-1] # 颠倒数字 def reverse(x): if x < 0: flag = -2 ** 31 result = -1 * int(str(x)[1:][::-1]) if result < flag: return 0 else: return result else: flag = 2 ** 31 - 1 result = int(str(x)[…
题目链接:https://www.rqnoj.cn/problem/514 题意: 设有字符串X,我们称在X的头尾及中间插入任意多个空格后构成的新字符串为X的扩展串,如字符串X为”abcbcd”,则字符串”abcb_cd”,”_a_bcbcd_”和”abcb_cd_”都是X的扩展串,这里“_”代表空格字符. 如果A1是字符串A的扩展串,B1是字符串B的扩展串,A1与B1具有相同的长度,那么我扪定义字符串A1与B1的距离为相应位置上的字符的距离总和,而两个非空格字符的距离定义为它们的ASCII码的…
LeetCode:比较含退格字符串[844] 题目描述 给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", T = "ad#c" 输出:true 解释:S 和 T 都会变成 “ac”. 示例 2: 输入:S = "ab##", T = "c#d#" 输出:true 解释:S 和 T 都会变成 “”. 示例 3: 输入:…
2485 小b重排字符串 2 秒 262,144 KB 5 分 1 级题   小b有一个字符串S,现在她希望重排列S,使得S中相邻字符不同. 请你判断小b是否可能成功. 样例解释:将"aab"重排为"aba"即可. 收起   输入 Ã输入一个只包含小写字母的字符串S,其长度n满足1≤n≤500 输出 如果可能,输出"Possible": 否则,输出"Impossible". 输入样例 aab 输出样例 Possible思路:统…
Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place wit…
LeetCode 25 k组一个翻转链表 TITLE 示例 1: 输入:head = [1,2,3,4,5], k = 2 输出:[2,1,4,3,5] 示例 2: 输入:head = [1,2,3,4,5], k = 3 输出:[3,2,1,4,5] 示例 3: 输入:head = [1,2,3,4,5], k = 1 输出:[1,2,3,4,5] 示例 4: 输入:head = [1], k = 1 输出:[1] (PS:还是加上title吧不然太难受了) 前言:LeetCode难度hard…
Given a non-empty string str and an integer k, rearrange the string such that the same characters are at least distance k from each other. All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empt…
原题链接在这里:https://leetcode.com/problems/rearrange-string-k-distance-apart/description/ 题目: Given a non-empty string s and an integer k, rearrange the string such that the same characters are at least distance k from each other. All input strings are gi…
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. Note: The length of num is less than 10002 and will be ≥ k. The given num does not contain any leading zero. Ex…
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 这道求交织相错的字符串和之前…
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred. Example 1: Input: [1,2,3,4,5], k=4, x=3 Outp…
Hard! 题目描述: 给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树. 下图是字符串 s1 = "great" 的一种可能的表示形式. great / \ gr eat / \ / \ g r e at / \ a t 在扰乱这个字符串的过程中,我们可以挑选任何一个非叶节点,然后交换它的两个子节点. 例如,如果我们挑选非叶节点 "gr" ,交换它的两个子节点,将会产生扰乱字符串 "rgeat" . rgea…
Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it equals Y. For example, "tars" and "rats" are similar (swapping at positions 0 and 2), and "rats" and "arts" are si…
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 100…
这是悦乐书的第351次更新,第376篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第6题(顺位题号是12).罗马数字由七个不同的符号表示:I,V,X,L,C,D和M. 符号 值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如,2用罗马数字写成II,由两个I加在一起.12写为XII,简称为X + II. 27写成XXVII,即XX + V + II. 罗马数字通常从左到右从最大到最小.但是,四个数字不是IIII.相反,4写为IV.…
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define a pair (u,v) which consists of one element from the first array and one element from the second array. Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) wit…
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements.    Your algorithm's time complexity must be…
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider that…
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这道题让我们合并k个有序链表,之前我们做过一道Merge Two Sorted Lists 混合插入有序链表,是混合插入两个有序链表.这道题增加了难度,变成合并k个有序链表了,但是不管合并几个,基本还是要两两合并.那么我们首先考虑的方法是能不能利用之前那道题的解法来解答此题.答案是肯定的,但是需要修改…
A typical solution is heap based - "top K". Complexity is O(nlgk). typedef pair<int, unsigned> Rec; struct Comp { bool operator()(const Rec &r1, const Rec &r2) { return r1.second > r2.second; } }; class Solution { public: vector…
题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { public: int myAtoi(string str) { ,i = ; for(;i<str.size() && str[i] == ' '; ++i); ] ="+-" ; ] ={,-}; ;j<;++j){ if(str[i] == ss[j]) {…
#include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ,e=,s=; ,min=-; ; ]={}; unsigned ; while(*str==' '){ //过滤掉连续空格 str++; } ')){ if(*str=='-'||*str=='+'){ //过滤掉正负号 if(*str=='-') f=-; str++; } ') //过滤掉前面的无用的0…
https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走路List<ListNode>.对当中每一个链表同当前链表做一遍类似于归并排序最后一步的merge操作. 算法复杂度是O(KN) public class Solution { ListNode mergeTwoLists(ListNode list1, ListNode list2) { Lis…
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first. Example 1: Inpu…
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesi…
Given a string, your task is to count how many palindromic substrings in this string. The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters. Example 1: Input: "abc" Ou…