阿华代码,不是逆风,就是我疯
你们的点赞收藏是我前进最大的动力!!
希望本文内容能够帮助到你!!
目录
一:替换所有的问号
二:提莫攻击
三:z字形变换
四:外观数列
五:数青蛙
方法一
方法二
一:替换所有的问号
1576. 替换所有的问号 - 力扣(LeetCode)
方法一
class Solution {
public String modifyString(String ss) {
char[] s = ss.toCharArray();
int len = s.length;
for(int i = 0 ; i < s.length ; i++){
if(s[i] == '?'){
for(char ch = 'a' ; ch <= 'z' ; ch++){
if( (i == 0 || ch != s[i-1]) && (i == len-1 || ch != s[i+1]) ){
s[i] = ch;
break;
}
}
}
}
String ret = String.valueOf(s);
return ret;
}
}
方法二(复杂)
class Solution {
public String modifyString(String s) {
char[] tem = new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char[] str = s.toCharArray();
for(int i = 0 ; i < str.length ; i++){
while(str[i] == '?'){
for(int j = 0 ; j < tem.length ; j++){
if(i == 0){
if((str.length > 1) && (str[i+1] != tem[j])){
str[i] = tem[j];
break;
}else if(str.length == 1){
str[i] = tem[j];
break;
}else{
}
}else if(i == str.length-1){
if(str[i-1] != tem[j]){
str[i] = tem[j];
}else{
}
}else{
if((str[i+1] != tem[j]) && (str[i-1] != tem[j])){
str[i] = tem[j];
break;
}else{
}
}
}
}
}
String ret = "";
for(int i = 0 ; i < str.length ; i++){
ret += str[i];
}
return ret;
}
}
二:提莫攻击
495. 提莫攻击
class Solution {
public int findPoisonedDuration(int[] nums, int time) {
int sum = 0;
for(int i = 0 ; i < nums.length-1 ; i++){
if(nums[i+1] - nums[i] >= time){
sum += time;
}else{
sum += nums[i+1]-nums[i];
}
}
sum += time;
return sum;
}
}
三:z字形变换
6. Z 字形变换
class Solution {
public String convert(String s, int numRows) {
//处理一下特殊情况
if(numRows == 1){
return s;
}
//1:创建一个变量来接收拼接的字符串
int d = 2 * numRows - 2;
StringBuilder ret = new StringBuilder();
int len = s.length();
//2:确定第一行的下标,并按照下标找到该字符,拼接上去
for(int i = 0 ; i < len ; i += d){
ret.append(s.charAt(i));
}
//3:确定中间的行数,两层循环,一行一行处理,需要两个指针用于确定坐标,然后拼接
for(int i = 1 ; i < numRows-1 ; i++){
for(int k = i , j = d - k ; k < len || j < len ; k += d , j += d){
if(k < len){
ret.append(s.charAt(k));
}
if(j < len){
ret.append(s.charAt(j));
}
}
}
//4:确定好最后一行,同第一行处理方式一样
for(int i = numRows-1 ; i < len ; i += d){
ret.append(s.charAt(i));
}
return ret.toString();
}
}
四:外观数列
38. 外观数列
class Solution {
public String countAndSay(int n) {
String ret = "1";
//1:下面的循环用于依次解释ret,只用解释n-1次
for(int i = 1 ; i <= n-1 ; i++){
//每一次都是一个新的StringBuilder
StringBuilder temp = new StringBuilder();
//模拟:第一次解释,对应的是n=2
int len = ret.length();
for(int left = 0 , right = 0 ; right < len ; ){
//2:模拟有几个相同的数
while(right < len && ret.charAt(left) == ret.charAt(right)){
right++;
}
temp.append(Integer.toString(right-left));
temp.append(ret.charAt(left));
left = right;
}
//更新ret,StringBuilder转换为String类型
ret = temp.toString();
}
return ret;
}
}
五:数青蛙
1419. 数青蛙
方法一
class Solution {
public int minNumberOfFrogs(String croakOfFrogs) {
char[] str = croakOfFrogs.toCharArray();
String t = "croak";
int n = t.length();
int[] hash = new int[n];
Map<Character,Integer> hashMap = new HashMap<>();//<字符,字符对应下标>
for(int i = 0 ; i < n ; i++){
hashMap.put(t.charAt(i),i);
}
for(char ch : str){
if(ch == t.charAt(0)){
if(hash[n-1] != 0){
hash[n-1]--;
hash[0]++;
}else{
hash[0]++;
}
}else{
int i = hashMap.get(ch);
if(hash[i-1] == 0){
return -1;
}else{
hash[i-1]--;
hash[i]++;
}
}
}
for(int i = 0 ; i < n-1 ; i++){
if(hash[i] != 0){
return -1;
}
}
return hash[n-1];
}
}
方法二
class Solution5 {
public int minNumberOfFrogs(String croakOfFrogs) {
//创建hash表,创建字符数组,遍历数组
Map<Character,Integer> hash = new HashMap<>();
char[] str = croakOfFrogs.toCharArray();
hash.put('c',0);hash.put('r',0);hash.put('o',0);hash.put('a',0);hash.put('k',0);
for(int i = 0 ; i < str.length ; i++){
if(str[i] == 'c'){
if(hash.get('k') == 0){
hash.put('c',hash.get('c')+1);
}else{
hash.put('c',hash.get('c')+1);
hash.put('k',hash.get('k')-1);
}
}else{
if(str[i] == 'r'){
if(hash.get('c') != 0){
hash.put('c',hash.get('c')-1);
hash.put('r',hash.get('r')+1);
}else{
return -1;
}
}else if(str[i] == 'o'){
if(hash.get('r') != 0){
hash.put('r',hash.get('r')-1);
hash.put('o',hash.get('o')+1);
}else{
return -1;
}
}else if(str[i] == 'a'){
if(hash.get('o') != 0){
hash.put('o',hash.get('o')-1);
hash.put('a',hash.get('a')+1);
}else{
return -1;
}
}else if(str[i] == 'k'){
if(hash.get('a') != 0){
hash.put('a',hash.get('a')-1);
hash.put('k',hash.get('k')+1);
}else{
return -1;
}
}else{}
}
}
if(hash.get('c') != 0 || hash.get('r') != 0 ||
hash.get('o') != 0 || hash.get('a') != 0 ){
return -1;
}
int ret = hash.get('k');
return ret;
//判断
}
}