454. 4Sum II

题意:给四个数组,每个数组内取一个数使得四个数和为0,问有多少种取法

思路:枚举为On4,考虑两个数组,On2枚举所有可能的和,将和的出现次数存入map中,On2枚举另两个数组,看是否加和为0

class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
int na = A.size(), nb = B.size(), nc = C.size(), nd = D.size();
int cnt = ;
map<int, int> mp;
for (int i = ; i < na; i++) {
for (int j = ; j < nb; j++) {
int sum = A[i] + B[j];
if (mp[-sum]) mp[-sum]++;
else mp[-sum] = ;
}
}
for (int i = ; i < nc; i++) {
for (int j = ; j < nd; j++) {
int sum = C[i] + D[j];
if (mp[sum]) cnt += mp[sum];
}
}
return cnt;
}
};

24. Swap Nodes in Pairs

题意:交换一个链表中每相邻两个元素

想看到就是多加个头的空指针操作(

ListNode *emptyhead = new ListNode(-);
emptyhead -> next = head;
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode *emptyhead = new ListNode(-);
emptyhead -> next = head;
ListNode *p = head, *q, *lst = emptyhead;
while (lst -> next && lst -> next -> next) {
p = lst -> next;
q = p -> next;
p -> next = q -> next;
q -> next = p;
lst -> next = q;
lst = p;
}
return emptyhead -> next;
}
};

25. Reverse Nodes in k-Group

题意:将每k个元素倒序

Given this linked list: ->->->->

For k = , you should return: ->->->->

For k = , you should return: ->->->->

思路:用两个指针来取倒序的一段

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode *emptyhead = new ListNode(-);
emptyhead -> next = head;
ListNode *pre = emptyhead, *cur = emptyhead;
int i = ;
while (cur -> next) {
i++;
cur = cur -> next;
if (i % k == ) {
pre = reverse(pre, cur -> next);
cur = pre;
}
}
return emptyhead -> next;
}
ListNode* reverse(ListNode* pre, ListNode* nxt) {
ListNode *lst, *cur;
lst = pre -> next;
cur = lst -> next;
while (cur != nxt) {
lst -> next = cur -> next;
cur -> next = pre -> next;
pre -> next = cur;
cur = lst -> next;
}
return lst;
} };

29. Divide Two Integers

题意:不用乘除模运算,完成整数乘法,若溢出则输出INT_MAX,向0取整

思路:思路是往下减,一个一个减布星,来移位

class Solution {
public:
int divide(int dividend, int divisor) {
long long res = ;
int flag = ;
if ((dividend < && divisor > ) || (dividend > && divisor < )) {
flag = -;
}
long long m = abs((long long)dividend);
long long n = abs((long long)divisor);
long long base, t;
while (m >= n) {
base = n;
t = ;
while (m >= (base << )) {
base <<= ;
t <<= ;
}
m -= base;
res += t;
}
if (flag == -) return int(-res);
if (res > ) return ;
return res;
}
};

31. Next Permutation

题意:给一个序列,输出字典序下一个的序列

思路:对于已经是字典序的序列特判,输出倒序;其他情况下,

从后往前找到第一个开始减小的元素

1 5 4 3 1

然后从后往前找到第一个比2大的元素,交换它们

1 5 4 1

然后把3之后的序列倒置

1 3 1 2 4 5

嘛拿纸笔画一画就好惹(

class Solution {
public:
void nextPermutation(vector<int>& nums) {
int n = nums.size();
int flag = ;
int last, idx;
for (int i = n - ; i >= ; i--) {
if (nums[i] < nums[i + ]) {
last = nums[i];
idx = i;
flag = ;
break;
}
}
if (flag == ) {
reverse(nums.begin(), nums.end());
} else {
for (int i = n - ; i >= idx + ; i--) {
if (nums[i] > last) {
nums[idx] = nums[i];
nums[i] = last;
break;
}
}
reverse(nums.begin() + idx + , nums.end());
}
return;
}
};
// 1 2 4 5 6 -> 1 2 4 6 5
// 1 2 4 6 5 -> 1 2 5 4 6
// 1 3 4 2 -> 1 4 2 3
// 3 4 2 1 -> 4 1 2 3

46. Permutations

题意:输出全排列

next_permutation 输出从当前排列之后的所有全排列 (所以要 sort 和 do while)

class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
do {
res.push_back(nums);
} while (next_permutation(nums.begin(), nums.end()));
return res;
}
};

