Java实现 LeetCode 316 去除重复字母】的更多相关文章

316. 去除重复字母 给定一个仅包含小写字母的字符串,去除字符串中重复的字母,使得每个字母只出现一次.需保证返回结果的字典序最小(要求不能打乱其他字符的相对位置). 示例 1: 输入: "bcabc" 输出: "abc" 示例 2: 输入: "cbacdcbc" 输出: "acdb" PS: 我把每一个数出现的次数都拿出来,我当前字符比我栈顶的小,并且我栈顶的字符还有多的在后面,我就可以把他替换了,记录一下是否使用 clas…
题目 贪心方法 用一个两个数组vector<int>cnt,vector<bool>in_right_place: string res:目前符合条件的字符串,到代码结束的时候,这个res才是正确的答案 cnt数组 用于记录每个char未来会出现的次数,in_right_place数组判断这个char是否已经在正确的位置上 什么叫做未来会出现呢? 我们拿样例做为例子: 输入: "bcabc" 我们先遍历一遍所有字符串记录cnt 结果是cnt['a']=1,cnt…
316. 去除重复字母 知识点:栈:单调 题目描述 给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次.需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置). 示例 输入:s = "bcabc" 输出:"abc" 输入:s = "cbacdcbc" 输出:"acdb" 解法一:单调 我们来仔细分析一下这道题目,它结合了很多知识点,因为题目中提出了很多要求. 1.去掉重复的:对于去重最常用的就是s…
JAVA中List对象去除重复值,大致分为两种情况,一种是List<String>.List<Integer>这类,直接根据List中的值进行去重,另一种是List<User>这种,List中存的是javabean对象,需要根据List中对象的某个值或某几个值进行比较去重.方法如下: 一.List<String>.List<Integer>对象去重复值. 这种情况的话,处理起来比较简单,通过JDK1.8新特性stream的distinct方法,可…
给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次.你必须保证返回结果是所有可能结果中的以字典排序的最短结果.例如:给定 "bcabc"返回 "abc"给定 "cbacdcbc"返回 "acdb" 详见:https://leetcode.com/problems/remove-duplicate-letters/description/ C++: class Solution { public: str…
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: "bcab…
520. 检测大写字母 给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如"leetcode". 如果单词不只含有一个字母,只有首字母大写, 比如 "Google". 否则,我们定义这个单词没有正确使用大写字母. 示例 1: 输入: "USA" 输出: True 示例 2: 输入: "FlaG"…
1. 循环list中的所有元素然后删除重复 public static List removeDuplicate(List list) { for ( int i = 0 ; i < list.size() - 1 ; i ++ ) { for ( int j = list.size() - 1 ; j > i; j -- ) { if (list.get(j).equals(list.get(i))) { list.remove(j); } } } return list; } 2. 通过H…
1. 循环list中的所有元素然后删除重复   public static List removeDuplicate(List list) { for ( int i = 0 ; i < list.size() - 1 ; i ++ ) { for ( int j = list.size() - 1 ; j > i; j -- ) { if (list.get(j).equals(list.get(i))) { list.remove(j); } } } return list; }   2.…
1. 循环list中的所有元素然后删除重复 public static List removeDuplicate(List list) { for ( int i = 0 ; i < list.size() - 1 ; i ++ ) { for ( int j = list.size() - 1 ; j > i; j -- ) { if (list.get(j).equals(list.get(i))) { list.remove(j); } } } return list; } 2. 通过H…
652. 寻找重复的子树 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两棵树重复是指它们具有相同的结构以及相同的结点值. 示例 1: 1 / \ 2 3 / / \ 4 2 4 / 4 下面是两个重复的子树: 2 / 4 和 4 因此,你需要以列表的形式返回上述重复子树的根结点. /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tr…
524. 通过删除字母匹配到字典里最长单词 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符串.如果答案不存在,则返回空字符串. 示例 1: 输入: s = "abpcplea", d = ["ale","apple","monkey","plea"] 输出: "apple" 示例…
466. 统计重复个数 定义由 n 个连接的字符串 s 组成字符串 S,即 S = [s,n].例如,["abc", 3]="abcabcabc". 另一方面,如果我们可以从 s2 中删除某些字符使其变为 s1,我们称字符串 s1 可以从字符串 s2 获得.例如,"abc" 可以根据我们的定义从 "abdbec" 获得,但不能从 "acbbe" 获得. 现在给出两个非空字符串 S1 和 S2(每个最多 10…
242. 有效的字母异位词 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram" 输出: true 示例 2: 输入: s = "rat", t = "car" 输出: false 说明: 你可以假设字符串只包含小写字母. 进阶: 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况? clas…
220. 存在重复元素 III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. 示例 1: 输入: nums = [1,2,3,1], k = 3, t = 0 输出: true 示例 2: 输入: nums = [1,0,1,1], k = 1, t = 2 输出: true 示例 3: 输入: nums = [1,5,9,1,5,9], k = 2, t = 3…
219. 存在重复元素 II 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. 示例 1: 输入: nums = [1,2,3,1], k = 3 输出: true 示例 2: 输入: nums = [1,0,1,1], k = 1 输出: true 示例 3: 输入: nums = [1,2,3,1,2,3], k = 2 输出: false PS: 滑动窗口 class So…
217. 存在重复元素 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [1,2,3,1] 输出: true 示例 2: 输入: [1,2,3,4] 输出: false 示例 3: 输入: [1,1,1,3,3,4,3,2,4,2] 输出: true class Solution { public boolean containsDuplicate(int[] nums) { Se…
1. 循环list中的所有元素然后删除重复 public   static   List  removeDuplicate(List list)  {         for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {             for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {                  if  (list.get(j).e…
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example: Given "bcabc&q…
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra mem…
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A = […
import java.util.*; /* 将自定义对象作为元素存到ArrayList集合中,并去除重复元素. 比如:存人对象.同姓名同年龄,视为同一个人.为重复元素. 思路: 1,对人描述,将数据封装进人对象. 2,定义容器,将人存入. 3,取出. List集合判断元素是否相同,依据是元素的equals方法. */ class Person { private String name; private int age; Person(String name,int age) { this.n…
在java开发中碰到了有些字符串是重复的,如果在进行业务处理要全部遍历太对的数据就会重复,所以在进行业务处理前进行一个去重操作. 这里由于业务需要所以先将字符串转化为string数组,使用split分割,然后将string数组一个个放到list里(list的remove可以将你不要的字符串删除掉,代参数的哦) 可以看到我使用的是list,在list里包含了一个contains函数,表示当前字符串是否与list里的元素有相同没有就add进list里 在最后还将list里的元素转化为string数组…
import java.util.Arrays; import java.util.HashSet; import java.util.Set; class Demo20 { public static void main(String[] args) { //int [] arr={1,2,3,3,4,4,4,4}; int [] arr={4,2,3,3,4,4,4,4}; //arr=delArr(arr); arr=delArrByHash(arr); //test(arr); Syst…
一.用List集合实现 int[] str = {5, 6, 6, 6, 8, 8, 7,4}; List<Integer> list = new ArrayList<Integer>(); for (int i=0; i<str.length; i++) { if(!list.contains(str[i])) { list.add(str[i]); } } System.out.println("去除重复后的list集合"+list); 输出结果是:…
一.用List集合实现   , , , , , , ,}; List<Integer> list = new ArrayList<Integer>(); ; i<str.length; i++) { if(!list.contains(str[i])) { list.add(str[i]); } } System.out.println("去除重复后的list集合"+list); 输出结果是: 去除重复后的list集合[5, 6, 8, 7, 4] 可以看…
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.…
Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.…
424. 替换后的最长重复字符 给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度. 注意: 字符串长度 和 k 不会超过 104. 示例 1: 输入: s = "ABAB", k = 2 输出: 4 解释: 用两个'A'替换为两个'B',反之亦然. 示例 2: 输入: s = "AABABBA", k = 1 输出: 4 解释: 将中间的一个'A'替换为'B',…
下拉框去除重复内容 <script type="text/javascript" src="http://www.joleye.com/libraries/javascript/jquery-1.5.1.min.js"></script> <a href="###">加载</a> <select> </select> <a href="###">…