image_2021-12-09_02-41-10.png
41.7 KB
#N888. Fair Candy Swap
problem link
#solution
problem link
#solution
class Solution {
public int[] fairCandySwap(int[] a, int[] b) {
int sumA = 0, sumB = 0;
for(int n: a) sumA+=n;
for(int n: b) sumB+=n;
int delta = (sumB - sumA) / 2;
Set<Integer> set = new HashSet<>();
for(int n: b) set.add(n);
for(int n: a)
if(set.contains(n+delta)) return new int[]{n, n+delta};
return new int[0];
}
}image_2021-12-09_02-58-54.png
28.4 KB
#N283. Move Zeroes
problem link
#solution
problem link
#solution
class Solution {
public void moveZeroes(int[] nums) {
int index = 0;
for (int n: nums)
if(n != 0)
nums[index++] = n;
while(index<nums.length)
nums[index++] = 0;
}
}image_2021-12-09_03-06-43.png
28.7 KB
#N1848. Minimum Distance to the Target Element
problem link
#solution
problem link
#solution
class Solution {
public int getMinDistance(int[] nums, int target, int start) {
int ans = Integer.MAX_VALUE;
for(int i=0; i<nums.length; i++){
if(nums[i] == target)
ans = Math.min(ans, Math.abs(i - start));
}
return ans;
}
}image_2021-12-09_03-19-58.png
27.5 KB
#N1287. Element Appearing More Than 25% In Sorted Array
problem link
#solution
problem link
#solution
class Solution {
public int findSpecialInteger(int[] arr) {
int length = arr.length, quart = length/4;
for(int i=0; i<length - quart; i++)
if(arr[i] == arr[i+quart]) return arr[i];
return -1;
}
}image_2021-12-10_00-32-27.png
75.3 KB
#N748. Shortest Completing Word
problem link
#solution
problem link
#solution
Map<Character, Integer> map=new HashMap<>();
int minLength=Integer.MAX_VALUE;
String result = null;
for(char ch: licensePlate.toLowerCase().toCharArray())
if(Character.isLetter(ch))map.put(ch, map.getOrDefault(ch, 0) +1);
for(String word:words){
System.out.println(word);
Map<Character, Integer> temp = new HashMap<>(map);
int count = temp.size();
for(char ch:word.toCharArray())
if(temp.containsKey(ch))temp.put(ch, temp.get(ch) -1);
for(Map.Entry<Character, Integer> entry: temp.entrySet()){
if(entry.getValue()>0) break;
count--;
}
if(count<=0&&minLength>word.length()){
result=word;
minLength=word.length();
}
}
return result;
image_2021-12-10_00-33-02.png
47.5 KB
#N1629. Slowest Key
problem link
#solution
problem link
#solution
class Solution {
public char slowestKey(int[] releaseTimes, String str) {
int max=releaseTimes[0], ix=0;
for(int i=1; i<releaseTimes.length; i++){
if(max<=releaseTimes[i]-releaseTimes[i-1]){
if(max==releaseTimes[i]-releaseTimes[i-1]&&str.charAt(i)>str.charAt(ix)){
ix=i;
//break;
}else if(max<releaseTimes[i]-releaseTimes[i-1]){
max=releaseTimes[i]-releaseTimes[i-1];
ix=i;}
}
}
return str.charAt(ix);
}
}image_2021-12-10_00-33-29.png
38.1 KB
#N217. Contains Duplicate
problem link
#solution
problem link
#solution
class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums == null){
return false;
}
int len = nums.length;
HashSet<Integer> set = new HashSet<Integer>();
for(int i = 0; i < len; i++){
if(!set.add(nums[i])){
return true;
}
}
return false;
}
}image_2021-12-10_00-33-55.png
37.7 KB
#N896. Monotonic Array
problem link
#solution
problem link
#solution
class Solution {
public boolean isMonotonic(int[] nums) {
int countI=0, countD=0, l=nums.length;
for(int i=0; i<l-1; i++){
if(nums[i] == nums[i+1]){
countI++;
countD++;
}
else if(nums[i] < nums[i+1]) countI++;
else countD++;
}
return countI == l-1 || countD == l-1;
}
}image_2021-12-10_00-49-07.png
39.1 KB
#N1331. Rank Transform of an Array
problem link
#solution
problem link
#solution
class Solution {
public int[] arrayRankTransform(int[] arr) {
int[] helper = Arrays.copyOf(arr, arr.length);
Arrays.sort(helper);
Map<Integer, Integer> rank = new HashMap<>();
int pos=1;
for(int n: helper)
if(rank.getOrDefault(n, 0) == 0) rank.put(n, rank.size()+1);
for(int i=0; i<arr.length; i++)
arr[i] = rank.get(arr[i]);
return arr;
}
}image_2021-12-10_01-25-15.png
43.5 KB
#N1560. Most Visited Sector in a Circular Track
problem link
#solution
problem link
#solution
class Solution {
public List<Integer> mostVisited(int n, int[] rounds) {
int l=rounds.length;
List<Integer> list = new ArrayList<>();
for(int i=rounds[0]; i<=rounds[l-1]; i++)
list.add(i);
if(list.size()>0) return list;
for(int i=1; i<=rounds[l-1]; i++)
list.add(i);
for(int i=rounds[0]; i<=n; i++)
list.add(i);
return list;
}
}image_2021-12-10_01-26-42.png
30.4 KB
#N167. Two Sum II - Input Array Is Sorted
problem link
#solution
problem link
#solution
class Solution {
public int[] twoSum(int[] numbers, int target) {
int start = 0, end = numbers.length - 1;
while(start < end){
if(numbers[start] + numbers[end] == target) break;
if(numbers[start] + numbers[end] < target) start++;
else end--;
}
return new int[]{start + 1, end + 1};
}
}image_2021-12-10_01-56-28.png
57.3 KB
#N1370. Increasing Decreasing String
problem link
#solution
problem link
#solution
class Solution {
public String sortString(String s) {
int count[] = new int[26];
StringBuilder sb=new StringBuilder();
for(char ch: s.toCharArray())
count[ch-'a']++;
while(sb.toString().length()<s.length()){
for(int i=0; i<26; i++){
if(count[i]>0){
sb.append((char) (i+'a'));
count[i]--;
}
}
for(int i=25; i>=0; i--){
if(count[i]>0){
sb.append((char) (i+'a'));
count[i]--;
}
}
}
return sb.toString();
}
}image_2021-12-10_02-03-39.png
42.1 KB
#N1436. Destination City
problem link
#solution
problem link
#solution
class Solution {
public String destCity(List<List<String>> paths) {
Map<String, Integer> map = new HashMap<>();
for(List<String> list: paths){
map.put(list.get(0), map.getOrDefault(list.get(0), 0) -1);
map.put(list.get(1), map.getOrDefault(list.get(1), 0) +1);
}
for(Map.Entry<String, Integer> entry: map.entrySet())
if(entry.getValue()>0) return entry.getKey();
return new String();
}
}image_2021-12-10_02-10-28.png
29.6 KB
#N1374. Generate a String With Characters That Have Odd Counts
problem link
#solution
problem link
#solution
class Solution {
public String generateTheString(int n) {
StringBuilder sb = new StringBuilder();
for(int i=0; i<n-1; i++)
sb.append('a');
return (n%2==0)?sb.append('b').toString():sb.append('a').toString();
}
}#statistics
Till this time, 192 in total,
180 easy;
12 medium;
type of problem solutions have been posted
Till this time, 192 in total,
180 easy;
12 medium;
type of problem solutions have been posted
image_2021-12-12_23-31-05.png
37.4 KB
#N1941. Check if All Characters Have Equal Number of Occurrences
problem link
#solution
problem link
#solution
class Solution {
public boolean areOccurrencesEqual(String s) {
int freq[] = new int[26], count=0;
char temp=s.charAt(0);
for(char ch: s.toCharArray()){
if(ch == temp) count++;
freq[ch-'a']++;
}
for(int i: freq)
if(i != 0 && i != count) return false;
return true;
}
}image_2021-12-12_23-43-22.png
33.4 KB
#N557. Reverse Words in a String III
problem link
#solution
problem link
#solution
class Solution {
public String reverseWords(String s) {
StringBuilder sb = new StringBuilder();
String[] words = s.split(" ");
for(String word: words){
sb.append(" ");
sb.append(new StringBuilder(word).reverse());
}
return sb.toString().trim();
}
}image_2021-12-12_23-53-03.png
34.8 KB
#N657. Robot Return to Origin
problem link
#solution
problem link
#solution
class Solution {
public boolean judgeCircle(String moves) {
int[] count = new int[4];
for(char ch: moves.toCharArray()){
if(ch == 'R') count[0]++;
else if(ch == 'L') count[1]++;
else if(ch == 'U') count[2]++;
else count[3]++;
}
return count[0] - count[1] == 0 && count[2] - count[3] == 0;
}
}image_2021-12-12_23-58-55.png
38.3 KB
#N1768. Merge Strings Alternately
problem link
#solution
problem link
#solution
class Solution {
public String mergeAlternately(String word1, String word2) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i<Math.max(word1.length(), word2.length()); i++){
if(word1.length()>i)
sb.append(word1.charAt(i));
if(word2.length()>i)
sb.append(word2.charAt(i));
}
return sb.toString();
}
}image_2021-12-13_00-32-47.png
23 KB
#N344. Reverse String
problem link
#solution
problem link
#solution
class Solution {
public void reverseString(char[] s) {
for(int i=0; i<s.length/2; i++){
char tmp = s[i];
s[i] = s[s.length-1-i];
s[s.length-1-i] = tmp;
}
}
}