image_2022-01-04_16-19-41.png
29.5 KB
#N1009. Complement of Base 10 Integer
problem link
#solution
problem link
#solution
class Solution {
public int bitwiseComplement(int n) {
if(n==0) return 1;
int power = 0;
for(int i=n; i>0; i/=2)
power++;
int temp=1;
while(power-- >0)
temp*=2;
return temp-n-1;
}
}image_2022-01-05_01-14-41.png
34.9 KB
#N2124. Check if All A's Appears Before All B's
problem link
#solution
problem link
#solution
class Solution {
public boolean checkString(String s) {
boolean isFound = false;
for(int i=0; i<s.length(); i++){
if(s.charAt(i) == 'b')
isFound = true;
if(isFound && s.charAt(i) == 'a')
return false;
}
return true;
}
}image_2022-01-05_01-37-27.png
56.5 KB
#N884. Uncommon Words from Two Sentences
problem link
#solution
problem link
#solution
class Solution {
public String[] uncommonFromSentences(String s1, String s2) {
Map<String, Integer> map = new HashMap<>();
for(String word: s1.split(" ")){
map.put(word, map.getOrDefault(word, 0) +1);
}
for(String word: s2.split(" ")){
map.put(word, map.getOrDefault(word, 0) +1);
}
List<String> list = new ArrayList<>();
for(Map.Entry<String, Integer> entry: map.entrySet()){
if(entry.getValue() == 1)
list.add(entry.getKey());
}
return list.toArray(new String[0]);
}
}image_2022-01-05_02-07-38.png
45.5 KB
#N1694. Reformat Phone Number
problem link
#solution
problem link
#solution
class Solution {
public String reformatNumber(String number) {
StringBuilder sb = new StringBuilder();
for (char c : number.toCharArray()) {
if (Character.isDigit(c)) sb.append(c);
}
int i=0;
for(; i<sb.length()-4;){
sb.insert(i+3, '-');
i+=4;
}
if(sb.length() - i == 4)
sb.insert(i+2, '-');
return sb.toString();
}
}image_2022-01-05_02-11-17.png
33.7 KB
#N1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence
problem link
#solution
problem link
#solution
class Solution {
public int isPrefixOfWord(String sentence, String searchWord) {
int ans=0;
for(String word: sentence.split(" ")){
if(word.startsWith(searchWord))
return ans+1;
ans++;
}
return -1;
}
}image_2022-01-05_02-13-05.png
59.7 KB
#N1078. Occurrences After Bigram
problem link
#solution
problem link
#solution
class Solution {
public String[] findOcurrences(String text, String first, String second) {
String[] arr=text.split(" ");
String[] ans = new String[100];
int count=0;
for(int i=0; i<arr.length-2; i++){
if(arr[i].equals(first)&&arr[i+1].equals(second)){
ans[i]=arr[i+2];
count ++;
}
}
String[] main=new String[count];
for(int i=0; i<count; i++){
for(int j=0; j<ans.length; j++){
if(ans[j]!=null){
main[i]=ans[j];
ans[j]=null;
break;
}
}
}
return main;
}
}image_2022-01-05_18-17-32.png
26.5 KB
#N181. Employees Earning More Than Their Managers
problem link
#solution
problem link
#solution
select t1.name as Employee from Employee t1, Employee t2
where t1.managerId = t2.id and t1.salary>t2.salary;
image_2022-01-06_02-11-10.png
27.5 KB
#N2114. Maximum Number of Words Found in Sentences
problem link
#solution
problem link
#solution
class Solution {
public int mostWordsFound(String[] sentences) {
int max = 0;
for(String str: sentences){
max = Math.max(max, str.split(" ").length);
}
return max;
}
}image_2022-01-06_14-12-20.png
26 KB
#N595. Big Countries
problem link
#solution
problem link
#solution
select name, population, area from World
where area >= 3000000 || population >= 25000000;
Leetcode in Java && Oracle
image_2022-01-06_14-12-20.png
memory usage of all database problems in leetcode is 0. so weird🤔
image_2022-01-06_17-37-07.png
60 KB
#N1179. Reformat Department Table
problem link
#solution
problem link
#solution
select id,
sum(if(month='Jan', revenue, null)) as Jan_Revenue,
sum(if(month='Feb', revenue, null)) as Feb_Revenue,
sum(if(month='Mar', revenue, null)) as Mar_Revenue,
sum(if(month='Apr', revenue, null)) as Apr_Revenue,
sum(if(month='May', revenue, null)) as May_Revenue,
sum(if(month='Jun', revenue, null)) as Jun_Revenue,
sum(if(month='Jul', revenue, null)) as Jul_Revenue,
sum(if(month='Aug', revenue, null)) as Aug_Revenue,
sum(if(month='Sep', revenue, null)) as Sep_Revenue,
sum(if(month='Oct', revenue, null)) as Oct_Revenue,
sum(if(month='Nov', revenue, null)) as Nov_Revenue,
sum(if(month='Dec', revenue, null)) as Dec_Revenue
from department
group by id
order by id
image_2022-01-08_23-24-30.png
42.9 KB
#N1408. String Matching in an Array
problem link
#solution
problem link
#solution
class Solution {
public List<String> stringMatching(String[] words) {
HashSet<String> set = new HashSet<>();
for(int i=0; i<words.length-1; i++){
for(int j=i+1; j<words.length; j++){
if(words[i].contains(words[j]))
set.add(words[j]);
if(words[j].contains(words[i]))
set.add(words[i]);
}
}
return new ArrayList<String>(set);
}
}image_2022-01-08_23-37-18.png
37.5 KB
#N1189. Maximum Number of Balloons
problem link
#solution
problem link
#solution
class Solution {
public int maxNumberOfBalloons(String text) {
int letters[] = new int[26];
for(char ch: text.toCharArray())
letters[ch-'a']++;
int min = letters[1];
min = Math.min(min, letters[0]);
min = Math.min(min, letters[11]/2);
min = Math.min(min, letters[14]/2);
min = Math.min(min, letters[13]);
return min;
}
}#statistics
Till this time, 223 in total,
210 easy;
13 medium;
type of problem solutions have been posted
Till this time, 223 in total,
210 easy;
13 medium;
type of problem solutions have been posted
#N1945. Sum of Digits of String After Convert
problem link
#solution
problem link
#solution
class Solution {
public int getLucky(String s, int k) {
StringBuilder sb=new StringBuilder();
for(int i=0;i<s.length();i++)
sb.append((s.charAt(i)-'a')+1);
String result=sb.toString();
if(result.length()==1)
return Character.getNumericValue(result.charAt(0));
int sum=0;
while(k-->0 && result.length()>1)
{
sum=0;
for(int i=0;i<result.length();i++)
sum+=Character.getNumericValue(result.charAt(i));
result=String.valueOf(sum);
}
return sum;
}
}#N1507. Reformat Date
problem link
#solution
problem link
#solution
class Solution {
public String reformatDate(String date) {
StringBuilder sb = new StringBuilder();
String month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String d[] = date.split(" ");
sb.append(d[2]+"-");
for(int i=0; i<12; i++)
if(month[i].equals(d[1])){
if(i<9) sb.append(0).append(i+1);
else sb.append(i+1);
}
sb.append("-");
if(d[1].substring(0, d[0].length()-2).length()==1)
sb.append(0).append(d[0].charAt(0));
else
sb.append(d[0].substring(0, 2));
return sb.toString();
}
}image_2022-01-10_00-30-16.png
41.6 KB
#N242. Valid Anagram
problem link
#solution
problem link
#solution
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return 0==1;
Map<Character, Integer> mapS = new HashMap<>();
Map<Character, Integer> mapT = new HashMap<>();
for(int i=0; i<s.length(); i++){
mapS.put(s.charAt(i), mapS.getOrDefault(s.charAt(i), 0) +1);
mapT.put(t.charAt(i), mapT.getOrDefault(t.charAt(i), 0) +1);
}
return mapS.equals(mapT);
}
}image_2022-01-11_15-22-08.png
20.7 KB
#N620. Not Boring Movies
problem link
#solution
problem link
#solution
select * from cinema
where description <> 'boring' and id%2=1
order by rating desc;
image_2022-01-11_15-26-47.png
20 KB
#N175. Combine Two Tables
problem link
#solution
problem link
#solution
select t1.firstname, t1.lastname, t2.city, t2.state
from person t1 left join address t2
on t1.personid=t2.personid;