Hola a todos, soy
方圆
una y otra vez, solo Seúl cocinado a mano
Número de pregunta
Relacionado con hash
1. la suma de dos números
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] {
map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
}
Operación de lista vinculada
2. Suma dos números
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode pre = new ListNode(0);
//这个当前节点很妙
ListNode cur = pre;
int add = 0;
while(l1 != null || l2 != null){
int x = l1 != null ? l1.val : 0;
int y = l2 != null ? l2.val : 0;
int sum = x + y + add;
int temp = sum % 10;
add = sum / 10;
cur.next = new ListNode(temp);
cur = cur.next;
//注意这里判断不是l1.next != null
if(l1 != null) l1 = l1.next;
if(l2 != null) l2 = l2.next;
}
if(add == 1)
cur.next = new ListNode(1);
return pre.next;
}
}
19. Elimina el nodo N de la parte inferior de la lista vinculada.
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode pre = new ListNode(0);
pre.next = head;
ListNode temp = pre;
ListNode cur = pre;
while(n != 0){
temp = temp.next;
n--;
}
while(temp.next != null){
temp = temp.next;
cur = cur.next;
}
cur.next = cur.next.next;
return pre.next;
}
}
61. Lista vinculada rotativa
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null || head.next == null)
return head;
ListNode old_tail = head;
int length = 1;
while(old_tail.next != null){
length++;
old_tail = old_tail.next;
}
//首尾相连
old_tail.next = head;
//找到新的尾巴需要走几步,比如说,实例1中
//从尾巴开始找是走k步就行,但是我们是从头部开始找
//就要用总长度减去k的值,还要减1是因为我们开始就在头部了
int step = length - k%length - 1;
ListNode new_tail = head;
for(int i = 0;i < step;i++)
new_tail = new_tail.next;
ListNode res = new_tail.next;
new_tail.next = null;
return res;
}
}
138. Copiar lista vinculada con puntero aleatorio
//做过第三遍了还是没想起来这个!
class Solution {
Map<Node,Node> map = new HashMap<>();
public Node copyRandomList(Node head) {
if(head == null)
return null;
if(map.containsKey(head))
return map.get(head);
Node node = new Node(head.val);
map.put(head,node);
node.next = copyRandomList(head.next);
node.random = copyRandomList(head.random);
return node;
}
}
206. Voltear lista vinculada
//第三遍了。。。
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null) return head;
ListNode pre = new ListNode(0);
ListNode cur = head;
ListNode temp = null;
while(cur != null){
temp = cur.next;
cur.next = pre.next;
pre.next = cur;
cur = temp;
}
return pre.next;
}
}
Recorrido de doble puntero (ventana deslizante)
3. La subcadena más larga sin caracteres repetidos
class Solution {
public int lengthOfLongestSubstring(String s) {
int length = s.length();
int res = 0;
HashMap<Character,Integer> map = new HashMap<>();
if(length == 0) return 0;
for(int start = 0,end = 0;end < length;end++){
if(map.containsKey(s.charAt(end)))
start = Math.max(start,map.get(s.charAt(end)) + 1);//abcab
map.put(s.charAt(end),end);
res = Math.max(res,end - start + 1);
}
return res;
}
}
11. El recipiente con más agua
class Solution {
public int maxArea(int[] height) {
int left = 0,right = height.length - 1;
int res = 0;
while(left <= right){
res = Math.max(res,
Math.min(height[left],height[right]) * (right - left));
if(height[left] < height[right]) left++;
else right--;
}
return res;
}
}
15. Sansanowa
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
int length = nums.length;
if(nums == null || length < 3) return res;
Arrays.sort(nums);
for(int i = 0;i < length;i++){
if(nums[i] > 0) break;
if(i > 0 && nums[i] == nums[i - 1]) continue;
int L = i + 1;
int R = length - 1;
while(L < R){
int sum = nums[i] + nums[L] + nums[R];
if(sum == 0){
res.add(Arrays.asList(nums[i],nums[L],nums[R]));
while(L < R && nums[L] == nums[L + 1]) L++;
while(L < R && nums[R] == nums[R - 1]) R--;
L++;
R--;
}else if(sum > 0) R--;
else L++;
}
}
return res;
}
}
16. La suma más cercana de tres números
class Solution {
public int threeSumClosest(int[] nums, int target) {
int res = nums[0] + nums[1] + nums[2];
int length = nums.length;
Arrays.sort(nums);
for(int i = 0;i < length;i++){
int L = i + 1;
int R = length - 1;
while(L < R){
int sum = nums[i] + nums[L] + nums[R];
if(Math.abs(sum - target) < Math.abs(res - target))
res = sum;
if(sum > target) R--;
else if(sum < target) L++;
else return sum;
}
}
return res;
}
}
26. Eliminar duplicados en una matriz ordenada
class Solution {
public int removeDuplicates(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int left = 0;
int right = 1;
while(right < nums.length){
if(nums[left] != nums[right]){
//把不重复的东西存储到左边儿
nums[left + 1] = nums[right];
left++;
}
right++;
}
return left + 1;
}
}
121. El mejor momento para comprar y vender acciones
class Solution {
public int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int res = 0;
for(int i = 0;i < prices.length;i++){
minPrice = Math.min(minPrice,prices[i]);
res = Math.max(res,prices[i] - minPrice);
}
return res;
}
}
209. El subarreglo más pequeño
class Solution {
public int minSubArrayLen(int s, int[] nums) {
if(nums.length == 0) return 0;
int left = 0,right = 0;
int sum = 0;
int res = Integer.MAX_VALUE;
while(right < nums.length){
sum += nums[right];
while(sum >= s){
res = Math.min(res,right - left + 1);
sum -= nums[left++];
}
right++;
}
return res == Integer.MAX_VALUE ? 0 : res;
}
}
¡Frio!