LeetCode练习
Joey_PRC
离优秀还差着十万八千里
展开
-
LeetCode 15. 3Sum
参考了别人的思路,效果并不好。。class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> res; sort(nums.begin(), nums.end...原创 2018-09-19 18:58:35 · 89 阅读 · 0 评论 -
LeetCode 12. Integer to Roman
44 msclass Solution {public: string dpstr(int dp, int x) { string s[4] = {"IVX", "XLC","CDM","M"}; string res; int num=0; switch(x) { c...原创 2018-09-19 18:57:50 · 86 阅读 · 0 评论 -
LeetCode 11. Container With Most Water
20ms,超过48%.class Solution {public: int maxArea(vector<int>& height) { int Area = 0; int L=0, R=height.size()-1; int HL=height[L], HR=height[R]; while(...原创 2018-09-19 11:36:22 · 79 阅读 · 0 评论 -
LeetCode 6. ZigZag Conversion
32 ms 超过28.9%。答案里有一种做法是只申请一个string,按照i从0到numRows去遍历(即每一层遍历完再遍历下一层)class Solution {public: string convert(string s, int numRows) { if(numRows == 1) return s; int tm...原创 2018-09-19 10:50:02 · 81 阅读 · 0 评论 -
LeetCode 5. Longest Palindromic Substring
100ms,超29%。。。class Solution {public: string longestPalindrome(string s) { int Aindex = 0, Alen = 0; int Bindex = 0, Blen = 0; string stra,strb; auto len = s.si...原创 2018-09-18 19:58:01 · 75 阅读 · 0 评论 -
LeetCode 3. Longest Substring Without Repeating Characters
顺序表存储子串,顺序检索,32ms.击败33%,待有空改进子串为hashclass Solution {public: int IsA(string a, char c) { for(int j=0; j<a.size(); j++) { if(c==a[j]) return j;...原创 2018-09-18 16:57:26 · 90 阅读 · 0 评论 -
LeetCode 564. Find the Closest Palindrome
折腾了一天多,从完全自己想轮子全是bug,到借鉴别人的思路,再踩无数string的坑,算是有些收获吧。。可惜只超过了10%的人。。。。class Solution {public: string Mirror(string n) { auto len = n.size(); string res = n.substr(0,len/2); ...原创 2018-09-18 15:14:32 · 141 阅读 · 0 评论 -
LeetCode 8. String to Integer (atoi)
Accepted 20ms 修改while循环终止条件为不遍历完,可以到12msclass Solution {public: int myAtoi(string str) { string result; int i=0,j=0,tag=0; long long res=0; if(!((str[i]>='0'...原创 2018-09-16 13:46:12 · 83 阅读 · 0 评论 -
LeetCode--2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i...原创 2018-09-15 19:11:06 · 85 阅读 · 0 评论 -
LeetCode 707
LeetCode 707 gdb没问题,但leetcode服务器跑出来老是不对//C++ file/*=============================================** Filename: Leetcode/7071.cpp** Author: Joe_CAO - [email protected]* Descrip...原创 2018-08-28 16:29:58 · 556 阅读 · 0 评论 -
LeetCode 42. Trapping Rain Water
8ms, 71.85%.。。。从下午4点到晚9点,前两个算法思路都是错误的。第三个思路正确即先遍历数组寻找最高点,再以最高点为界,左右依次计算阶梯状的蓄水池(类似于梯田)以左侧为例,LeftTrap函数,输入的参数LeftIndex所在的柱子是从0到LeftIndex的最高柱,遍历0到LeftIndex,寻找第二高柱子NewLeftIndex,计算两者中间的水量,再递归调用Lefttra...原创 2018-09-20 21:22:17 · 111 阅读 · 0 评论