leetcode
SortedX
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
832. Flipping an Image
比较简单的一道题,利用了数组只有1,0,互换加翻转就是1-要互换的数字要注意的是:在循环里,如果是奇数个元素,中间的元素不参与循环。class Solution {public: vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) { for...原创 2019-04-03 20:42:05 · 158 阅读 · 0 评论 -
977 Squares of a Sorted Array
1、第一种方法就是遍历一遍,将每个元素求平均,然后存入数组,利用C++ 自带的排序函数排序。当然,时间耗时最长140ms。#include <algorithm>sort(A.begin(), A.end());2、遍历一遍,获得平方的数组和最后一个负数的坐标。获得负数的坐标neg后,若有非负数,则非负数的坐标从neg+1开始,正数和负数分开处理。,时间耗时104ms。...原创 2019-04-03 19:47:33 · 210 阅读 · 0 评论 -
852 Peak Index in a Mountain Array
1、部分有序数组中寻找最大值 int peakIndexInMountainArray(vector<int>& A) { int len = A.size() - 1; int i = 0; while (true) { int pos = (len + i) / 2; if (A[pos-1] > A[pos]...原创 2019-04-09 17:30:31 · 132 阅读 · 0 评论 -
944 Delete Columns to Make Sorted && vector使用
涉及到两层循环,数组的遍历和字母的遍历。1、刚开始是外循环数组,再判断字母,这样在判断的时候需要标记不符合条件的字母位置,这样数组循环时,被标记的字母位置不需要加入比较,因此需要额外的数组来记录不符合的位置。 // The two-level loop order is important int minDeletionSize(vector<string>& ...原创 2019-04-09 17:24:05 · 157 阅读 · 0 评论 -
728 Self Dividing Numbers ???
1、这道题就是将一个整型数据每位数字剥离出来,看看是不是整形数据的因子。题目不难,但是出现了个问题??? vector<int> iselfDividingNumbers(int left, int right) { vector <int> res; if (left > right) return res; for (int i...原创 2019-04-09 16:49:23 · 165 阅读 · 0 评论 -
617 Merge Two Binary Trees
1、二叉树基本就是涉及到递归问题。这道题2种思路,一种是直接在其中一棵树上合并另一棵树,一种是创建新树。// Definition for a binary tree node.struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL)...原创 2019-04-09 16:46:17 · 149 阅读 · 0 评论 -
461 Hamming Distance
1、这个题目由于对整数的与或非不熟悉,走了弯路,先将整数转换成了二进制并将每位数字存入整型数组,其实不用转换。将二进制存入数组是要考虑高位补0的,所以真的好麻烦。 int hammingDistance(int x, int y) { int resX[31] = {0}; int resY[31] = {0}; int res = 0; trans2b...原创 2019-04-09 16:42:58 · 229 阅读 · 0 评论 -
657 Robot Return to Origin
1、这个比较简单,向上走的步数和向下走的步数相同,向左走的步数和向右走的步数一致就一定能回到原点。所以设置两个数记录上下、左右的步数即可。 bool judgeCircle(string moves) { int ud=0, rl=0; for (int i=0; i < moves.length(); ++i) { switch (moves[i]) {...原创 2019-04-09 16:26:39 · 191 阅读 · 0 评论 -
加速C++:std::ios::sync_with_stdio(false);cin.tie(NULL);
static int x = [](){ ios::sync_with_stdio(false); cin.tie(NULL); // cout.tie(NULL); return 0; }();整体上这个函数是可以加速的!1、static int x = [](){}(); 这种奇怪的函数写法,据说是C++11新引入的lamnda 表达式,C++11我也不...原创 2019-04-02 21:28:34 · 2138 阅读 · 3 评论 -
961. N-Repeated Element in Size 2N Array
本身题目没有什么难度,分析好题目就避免全部遍历了。一共2N个数字,其中有N+1个不同的,有某一个数字重复N遍,也就是说,其他N个不同的数字只能出现一次,所以就是判断哪个数字出现大于一次就好了。 int repeatedNTimes(vector<int>& A) { map<int,int> res; for (int i=0; i &l...原创 2019-04-02 21:29:32 · 139 阅读 · 0 评论 -
929 Unique Email Addresses
快的方法有一些还没看懂,过段时间看1、笨方法从题中可以看出要分2部分考虑local name 和domain name,所以设置了flag来标记是哪一部分;考虑到“+”情况,local name中“+”后的内容到“@”全部删掉,但是在domain name中内容继续添加,就有设置了plus_flag来区分情况。plus_flag涉及到2次修改,碰到“+”和“@”都要进行修改。...原创 2019-04-01 20:08:04 · 467 阅读 · 0 评论 -
804 Unique Morse Code Words
1、将小写的26个字母的大小转化为从0开始 int uniqueMorseRepresentations(vector<string>& words) { string morse_code[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..", ".---","-.-"...原创 2019-04-01 20:01:08 · 170 阅读 · 0 评论 -
709 To Lower Case
1、 a = 97 A =65 string toLowerCase(string str) { for (int i=0; i < str.size(); ++i) { if (str[i] >= 'a' && str[i] <= 'z') { continue; } else if (str[i] &g...原创 2019-04-01 19:56:57 · 117 阅读 · 0 评论 -
771. Jewels and Stones
这涉及到两个数组查找。1、首先想到的就是暴力查找,两层叠加循环。 int numJewelsIsStone(string J, string S) { int num = 0; for (int i=0; i < S.size(); ++i) { for (int j=0; j < J.size(); ++j) { if (S...原创 2019-04-01 19:53:50 · 113 阅读 · 0 评论 -
905. Sort Array By Parity
比较简单的一道题目。 vector<int> sortArrayByParity(vector<int>& A) { vector<int> res(A.size()); int even = 0, odd = A.size() - 1; for (int i=0; i < A.size(); ++i) { ...原创 2019-04-03 19:48:37 · 154 阅读 · 0 评论
分享