image_2021-11-19_01-41-26.png
31.8 KB
#N268. Missing Number
problem link
#solution
problem link
#solution
class Solution {
public int missingNumber(int[] nums) {
int count[]=new int[nums.length+1];
for(int num:nums)
count[num]++;
for(int i=0; i<count.length; i++){
if(count[i]==0)
return i;
}
return 0;
}
}image_2021-11-19_01-54-18.png
35.9 KB
#N141. Linked List Cycle
problem link
#solution
problem link
#solution
public class Solution {
public boolean hasCycle(ListNode head) {
if(head==null) return false;
ListNode curr=head, ahead=head;
while(ahead.next!=null&&ahead.next.next!=null){
curr=curr.next;
ahead=ahead.next.next;
if(curr==ahead)
return true;
}
return false;
}
}image_2021-11-19_14-54-18.png
29 KB
#N461. Hamming Distance
problem link
#solution
problem link
#solution
class Solution {
public int hammingDistance(int x, int y) {
int num=x^y;
int count=0;
while(num>0){
if(num%2==1)
count++;
num/=2;
}
return count;
}
}image_2021-11-19_15-28-32.png
56.6 KB
#medium
#N1721. Swapping Nodes in a Linked List
problem link
#solution
#N1721. Swapping Nodes in a Linked List
problem link
#solution
class Solution {
public ListNode swapNodes(ListNode head, int k) {
int length=0, k1=k;
ListNode curr=head;
while(curr!=null){
curr=curr.next;
length++;
}
ListNode first=head, second=head;
while(k>1){
first=first.next;
k --;
}
while(length-k1>0){
second=second.next;
length--;
}
int temp=first.val;
first.val=second.val;
second.val=temp;
return head;
}
}image_2021-11-19_16-28-32.png
50.6 KB
#N1021. Remove Outermost Parentheses
problem link
#solution
problem link
#solution
class Solution {
public String removeOuterParentheses(String s) {
StringBuilder res=new StringBuilder();
Stack stack=new Stack();
int size=0;
for(char ch:s.toCharArray()){
if(ch=='('){
stack.push(ch);
size++;
if(size!=1)
res.append(ch);
}
if(ch==')'){
stack.pop();
size--;
if(size!=0)
res.append(ch);
}
}
return res.toString();
}
}image_2021-11-19_17-27-59.png
35.1 KB
#N1475. Final Prices With a Special Discount in a Shop
problem link
#solution
problem link
#solution
class Solution {
public int[] finalPrices(int[] prices) {
Stack<Integer> stack=new Stack<>();
for(int i=0; i<prices.length; i++){
while(stack.size()>0&&prices[stack.peek()]>=prices[i]){
prices[stack.peek()]-=prices[i];
stack.pop();
}
stack.push(i);
}
return prices;
}
}image_2021-11-19_17-58-20.png
48.5 KB
#N1047. Remove All Adjacent Duplicates In String
problem link
#solution
problem link
#solution
class Solution {
public String removeDuplicates(String s) {
StringBuilder res=new StringBuilder();
Stack<Character> stack=new Stack<>();
char[] letters=s.toCharArray();
for(int i=letters.length-1; i>=0; i--){
if(!stack.isEmpty()&&letters[i]==stack.peek())
stack.pop();
else
stack.push(letters[i]);
}
while(!stack.isEmpty()){
res.append(stack.pop());
}
return res.toString();
}
}image_2021-11-22_11-40-28.png
37 KB
#N1700. Number of Students Unable to Eat Lunch
problem link
#solution
problem link
#solution
class Solution {
public int countStudents(int[] students, int[] sandwiches) {
int[] count=new int[2];
int i=0;
for(int student:students)
count[student]++;
for(; i<sandwiches.length && count[sandwiches[i]]!=0; i++)
count[sandwiches[i]]--;
return sandwiches.length-i;
}
}image_2021-11-22_18-01-53.png
45.3 KB
#N933. Number of Recent Calls
problem link
#solution
problem link
#solution
class RecentCounter {
Queue<Integer> queue = new LinkedList<>();
public RecentCounter() {
}
public int ping(int t) {
while(!queue.isEmpty() && queue.peek()<t-3000) queue.remove();
queue.add(t);
return queue.size();
}
}image_2021-11-24_13-46-29.png
43.9 KB
#N1656. Design an Ordered Stream
problem link
#solution
problem link
#solution
class OrderedStream {
int index=1;
Map<Integer, String> map=new HashMap<>();
public OrderedStream(int n) {
}
public List<String> insert(int key, String value) {
map.put(key, value);
List<String> list=new ArrayList<>();
while(map.containsKey(index)){
list.add(map.get(index));
index++;
}
return list;
}
}image_2021-11-24_17-35-55.png
43.3 KB
#N938. Range Sum of BST
problem link
#solution
problem link
#solution
class Solution {
int sum=0;
public int rangeSumBST(TreeNode root, int low, int high) {
if(root == null) {
return 0;
}
if(low<=root.val&&high>=root.val) sum+=root.val;
if(root.val>low){
rangeSumBST(root.left, low, high);
}
if(root.val<=high){
rangeSumBST(root.right, low, high);
}
return sum;
}
}image_2021-11-25_13-24-04.png
40.7 KB
#N897. Increasing Order Search Tree
problem link
#solution
problem link
#solution
class Solution {
TreeNode res = new TreeNode();
TreeNode curr=res;
public TreeNode increasingBST(TreeNode root) {
inOrder(root);
return res.right;
}
public void inOrder(TreeNode root){
if(root == null) return;
inOrder(root.left);
curr.right = new TreeNode(root.val);
curr = curr.right;
inOrder(root.right);
}
}image_2021-11-25_13-25-08.png
35.9 KB
#N700. Search in a Binary Search Tree
problem link
#solution
problem link
#solution
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if(root == null) return null;
if(root.val>val){
root = searchBST(root.left, val);
}
else if(root.val<val){
root = searchBST(root.right, val);
}
return root;
}
}image_2021-11-27_01-37-20.png
32.3 KB
#N35. Search Insert Position
problem link
#solution
problem link
#solution
class Solution {
public int searchInsert(int[] nums, int target) {
int start=0, end=nums.length-1, mid;
while(start<=end){
mid=(start+end)/2;
if(nums[mid]==target) return mid;
else if(nums[mid]<target) start=mid+1;
else if(nums[mid]>target) end=mid-1;
}
return start;
}
}image_2021-11-27_02-12-57.png
16.5 KB
#N2057. Smallest Index With Equal Value
problem link
#solution
problem link
#solution
class Solution {
public int smallestEqual(int[] nums) {
for(int i=0; i<nums.length; i++)
if((i-nums[i])%10==0) return i;
return -1;
}
}image_2021-11-27_02-27-01.png
39.4 KB
#N2078. Two Furthest Houses With Different Colors
problem link
#solution
problem link
#solution
class Solution {
public int maxDistance(int[] colors) {
int x1=0, x2=0;
for(int i=colors.length-1; i>0; i--)
if(colors[i]!=colors[0]){
x1=i;
break;
}
for(int i=0; i<colors.length-1; i++)
if(colors[i]!=colors[colors.length-1]) {
x2=colors.length-1-i;
break;
}
return Math.max(x1, x2);
}
}#statistics
Till this time, 144 in total,
133 easy;
11 medium;
type of problem solutions have been posted
Till this time, 144 in total,
133 easy;
11 medium;
type of problem solutions have been posted
image_2021-11-29_17-37-18.png
29.5 KB
#N2089. Find Target Indices After Sorting Array
problem link
#solution
problem link
#solution
class Solution {
public List<Integer> targetIndices(int[] nums, int target) {
Arrays.sort(nums);
List<Integer> list = new ArrayList<>();
for(int i=0; i<nums.length; i++)
if(nums[i]==target) list.add(i);
return list;
}
}image_2021-11-29_18-29-48.png
40.7 KB
#N1991. Find the Middle Index in Array
problem link
#solution
problem link
#solution
class Solution {
public int findMiddleIndex(int[] nums) {
int sum = 0, preSum=0, index=-1;
for(int n:nums)
sum+=n;
for(int i=0; i<nums.length; i++){
if(sum-nums[i]==preSum){
return i;
}
else{
preSum+=nums[i];
sum-=nums[i];
}
}
return index;
}
}image_2021-11-29_18-31-35.png
36.8 KB
#N724. Find Pivot Index
problem link
#solution
problem link
#solution
class Solution {
public int pivotIndex(int[] nums) {
int sum = 0, preSum=0, index=-1;
for(int n:nums)
sum+=n;
for(int i=0; i<nums.length; i++){
if(sum-nums[i]==preSum){
return i;
}
else{
preSum+=nums[i];
sum-=nums[i];
}
}
return index;
}
}