leo_0

1.3万

leo_0
15天前

### 题目描述

blablabla

#### 样例

blablabla


### 算法1

#### Java 代码

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
int lo = 0;
int hi = m*n-1;
while(lo <= hi){
int mid = lo + (hi-lo>>1);
int pivot = matrix[mid/n][mid%n];
if(pivot == target){
return true;
}else if(pivot < target){
lo++;
}else{
hi--;
}
}
return false;
}
}


leo_0
15天前
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
int lo = 0;
int hi = m*n-1;
while(lo <= hi){
int mid = lo + (hi-lo>>1);
int pivot = matrix[mid/n][mid%n];
if(pivot == target){
return true;
}else if(pivot < target){
lo++;
}else{
hi--;
}
}
return false;
}
}


leo_0
19天前

### 题目描述

blablabla

#### 样例

blablabla


### 算法1

blablabla

#### Java 代码

/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode() {}
*     ListNode(int val) { this.val = val; }
*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy.next;
ListNode prev = dummy;
while(cur != null){
if(cur.next != null && cur.val == cur.next.val){
while(cur.next != null && cur.val == cur.next.val){
cur = cur.next;
}
prev.next = cur.next;
}else{
prev = prev.next;
}
cur = cur.next;
}

return dummy.next;
}
}


leo_0
19天前
/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode() {}
*     ListNode(int val) { this.val = val; }
*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy.next;
ListNode prev = dummy;
while(cur != null){
if(cur.next != null && cur.val == cur.next.val){
while(cur.next != null && cur.val == cur.next.val){
cur = cur.next;
}
prev.next = cur.next;
}else{
prev = prev.next;
}
cur = cur.next;
}

return dummy.next;
}
}


leo_0
21天前

### 题目描述

blablabla

#### 样例

blablabla


### 算法1

Stack

#### Java 代码

class Solution {
public boolean find132pattern(int[] nums) {
if(nums.length < 3) return false;
Stack<Integer> stack = new Stack<>();
int second = Integer.MIN_VALUE;
stack.push(nums[nums.length-1]);
for(int i = nums.length-2;i>=0;i--){
if(nums[i]< second){
return true;
}else{
while(!stack.isEmpty() && stack.peek() < nums[i]){
second = stack.pop();
}
stack.push(nums[i]);
}
}
return false;
}
}


leo_0
21天前
class Solution {
public boolean find132pattern(int[] nums) {
if(nums.length < 3) return false;
Stack<Integer> stack = new Stack<>();
int second = Integer.MIN_VALUE;
stack.push(nums[nums.length-1]);
for(int i = nums.length-2;i>=0;i--){
if(nums[i]< second){
return true;
}else{
while(!stack.isEmpty() && stack.peek() < nums[i]){
second = stack.pop();
}
stack.push(nums[i]);
}
}
return false;
}
}


leo_0
22天前

### 题目描述

blablabla

#### 样例

blablabla


### 算法1

list + 递归

#### 时间复杂度

Time complexity:
Constructor: O(N + L)
next(): O(1)
hasNext(): O(1)
Space complexity : O(N + D)

#### Java 代码

/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
*
*     // @return true if this NestedInteger holds a single integer, rather than a nested list.
*     public boolean isInteger();
*
*     // @return the single integer that this NestedInteger holds, if it holds a single integer
*     // Return null if this NestedInteger holds a nested list
*     public Integer getInteger();
*
*     // @return the nested list that this NestedInteger holds, if it holds a nested list
*     // Return empty list if this NestedInteger holds a single integer
*     public List<NestedInteger> getList();
* }
*/
public class NestedIterator implements Iterator<Integer> {
private List<Integer> list = new ArrayList<Integer>();
private int position = 0; // Pointer to next integer to return.

public NestedIterator(List<NestedInteger> nestedList) {
flattenList(nestedList);
}

private void flattenList(List<NestedInteger> nestedList) {
for (NestedInteger nestedInteger : nestedList) {
if (nestedInteger.isInteger()) {
} else {
flattenList(nestedInteger.getList());
}
}
}

@Override
public Integer next() {
if (hasNext()){
return list.get(position++);
}
return null;

}

@Override
public boolean hasNext() {
return position < list.size();
}
}

/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/


leo_0
22天前
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
*
*     // @return true if this NestedInteger holds a single integer, rather than a nested list.
*     public boolean isInteger();
*
*     // @return the single integer that this NestedInteger holds, if it holds a single integer
*     // Return null if this NestedInteger holds a nested list
*     public Integer getInteger();
*
*     // @return the nested list that this NestedInteger holds, if it holds a nested list
*     // Return empty list if this NestedInteger holds a single integer
*     public List<NestedInteger> getList();
* }
*/
public class NestedIterator implements Iterator<Integer> {
private List<Integer> list = new ArrayList<Integer>();
private int position = 0; // Pointer to next integer to return.

public NestedIterator(List<NestedInteger> nestedList) {
flattenList(nestedList);
}

private void flattenList(List<NestedInteger> nestedList) {
for (NestedInteger nestedInteger : nestedList) {
if (nestedInteger.isInteger()) {
} else {
flattenList(nestedInteger.getList());
}
}
}

@Override
public Integer next() {
if (hasNext()){
return list.get(position++);
}
return null;

}

@Override
public boolean hasNext() {
return position < list.size();
}
}

/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/


leo_0
23天前
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int sum = 0;
while (n != 0) {
sum++;
n &= (n - 1);
}
return sum;
}
}


leo_0
25天前
class Solution {
private final static Map<String, BiFunction<Integer, Integer, Integer>> map = new HashMap<>();
static{
map.put("+", (a,b)->a+b);
map.put("-", (a,b)->a-b);
map.put("*", (a,b)->a*b);
map.put("/", (a,b)->a/b);
}

public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for(String s:tokens){
if(map.containsKey(s)){
int num2 = stack.pop();
int num1 = stack.pop();
BiFunction<Integer, Integer, Integer> operation = map.get(s);
int res = operation.apply(num1, num2);
stack.push(res);
}else{
stack.push(Integer.valueOf(s));
}
}
return stack.pop();
}
}