九层之台,起于垒土,你我皆是垒土人✔
愿我的文章对你有所帮助!
欢迎===关注===点赞===评论,共同学习,共同进步!
一、字符串整理
给你一个由大小写英文字母组成的字符串 s
。
一个整理好的字符串中,两个相邻字符 s[i]
和 s[i+1]
,其中 0<= i <= s.length-2
,要满足如下条件:
- 若
s[i]
是小写字符,则s[i+1]
不可以是相同的大写字符。 - 若
s[i]
是大写字符,则s[i+1]
不可以是相同的小写字符。
请你将字符串整理好,每次你都可以从字符串中选出满足上述条件的 两个相邻 字符并删除,直到字符串整理好为止。
请返回整理好的 字符串 。题目保证在给出的约束条件下,测试样例对应的答案是唯一的。
注意:空字符串也属于整理好的字符串,尽管其中没有任何字符。
示例 1:
输入:s = "leEeetcode" 输出:"leetcode" 解释:无论你第一次选的是 i = 1 还是 i = 2,都会使 "leEeetcode" 缩减为 "leetcode" 。
示例 2:
输入:s = "abBAcC" 输出:"" 解释:存在多种不同情况,但所有的情况都会导致相同的结果。例如: "abBAcC" --> "aAcC" --> "cC" --> "" "abBAcC" --> "abBA" --> "aA" --> ""
示例 3:
输入:s = "s" 输出:"s"
解题思路:
1、首先创建一个StringBuffer类,实例化一个Str方便将后续符合条件的字符进行增删
2、首先遍历字符串使用CharAt()方法,如果Str里的最后一个字符与给出的字符进行比较,使用的ToLowerCase()方法比较,如果s中的是A,Str里已经存了a,即两个字母小写相同,那么把A剔除,使用deleteAt方法,这样可以保证Str里存储的都是正确的字符。
3、最后返回Str类并使用ToString转换成字符串。
ublic class LeetCode_1 {
@Test
public static void main(String[] args) {
makeGood("AaBbCcD");
}
public static String makeGood(String s)//字符串整理
{
StringBuffer str=new StringBuffer();
int strIndex=-1;
int length=s.length();
for(int i=0;i<length;i++)
{
char ch=s.charAt(i);
if(str.length()>0&&Character.toLowerCase(ch)==Character.toLowerCase(str.charAt(strIndex))&&ch!=str.charAt(strIndex))
{
str.deleteCharAt(strIndex);
strIndex--;
}
else {
str.append(ch);
strIndex++;
}
}
System.out.print(str.toString());
return str.toString();
}
}
二、排序数组
给你一个整数数组 nums
,请你将该数组升序排列。
解题思路:
1、直接使用排序算法,但是考虑到时间复杂度等,建议使用快速排序,并且需要经过优化的快速排序,本人以及测试过力扣上有的变态测试用例会导致超时。所以使用优化的快速排序,多使用一个方法:随机选取一个Key
public static int randomizedPartition(int[] nums, int l, int r) {
int i = new Random().nextInt(r - l + 1) + l; // 随机选一个作为我们的Key
Swap(nums, r, i);
return partition(nums, l, r);
}
如果对快速排序有疑问的可以参考本人的过于快速排序算法的解析:
http://t.csdn.cn/yQAmp
完整实现:
import java.util.Random;
public class QuickSort_2 {
public static void main(String args[])
{
int Arrys[]={32,18,23,45,79,88,20,84,97,6};
sortArray(Arrys);
for(int i:Arrys)
System.out.print(i);
}
public static int[] sortArray(int[] nums) {
randomizedQuicksort(nums, 0, nums.length - 1);
return nums;
}
public static void randomizedQuicksort(int[] nums, int l, int r) {
if (l < r) {
int pos = randomizedPartition(nums, l, r);
randomizedQuicksort(nums, l, pos - 1);
randomizedQuicksort(nums, pos + 1, r);
}
}
public static int randomizedPartition(int[] nums, int l, int r) {
int i = new Random().nextInt(r - l + 1) + l; // 随机选一个作为我们的主元
Swap(nums, r, i);
return partition(nums, l, r);
}
public static int partition(int[] Arrys, int left, int right) {
int Key=left;//设置最左边为Key
while (left<right){
while (left<right && Arrys[right]>=Arrys[Key] ) //先右向左开始找小于Key的值
{
right--;
}
while (left<right && Arrys[left]<=Arrys[Key] )//从左向右边开始找大于Key的值
{
left++;
}
//如果满足条件就交换left和right的值
Swap(Arrys,left,right);
}
//最后left和right相遇,交换left和Key值,并返回left,即找到数据中的中间值位置
Swap(Arrys,left,Key);
return left;
}
private static void Swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
发文不易,恳请大佬们高抬贵手!
点赞:随手点赞是种美德,是大佬们对于本人创作的认可!
评论:往来无白丁,是你我交流的的开始!
收藏:愿君多采撷,是大佬们对在下的赞赏!