
LeetCode
HccqXd
这个作者很懒,什么都没留下…
展开
-
LeetCode_21——合并两个有序链表
class Solution1 { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* phead = new ListNode(-1); //构建带有头结点的链表 ListNode* pNode = phead; while (l1 != nullptr && l2 !=...原创 2020-01-05 21:54:37 · 130 阅读 · 0 评论 -
LeetCode_20——有效的括号
class Solution1 { public: bool isValid(string s) { stack<char> cs; if (s[0]==')'||s[0]=='}'||s[0]==']') //字符串第0个元素就是右括号,返回false return false; for (int i = 0; i != s.size(); i++) { ...原创 2020-01-05 21:48:21 · 142 阅读 · 0 评论 -
LeetCode_14——最长公共前缀
if (strs.size() == 0) return ""; string result; for (int i = 1; i <= strs[0].size(); i++) { //以第一个字符串为基准,设公共前缀为第一个字符串前i个子字符串 result = strs[0].substr(0, i); for (int j = 1; j != strs....原创 2020-01-05 21:42:43 · 284 阅读 · 0 评论 -
LeetCode_13——罗马数字转整数
解法:定义返回值res=0,依次遍历字符串, 比如IV,先遍历I,因为I后面的V比I大,则res=res+5-1=4; 又比如VIII,(1)先遍历V,因为V后面I比V小,则res=res+5=5; (2)再遍历I,因为后面I和I相等,则res=res+1=6; (3)同理,最终可得res=8; class...原创 2020-01-05 21:32:03 · 155 阅读 · 0 评论 -
LeetCode_9——回文数
一、先转换为字符串,再利用反转函数: class Solution1 { public: bool isPalindrome(int x) { if (x < 0) return false; string temp = to_string(x); string temp_re = to_string(x); reverse(temp_re.begin(), te...原创 2019-12-05 10:16:49 · 113 阅读 · 0 评论 -
LeetCode_7——整数反转
一、利用C++逆迭代器实现: class Solution1 { public: int reverse(int x) { vector<int> vec; while (x != 0) { vec.push_back(x % 10); x = x / 10; } int result = 0, mul = 1; for (auto pt = ve...原创 2019-12-05 10:03:31 · 121 阅读 · 0 评论 -
LeetCode_1——两数之和
一、暴力解法: class Solution1 { public: vector<int> twoSum(vector<int>& nums, int target) { int i, j; vector<int> result; for (i = 0; i != nums.size(); i++) { for (j = i + ...原创 2019-12-05 09:51:38 · 124 阅读 · 0 评论