image_2022-09-15_18-25-04.png
54.1 KB
#medium
#N59. Spiral Matrix II
problem link
#solution
#N59. Spiral Matrix II
problem link
#solution
class Solution {
public int[][] generateMatrix(int n) {
int num=1;
int[][] mat = new int[n][n];
int up=0, down=n-1, left=0, right=n-1;
while(up<=down && left<=right){
for(int i=left; i<=right; i++)
mat[up][i]=num++;
up++;
if(up>down || left>right) break;
for(int i=up; i<=down; i++)
mat[i][right]=num++;
right--;
for(int i=right; i>=left; i--)
mat[down][i]=num++;
down--;
if(up>down || left>right) break;
for(int i=down; i>=up; i--)
mat[i][left]=num++;
left++;
}
return mat;
}
}⚡1🔥1
image_2022-09-22_02-09-40.png
54 KB
#medium
#N985. Sum of Even Numbers After Queries
problem link
#solution
#N985. Sum of Even Numbers After Queries
problem link
#solution
class Solution {
public int[] sumEvenAfterQueries(int[] nums, int[][] queries) {
int sum=0;
for(int n: nums) if(n%2==0) sum+=n;
int[] ans = new int[queries.length];
for(int i=0; i<queries.length; i++){
if((nums[queries[i][1]]+queries[i][0]) %2==0){
if(nums[queries[i][1]]%2==0)
sum+=queries[i][0];
else
sum+=nums[queries[i][1]]+queries[i][0];
}else{
if(nums[queries[i][1]] %2==0)
sum-=nums[queries[i][1]];
}
nums[queries[i][1]]+=queries[i][0];
ans[i]=sum;
}
return ans;
}
}🐳3👍2🕊1
image_2022-09-25_15-38-29.png
30.8 KB
#medium
#N1680. Concatenation of Consecutive Binary Numbers
problem link
#solution
#N1680. Concatenation of Consecutive Binary Numbers
problem link
#solution
class Solution {
public int concatenatedBinary(int n) {
int digits=0;
long res=0;
for(int i=1; i<=n; i++){
if((i&(i-1))==0) digits++;
res=((res<<digits)+i)%1000000007;
}
return (int) res;
}
}👍1
image_2022-09-25_15-53-22.png
39.6 KB
#N101. Symmetric Tree
problem link
#solution
problem link
#solution
class Solution {
public boolean isSymmetric(TreeNode root) {
return root==null||func(root.left, root.right);
}
public boolean func(TreeNode left, TreeNode right){
if(left==null && right==null) return true;
if(left==null ^ right==null || left.val!=right.val) return false;
return func(left.left, right.right) && func(left.right, right.left);
}
}⚡2👍1
image_2022-09-25_16-47-51.png
55.7 KB
#N125. Valid Palindrome
problem link
#solution
problem link
#solution
class Solution {
public boolean isPalindrome(String s) {
for(int i=0, j=s.length()-1; i<j; ){
char chari=s.charAt(i);
char charj=s.charAt(j);
if(!Character.isLetterOrDigit(chari)) {
i++; continue;
}
if(!Character.isLetterOrDigit(charj)) {
j--; continue;
}
if(Character.isLetterOrDigit(chari) && Character.isLetterOrDigit(charj)
&& Character.toLowerCase(chari)!=Character.toLowerCase(charj)) return false;
else{
i++;j--;
}
}
return true;
}
}👍2🍾2
image_2022-11-19_00-56-46.png
87 KB
#medium
#N8. String to Integer (atoi)
problem link
#solution
#N8. String to Integer (atoi)
problem link
#solution
class Solution {
public int myAtoi(String s) {
if(s.contains("+-")||s.contains("-+")) return 0;
int num=0;
boolean isPos=true, isbrak=true;
for(int i=0;i<s.length();i++){
if(Character.isLetter(s.charAt(i)) || s.charAt(i)=='.' || (s.charAt(i)==' ')&&!isbrak) break;
if(!isbrak && (s.charAt(i)=='-' || s.charAt(i)=='+')) break;
if(s.charAt(i)=='+'||s.charAt(i)=='-') isbrak=false;
if(Character.isDigit(s.charAt(i))){
isbrak=false;
int x=s.charAt(i)-'0';
if(num>Integer.MAX_VALUE/10 || (num==Integer.MAX_VALUE/10 && x>7)){
return isPos?Integer.MAX_VALUE:-Integer.MAX_VALUE-1;
}
num=num*10+x;
}
if(s.charAt(i)=='-') isPos=false;
}
return isPos?num:-num;
}
}
P.s.: F**k finally, after 28 submissions⚡3👍1
image_2022-11-25_21-47-57.png
69.7 KB
#N118. Pascal's Triangle
problem link
#solution
problem link
#solution
class Solution {
public List<List<Integer>> generate(int rows) {
List<List<Integer>> list = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
temp.add(1);
list.add(temp);
for(int i=2; i<=rows; i++){
List<Integer> temp1 = new ArrayList<>();
temp1.add(1);
for(int j=1; j<list.get(i-2).size(); j++){
temp1.add(list.get(i-2).get(j-1) + list.get(i-2).get(j));
}
temp1.add(1);
list.add(temp1);
}
return list;
}
}👍2
image_2022-11-30_23-05-13.png
48.5 KB
#N2367. Number of Arithmetic Triplets
problem link
#solution
problem link
#solution
class Solution {
public int arithmeticTriplets(int[] nums, int diff) {
int count=0;
Set<Integer> set = new HashSet<>();
for(int n: nums) set.add(n);
for(int i=0; i<nums.length; i++){
if(set.contains(nums[i]+diff) && set.contains(nums[i]+2*diff)) count++;
}
return count;
}
}⚡3👍1
image_2022-12-06_08-59-27.png
152.7 KB
I liked new UI of leetcode.com ;)
image_2022-12-13_01-43-00.png
12 KB
#N205. Isomorphic Strings
problem link
#solution
problem link
#solution
class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character, Character> map = new HashMap<>();
Map<Character, Character> map1 = new HashMap<>();
for(int i=0; i<s.length(); i++){
if(map.containsKey(s.charAt(i)) && map.get(s.charAt(i))!=t.charAt(i)
|| map1.containsKey(t.charAt(i)) && map1.get(t.charAt(i))!=s.charAt(i)){
return false;
}else if(!map.containsKey(s.charAt(i))){
map.put(s.charAt(i), t.charAt(i));
map1.put(t.charAt(i), s.charAt(i));
}
}
return true;
}
}⚡3👍1
image_2022-12-15_00-24-17.png
29.5 KB
Runtime 13 ms Beats 95.50%#N703. Kth Largest Element in a Stream
Memory 46.2 MB Beats 91.49%
problem link
#solution
class KthLargest {
int k;
PriorityQueue<Integer> pq;
public KthLargest(int k, int[] nums) {
pq=new PriorityQueue<>();
for(int n: nums) pq.offer(n);
this.k=k;
while(pq.size()>k) pq.poll();
}
public int add(int val) {
pq.offer(val);
if(pq.size()>k) pq.poll();
return pq.peek();
}
}⚡3👍1
image_2022-12-15_01-55-30.png
43.1 KB
Runtime 2 ms Beats 81.30% Memory 41.9 MB Beats 88.62%
#N883. Projection Area of 3D Shapes
problem link
#solution
class Solution {
public int projectionArea(int[][] grid) {
int xy=0, yz=0, zx=0, max=0;
for(int i=0; i<grid.length; i++){
for(int j=0; j<grid[0].length; j++){
if(grid[i][j]!=0) xy++;
max=Math.max(max, grid[i][j]);
}
zx+=max;
max=0;
}
for(int i=0; i<grid[0].length; i++){
for(int j=0; j<grid.length; j++)
max=Math.max(max, grid[j][i]);
yz+=max;
max=0;
}
return xy+yz+zx;
}
}👍5
image_2022-12-22_00-42-57.png
30.2 KB
Runtime 3 ms Beats 99.42%#N2500. Delete Greatest Value in Each Row
Memory 42.1 MB Beats 97.7%
problem link
#solution
class Solution {
public int deleteGreatestValue(int[][] grid) {
int m=grid.length, n=grid[0].length;
int sum=0, max=0;
for(int i=0; i<m; i++) Arrays.sort(grid[i]);
for(int i=n-1; i>=0; i--){
max=0;
for(int j=0; j<m; j++){
if(grid[j][i]>max) max=grid[j][i];
}
sum+=max;
}
return sum;
}
}⚡3
image_2022-12-22_00-57-50.png
35 KB
Runtime 1 ms Beats 98.18%
Memory 40.9 MB Beats 82%
#N2283. Check if Number Has Equal Digit Count and Digit Value
problem link
#solution
class Solution {
public boolean digitCount(String num) {
Map<Integer, Integer> map = new HashMap<>();
for(char c: num.toCharArray()){
int n=c-'0';
map.put(n, map.getOrDefault(n, 0) +1);
}
for(int i=0; i<num.length(); i++){
if(!map.containsKey(i) && num.charAt(i)!='0'
|| map.containsKey(i) && map.get(i)!=num.charAt(i)-'0') return false;
}
return true;
}
}⚡2👍2
image_2022-12-22_01-16-57.png
33.6 KB
Runtime16 msBeats61.69%#N1399. Count Largest Group
Memory41.4 MBBeats66.23%
problem link
#solution
class Solution {
public int countLargestGroup(int n) {
Map<Integer, Integer> map = new HashMap<>();
int count=0, max=0;
int[] dp = new int[n+1];
for(int i=1; i<=n; i++){
dp[i]=dp[i/10]+i%10;
map.put(dp[i], map.getOrDefault(dp[i], 0) +1);
if(map.get(dp[i])>max){
count=0;
max=map.get(dp[i]);
}
if(map.get(dp[i])==max) count++;
}
return count;
}
}👍2
image_2022-12-22_01-42-08.png
8.7 KB
#N2481. Minimum Cuts to Divide a Circle
problem link
#solution
problem link
#solution
class Solution {
public int numberOfCuts(int n) {
return (n==1?0:n/(2-n%2));
}
}👍2
#note
If we want to look through all subsets of array:
If we want to look through all subsets of array:
int n=arr.length;
for(int i=0; i<(1<<n); i++){
for(int j=0; j<n; j++){
if((i&(1<<j))>0){
System.out.println(arr[j]+" ");
}
}
}
🔥2👌1
image_2023-01-02_03-24-42.png
61 KB
Runtime1 msBeats95.21%
Memory40.3 MBBeats73.73%
#N290. Word Pattern
problem link
#solution
class Solution {
public boolean wordPattern(String pattern, String s) {
Map<Character, String> map = new HashMap<>();
int i=0;
for(String part: s.split(" ")){
if(i==pattern.length()||map.containsKey(pattern.charAt(i)) && !map.get(pattern.charAt(i)).equals(part)
||map.containsValue(part) && get(map, part)!=pattern.charAt(i))
return false;
map.put(pattern.charAt(i), part);
i++;
}
return i==pattern.length();
}
public char get(Map<Character, String> map, String part){
for (Map.Entry<Character, String> entry : map.entrySet()) {
if (entry.getValue().equals(part)) {
return entry.getKey();
}
}
return '0';
}
}👍2