Java查询时间段(startTime
330
2022-08-19
剑指Offer之Java算法习题精讲数组与字符串题
题目一
解法
class Solution {
public int thirdMax(int[] nums) {
Arrays.sort(nums);
if(nums.length<3){
return nums[nums.length-1];
}
int p = 1;
for(int i =nums.length-2;i>=0;i--){
if(nums[i]==nums[i+1]){
}else{
++p;
if(p==3){
return nums[i];
}
}
}
return nums[nums.length-1];
}
}
题目二
解法
class Solution {
public List
ArrayList
for(int i = 1;i<=n;i++){
if(i%3==0&&i%5==0){
list.add("FizzBuzz");
}else if(i%3==0){
list.add("Fizz");
}else if(i%5==0){
list.add("Buzz");
}else{
list.add(""+i);
}
}
return list;
}
}
题目三
解法
class Solution {
public char findTheDifference(String s, String t) {
int[] q = new int[500];
for(int i = 0;i q[s.charAt(i)] += 1; } for(int i = 0;i q[t.charAt(i)]-=1; if(q[t.charAt(i)]<0){ return t.charAt(i); } } return t.charAt(0); } } 题目四 解法 class Solution { public int firstUniqChar(String s) { int[] w = new int[60]; for(int i=0;i w[s.charAt(i)-'a']+=1; } char z = 'a'; for(int i =0;i if(w[i]==1){ z =(char)(i+'a'); break; } } for(int i=0;i if(w[s.charAt(i)-'a']==1){ return i; } } return -1; } } 题目五 解法 class Solution { public int findMaxConsecutiveOnes(int[] nums) { int maxCount = 0, count = 0; int n = nums.length; for (int i = 0; i < n; i++) { if (nums[i] == 1) { count++; } else { maxCount = Math.max(maxCount, count); count = 0; } } maxCount = Math.max(maxCount, count); return maxCount; } }
q[s.charAt(i)] += 1;
}
for(int i = 0;i q[t.charAt(i)]-=1; if(q[t.charAt(i)]<0){ return t.charAt(i); } } return t.charAt(0); } } 题目四 解法 class Solution { public int firstUniqChar(String s) { int[] w = new int[60]; for(int i=0;i w[s.charAt(i)-'a']+=1; } char z = 'a'; for(int i =0;i if(w[i]==1){ z =(char)(i+'a'); break; } } for(int i=0;i if(w[s.charAt(i)-'a']==1){ return i; } } return -1; } } 题目五 解法 class Solution { public int findMaxConsecutiveOnes(int[] nums) { int maxCount = 0, count = 0; int n = nums.length; for (int i = 0; i < n; i++) { if (nums[i] == 1) { count++; } else { maxCount = Math.max(maxCount, count); count = 0; } } maxCount = Math.max(maxCount, count); return maxCount; } }
q[t.charAt(i)]-=1;
if(q[t.charAt(i)]<0){
return t.charAt(i);
}
}
return t.charAt(0);
}
}
题目四
解法
class Solution {
public int firstUniqChar(String s) {
int[] w = new int[60];
for(int i=0;i w[s.charAt(i)-'a']+=1; } char z = 'a'; for(int i =0;i if(w[i]==1){ z =(char)(i+'a'); break; } } for(int i=0;i if(w[s.charAt(i)-'a']==1){ return i; } } return -1; } } 题目五 解法 class Solution { public int findMaxConsecutiveOnes(int[] nums) { int maxCount = 0, count = 0; int n = nums.length; for (int i = 0; i < n; i++) { if (nums[i] == 1) { count++; } else { maxCount = Math.max(maxCount, count); count = 0; } } maxCount = Math.max(maxCount, count); return maxCount; } }
w[s.charAt(i)-'a']+=1;
}
char z = 'a';
for(int i =0;i if(w[i]==1){ z =(char)(i+'a'); break; } } for(int i=0;i if(w[s.charAt(i)-'a']==1){ return i; } } return -1; } } 题目五 解法 class Solution { public int findMaxConsecutiveOnes(int[] nums) { int maxCount = 0, count = 0; int n = nums.length; for (int i = 0; i < n; i++) { if (nums[i] == 1) { count++; } else { maxCount = Math.max(maxCount, count); count = 0; } } maxCount = Math.max(maxCount, count); return maxCount; } }
if(w[i]==1){
z =(char)(i+'a');
break;
}
}
for(int i=0;i if(w[s.charAt(i)-'a']==1){ return i; } } return -1; } } 题目五 解法 class Solution { public int findMaxConsecutiveOnes(int[] nums) { int maxCount = 0, count = 0; int n = nums.length; for (int i = 0; i < n; i++) { if (nums[i] == 1) { count++; } else { maxCount = Math.max(maxCount, count); count = 0; } } maxCount = Math.max(maxCount, count); return maxCount; } }
if(w[s.charAt(i)-'a']==1){
return i;
}
}
return -1;
}
}
题目五
解法
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int maxCount = 0, count = 0;
int n = nums.length;
for (int i = 0; i < n; i++) {
if (nums[i] == 1) {
count++;
} else {
maxCount = Math.max(maxCount, count);
count = 0;
}
}
maxCount = Math.max(maxCount, count);
return maxCount;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~