题目一
解法
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<String> fizzBuzz(int n) {
ArrayList<String> list =new ArrayList<String>();
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<s.length();i++){
q[s.charAt(i)] += 1;
}
for(int i = 0;i<t.length();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<s.length();i++){
w[s.charAt(i)-'a']+=1;
}
char z = 'a';
for(int i =0;i<w.length;i++){
if(w[i]==1){
z =(char)(i+'a');
break;
}
}
for(int i=0;i<s.length();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;
}
}