image_2022-05-23_17-57-27.png
87.1 KB
#medium
#N1418. Display Table of Food Orders in a Restaurant
problem link
#solution
#N1418. Display Table of Food Orders in a Restaurant
problem link
#solution
class Solution {
public static List<List<String>> displayTable(List<List<String>> orders) {
int table; String food;
Map<Integer, TreeMap<String, Integer>> map = new TreeMap<>();
Set<String> set = new TreeSet<>();
for (List<String> order : orders) {
table = Integer.parseInt(order.get(1));food = order.get(2);
set.add(food);
TreeMap<String, Integer> foodMap;
if (map.get(table) == null) foodMap = new TreeMap<>();
else foodMap = map.get(table);
foodMap.put(food, foodMap.getOrDefault(food, 0) +1);
map.put(table, foodMap);
}
Leetcode in Java && Oracle
image_2022-05-23_17-57-27.png
List<List<String>> list = new ArrayList<>();
List<String> temp = new ArrayList<>(); temp.add("Table");
for (String s: set)
temp.add(s);
list.add(temp);
for (Map.Entry<Integer, TreeMap<String, Integer>> entry: map.entrySet()){
temp = new ArrayList<>();
temp.add(String.valueOf(entry.getKey()));
for (String s : set)
temp.add(String.valueOf(entry.getValue().getOrDefault(s, 0)));
list.add(temp);
}
return list;
}
}
image_2022-05-24_13-58-14.png
73.4 KB
#medium
#N811. Subdomain Visit Count
problem link
#solution
#N811. Subdomain Visit Count
problem link
#solution
public List<String> subdomainVisits(String[] cpdomains) {
Map<String, Integer> map = new HashMap<>(); String website, websites[]; int number; StringBuilder sb;
for(String s: cpdomains){
sb=new StringBuilder(); number=Integer.parseInt(s.split(" ")[0]);
websites=s.split(" ")[1].split("\\.");
for(int i=websites.length-1; i>0; i--){
sb.insert(0, websites[i]);
map.put(sb.toString(), map.getOrDefault(sb.toString(), 0) +number);
sb.insert(0, ".");
} sb.insert(0, websites[0]);
map.put(sb.toString(), map.getOrDefault(sb.toString(), 0) +number);
} List<String> list = new ArrayList<>();
for(Map.Entry<String, Integer> entry: map.entrySet()){
sb=new StringBuilder();
sb.append(entry.getValue()).append(" ").append(entry.getKey());
list.add(sb.toString());
} return list;}image_2022-05-24_13-59-41.png
25.6 KB
#N2278. Percentage of Letter in String
problem link
#solution
problem link
#solution
class Solution {
public int percentageLetter(String s, char letter) {
int count=0;
for(char ch: s.toCharArray())
if(ch==letter) count++;
return count*100/s.length();
}
}image_2022-05-24_17-17-22.png
35.3 KB
#medium
#N48. Rotate Image
problem link
#solution
#N48. Rotate Image
problem link
#solution
class Solution {
public void rotate(int[][] matrix) {
int n=matrix.length, temp;
for(int i=0; i<n/2; i++){
for(int j=0; j<(n+1)/2; j++){
temp=matrix[i][j];
matrix[i][j]=matrix[n-1-j][i];
matrix[n-1-j][i]=matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j]=matrix[j][n-1-i];
matrix[j][n-1-i]=temp;
}
}
}
}🔥3
image_2022-05-25_18-00-54.png
18.6 KB
#N1025. Divisor Game
problem link
#solution
problem link
#solution
class Solution {
public boolean divisorGame(int n) {
return n%2==0;
}
}image_2022-06-07_13-45-55.png
38 KB
#N88. Merge Sorted Array
problem link
#solution
problem link
#solution
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
for(int i=nums1.length-1; i>=0; i--){
if(m>0 && n>0){
if(nums1[m-1]>nums2[n-1]){
nums1[i]=nums1[m-1];
m--;
}else{
nums1[i]=nums2[n-1];
n--;
}
}
}
for(int i=n-1; i>=0; i--){
nums1[i]=nums2[i];
}
}
}image_2022-06-10_14-39-52.png
43.5 KB
#medium
#N3. Longest Substring Without Repeating Characters
problem link
#solution
#N3. Longest Substring Without Repeating Characters
problem link
#solution
class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.length()==0) return 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int max=0;
for (int i=0, j=0; i<s.length(); ++i){
if (map.containsKey(s.charAt(i))){
j = Math.max(j,map.get(s.charAt(i))+1);
}
map.put(s.charAt(i),i);
max = Math.max(max,i-j+1);
}
return max;
}
}image_2022-06-24_15-16-19.png
34 KB
#N392. Is Subsequence
problem link
#solution
problem link
#solution
class Solution {
public boolean isSubsequence(String s, String t) {
if(s.length()==0) return true;
int is=0, it=0;
while(it<t.length()){
if(s.charAt(is)==t.charAt(it)){
is++;
if(is==s.length()) return true;
}
it++;
}
return false;
}
}👍2
image_2022-06-27_14-22-38.png
25.6 KB
#medium
#N1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
problem link
#N1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
problem link
class Solution {
public int minPartitions(String n) {
for(int i=9; i>=0; i--){
if(n.contains(""+i))
return i;
}
return -1;
}
}image_2022-06-27_16-25-33.png
53.6 KB
#HARD
#N4. Median of Two Sorted Arrays
problem link
#N4. Median of Two Sorted Arrays
problem link
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int l1=nums1.length, l2=nums2.length, l=l1+l2;
int num[] = new int[l], i=0, j=0;
for(; i<l1 || j<l2; ){
if(i<l1 && j<l2){
if(nums1[i]>nums2[j])
num[i+j]=nums2[j++];
else
num[i+j]=nums1[i++];
}else if(i<l1)
num[i+j]=nums1[i++];
else
num[i+j]=nums2[j++];
}
return l%2==0 ? (num[l/2-1]+num[l/2])/2.0 : (double)num[l/2];
}
}🔥3
image_2022-06-27_18-28-25.png
42.3 KB
#medium
#N6. Zigzag Conversion
problem link
#N6. Zigzag Conversion
problem link
class Solution {
public String convert(String s, int n) {
if(n==1) return s;
StringBuilder sb = new StringBuilder();
for(int i=0; i<n; i++){
for(int j=i; j<s.length(); ){
sb.append(s.charAt(j));
if(i!=0 && i!=n-1 && j<s.length()-(n-i-1)*2){
sb.append(s.charAt(j+(n-i-1)*2));
}
j=j+2*n-2;
}
}
return sb.toString();
}
}image_2022-06-30_14-03-53.png
41.3 KB
#medium
#N7. Reverse Integer
problem link
#solution
#N7. Reverse Integer
problem link
#solution
class Solution {
public int reverse(int x) {
int res=0;
boolean isPos=true;
if(x<0){
x=-x;
isPos=false;
}
while(x>0){
int last=x%10;
x/=10;
if(res>Integer.MAX_VALUE/10||res==Integer.MAX_VALUE/10 && last>=8) return 0;
res=res*10+last;
System.out.println(res);
}
return isPos ? res : -res;
}
}image_2022-06-30_14-05-21.png
36.2 KB
#N14. Longest Common Prefix
problem link
#solution
problem link
#solution
class Solution {
public String longestCommonPrefix(String[] strs) {
String s=strs[0];
int l=s.length(), count=0;
while(l>0){
count=0;
for(String str:strs){
if(str.startsWith(s.substring(0, l))) count++;
}
if(count==strs.length) return s.substring(0, l);
l--;
}
return "";
}
}👍2
image_2022-07-01_01-20-41.png
44 KB
#medium
#N15. 3Sum
problem link
#solution
#N15. 3Sum
problem link
#solution
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Set<List<Integer>> set = new HashSet<>();
Arrays.sort(nums);
for(int i=0; i<nums.length-2; i++){
int j=i+1, k=nums.length-1;
while(j<k){
int sum=nums[i]+nums[j]+nums[k];
if(sum==0) set.add(Arrays.asList(nums[i], nums[j++], nums[k--]));
else if(sum>0) k--;
else j++;
}
}
return new ArrayList<>(set);
}
}👍1
image_2022-07-01_01-33-56.png
59.1 KB
#medium
#N16. 3Sum Closest
problem link
#solution
#N16. 3Sum Closest
problem link
#solution
class Solution {
public int threeSumClosest(int[] nums, int target) {
int min=Integer.MAX_VALUE, res=-1000;
Arrays.sort(nums);
for(int i=0; i<nums.length-2; i++){
int j=i+1, k=nums.length-1;
while(j<k){
int sum=nums[i]+nums[j]+nums[k];
if(sum>=target) k--;
else j++;
if(Math.abs(sum-target)<min){
min=Math.abs(sum-target);
res=sum;
}
}
}
return res;
}
}👍2
image_2022-07-04_00-03-42.png
40.6 KB
#medium
#N19. Remove Nth Node From End of List
problem link
#solution
#N19. Remove Nth Node From End of List
problem link
#solution
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode start = new ListNode();
ListNode slow=start, fast=start;
start.next=head;
while(n-- +1 >0){
fast=fast.next;
}
while(fast!=null){
fast=fast.next;
slow=slow.next;
}
slow.next=slow.next.next;
return start.next;
}
}👍2
image_2022-07-06_13-02-51.png
77.4 KB
#hard
#N23. Merge k Sorted Lists
problem link
#solution
#N23. Merge k Sorted Lists
problem link
#solution
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if(lists.length==0) return null;
ListNode res=lists[0];
for(int i=1; i<lists.length; i++)
res=merge(res, lists[i]);
return res;
}
public ListNode merge(ListNode res, ListNode list){
ListNode l1=new ListNode(0);
ListNode head=l1;
while(res!=null && list!=null){
if(res.val<list.val){
l1.next=res;
res=res.next;
}else{
l1.next=list;
list=list.next;
}
l1=l1.next;
}
while(res!=null){
l1.next=res;
res=res.next;
l1=l1.next;
}
while(list!=null){
l1.next=list;
list=list.next;
l1=l1.next;
}
return head.next;
}
}image_2022-08-04_01-02-22.png
42 KB
#medium
#N729. My Calendar I
problem link
#solution
#N729. My Calendar I
problem link
#solution
class MyCalendar {
Set<Pair<Integer, Integer>> set = new HashSet<>();
public MyCalendar() {}
public boolean book(int start, int end) {
Pair<Integer, Integer> pair = new Pair<>(start, end);
for(Pair<Integer, Integer> s: set){
if(s.getKey() < end && start < s.getValue()){
return false;
}
}
set.add(pair);
return true;
}
}👍1