Leetcode in Java && Oracle
422 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2022-05-19_16-49-35.png
49.3 KB
#medium
#N31. Next Permutation
problem link
#solution
class Solution {
public void nextPermutation(int[] nums) {
int i=nums.length-2, j=nums.length-1, temp;
while(i>-1 && nums[i]>=nums[i+1])
i--;
if(i>=0){
while(nums[i]>=nums[j])
j--;

temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
int k=nums.length-1;
i++;
while(i<k){
temp=nums[i];
nums[i]=nums[k];
nums[k]=temp;
i++; k--;
}
}
}
image_2022-05-23_17-57-27.png
87.1 KB
#medium
#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);
}
image_2022-05-24_13-58-14.png
73.4 KB
#medium
#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_17-17-22.png
35.3 KB
#medium
#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_14-13-45.png
220.7 KB
image_2022-06-10_14-39-52.png
43.5 KB
#medium
#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-27_14-22-38.png
25.6 KB
#medium
#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_18-28-25.png
42.3 KB
#medium
#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
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-07-01_01-20-41.png
44 KB
#medium
#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
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
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-08-04_01-02-22.png
42 KB
#medium
#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
image_2022-08-19_01-28-07.png
49.7 KB
#medium
#N1338. Reduce Array Size to The Half
problem link
#solution
class Solution {
public int minSetSize(int[] arr) {
int count=0, sum=0;
Map<Integer, Integer> map = new HashMap<>();
for(int n: arr){
map.put(n, map.getOrDefault(n, 0) +1);
}
List<Map.Entry<Integer, Integer>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (i1, i2) -> i2.getValue().compareTo(i1.getValue()));
for (Map.Entry<Integer, Integer> aa : list) {
sum+=aa.getValue();
count++;
if(sum>=arr.length/2){
return count;
}
}
return -1;
}
}
image_2022-08-20_22-23-53.png
50.1 KB
#medium
#N38. Count and Say
problem link
#solution
class Solution {
public String countAndSay(int n) {
StringBuilder sb = new StringBuilder("1");
StringBuilder temp = new StringBuilder();
int sikl=2, count;
while(sikl++<=n){
count=1;
for(int i=1; i<sb.length(); i++){
if(sb.charAt(i-1)==sb.charAt(i)) count++;
else{
temp.append(count).append(sb.charAt(i-1));
count = 1;
}
}
temp.append(count).append(sb.charAt(sb.length()-1));
sb=temp;
temp = new StringBuilder();
}
return sb.toString();
}
}
image_2022-08-20_23-54-22.png
60.8 KB
#medium
#N36. Valid Sudoku
problem link
#solution
class Solution {
public boolean isValidSudoku(char[][] board) {
boolean case1=true, case2=true, case3=true;
Set<Character> set1 = new HashSet<>();
Set<Character> set2 = new HashSet<>();
Set<Character> set3 = new HashSet<>();
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
if(board[i][j] != '.' && !set1.add(board[i][j])) return false;
if(board[j][i] != '.' && !set2.add(board[j][i])) return false;
if(board[3*(i/3)+j/3][3*(i%3)+j%3] != '.' &&
!set3.add(board[3*(i/3)+j/3][3*(i%3)+j%3])) return false;
}
set1 = new HashSet<>();
set2 = new HashSet<>();
set3 = new HashSet<>();
}
return true;
}
}
🔥2