56. Merge Intervals

结构体vector的排序姿势……?

为啥cmp不行啊qwq(

struct Interval {
int start;
int end;
Interval() : start(), end() {}
Interval(int s, int e) : start(s), end(e) {}
};
vector<Interval>& intervals;
sort(intervals.begin(), intervals.end(), [](Interval &a, Interval &b) {return a.start < b.start;});

LeetCode || 大杂烩w的更多相关文章

  1. lucene入门创建索引——(二)

    1.程序宏观结构图

  2. leetCode刷题(将字符串转成W形状的字符串再以Z形字符串输出)

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  3. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  4. [LeetCode] Boom Enemy 炸弹人

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  5. [LeetCode] Russian Doll Envelopes 俄罗斯娃娃信封

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  6. [LeetCode] Strobogrammatic Number III 对称数之三

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  7. [LeetCode] Word Ladder II 词语阶梯之二

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  8. [LeetCode] Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  9. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

随机推荐

  1. opencv3.1 压缩并拼图

    必须有重叠才能拼,压缩越多,拼接越快 #include <opencv2\opencv.hpp> #include <opencv2\stitching.hpp> using ...

  2. bzoj 5120: [2017国家集训队测试]无限之环【最小费用最大流】

    玄妙的建图-- 这种平衡度数的题按套路是先黑白染色然后分别连ST点,相邻格子连黑向白连费用1流量0的边,然后考虑费用怎么表示 把一个点拆成五个,上下左右中,中间点黑白染色连ST, 对于连S的点,中点连 ...

  3. 【TeamViewer】v13.2.26558版本 修改ID

    TeamViewer是一款远程协作软件,可以让你在一台机器上操作另一台机器.比如我最近就经常在家里连接公司的电脑进行远程工作.可以说是对于程序员很好用的一个软件. TeamViewer 使用频繁后会被 ...

  4. (转)关于MongoDB你需要知道的几件事

    本文列举了颇让作者困惑的一些MongoDB限制,如果你也打算使用MongoDB,那么至少要提前了解这些限制,以免遇到的时候措手不及. 消耗磁盘空间 这是我的第一个困惑:MongoDB会消耗太多的磁盘空 ...

  5. 51Nod 1242 斐波那契数列的第N项(矩阵快速幂)

    #include <iostream> #include <algorithm> using namespace std; typedef long long LL; ; ; ...

  6. E.华华给月月准备礼物

    链接:https://ac.nowcoder.com/acm/contest/392/E 题意: 二月中旬虐狗节前夕,华华决定给月月准备一份礼物.为了搭建礼物的底座,华华需要若干根同样长的木棍.华华手 ...

  7. <?php } ?> 标记

    只是为了分离php 和html 代码的一种书写方法. 你要知道 一段程序代码 function fool(){//内容}是这么组成的那么当有html代码的时候就需要先暂时将php的开始部分给分开(不分 ...

  8. Glide加载图片的事例

    //获取图片的url String url = resultsEntity.getUrl(); //判断获取的图片是否存在 if (resultsEntity.getItemHeight() > ...

  9. C8051F单片机定时器的定时

    假设C8051F020单片机的晶振是sysclk=22114800HZ,即每秒计22114800个数经过Div=12分频后得到定时器的计数频率Tclk=sysclk/12,每秒计22114800÷12 ...

  10. Leetcode 题解 - 目录

    本文从 Leetcode 中精选大概 200 左右的题目,去除了某些繁杂但是没有多少算法思想的题目,同时保留了面试中经常被问到的经典题目. 算法思想 双指针 排序 贪心思想 二分查找 分治 搜索 动态 ...