Skip to content

Instantly share code, notes, and snippets.

View bnyu's full-sized avatar
💕
hello world

xuyue bnyu

💕
hello world
View GitHub Profile
@bnyu
bnyu / ip_port.go
Last active August 3, 2022 03:48
IP常用字符串转换
package util
import (
"strconv"
"strings"
)
func GetIpAndPortFromAddr(addr string) (ip string, port string) {
if strings.Contains(addr, ".") { //eg "192.0.2.1:25"
if ipPort := strings.Split(addr, ":"); len(ipPort) == 2 {
@bnyu
bnyu / sync_map.go
Created April 9, 2022 16:14
generics wrapper of go sync map
package util
import "sync"
type SyncMap[K any, V any] struct {
inter *sync.Map
}
func NewSyncMap[K any, V any]() SyncMap[K, V] {
return SyncMap[K, V]{
package util
import (
"fmt"
"strconv"
"strings"
)
var bitsUnits = []string{"", "K", "M", "G", "T", "P", "E", "Z"}
@bnyu
bnyu / Solution040.java
Created November 6, 2017 17:55
40. Combination Sum 2
import java.util.ArrayList;
import java.util.List;
/**
* 40. Combination Sum 2
* Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
* Each number in C may only be used once in the combination.
* Note:
* All numbers (including target) will be positive integers.
* The solution set must not contain duplicate combinations.
@bnyu
bnyu / Solution039.java
Created November 6, 2017 17:55
39. Combination Sum
import java.util.ArrayList;
import java.util.List;
/**
* 39. Combination Sum
* Given a set of candidate numbers (C) (without duplicates) and a target number (T),
* find all unique combinations in C where the candidate numbers sums to T.
* The same repeated number may be chosen from C unlimited number of times.
* Note:
* All numbers (including target) will be positive integers.
@bnyu
bnyu / Solution037.java
Created November 6, 2017 17:55
37. Sudoku Solver
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* 37. Sudoku Solver
* Write a program to solve a Sudoku puzzle by filling the empty cells.
* Empty cells are indicated by the character '.'.
* You may assume that there will be only one unique solution.
@bnyu
bnyu / Solution036.java
Last active November 6, 2017 18:36
36. Valid Sudoku
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* 36. Valid Sudoku
* Determine if a Sudoku is valid.
* The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
* A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
@bnyu
bnyu / Solution035.java
Created November 6, 2017 17:54
35. Search Insert Position
/**
* 35. Search Insert Position
* Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
* You may assume no duplicates in the array.
*/
public class Solution035 {
public int searchInsert(int[] nums, int target) {
int first = 0;
int last = nums.length - 1;
if (last < 0 || target <= nums[first])
@bnyu
bnyu / Solution034.java
Created November 6, 2017 17:54
34. Search for a Range
/**
* 34. Search for a Range
* Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.
* Your algorithm's runtime complexity must be in the order of O(log n).
* If the target is not found in the array, return [-1, -1].
*/
public class Solution034 {
public int[] searchRange(int[] nums, int target) {
int first = 0;
int last = nums.length - 1;
@bnyu
bnyu / Solution033.java
Created November 6, 2017 17:54
33. Search in Rotated Sorted Array
/**
* 33. Search in Rotated Sorted Array
* Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
* (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
* You are given a target value to search. If found in the array return its index, otherwise return -1.
* You may assume no duplicate exists in the array.
*/
class Solution033 {
public int search(int[] nums, int target) {
int first = 0;