leetcode

LeetCode 链表题 ( Java )

血红的双手。 提交于 2019-12-08 22:15:37
leetcode 237. 删除链表中的节点 链接: https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 示例 : 输入: head = [4,5,1,9], node = 5输出: [4,1,9]解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9. 这道题比较简单, 修改之前节点的 next 指针,使其指向之后的节点 : /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public void deleteNode(ListNode node) { node.val = node.next.val; node.next = node.next.next; } } leetcode 160. 相交链表 链接: https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ 编写一个程序,找到两个单链表相交的起始节点。 如下面的两个链表:

LeetCode Contest 166

若如初见. 提交于 2019-12-08 12:46:25
LeetCode Contest 166 第一次知道LeetCode 也有比赛。 很久没有打过这种线上的比赛,很激动。 直接写题解吧 第一题 很弱智 class Solution { public: int subtractProductAndSum(int n) { int num1=0; int num2=1; while(n) { int x = n%10; num1+=x; num2*=x; n/=10; } return num2-num1; } }; 第二题 也很简单的,我先排个序。 但是在用c++的快排的时候,被坑了,我一直的习惯是写自定义比较函数,没有写运算符重载,不知道为什么一直RE,浪费了挺多时间 struct Node { int value; int index; Node(){} Node(int value,int index) { this->value = value; this->index = index; } bool operator <(const Node &x)const { return value<x.value; } }; class Solution { public: int vis[1005]; Node a[1005]; vector<vector<int>> groupThePeople(vector<int>&

Leetcode 415 字符串相加 C++,Java,Python

人盡茶涼 提交于 2019-12-08 07:24:28
Leetcode415 字符串相加 来源:力扣(LeetCode) 链接: https://leetcode-cn.com/problems/add-strings 博主Github : https://github.com/GDUT-Rp/LeetCode 题目: 给定两个字符串形式的非负整数 num1 和 num2 ,计算它们的和。 示例 1: 输入: num1 = "2", num2 = "3" 输出: "5" 示例 2: 输入: num1 = "123", num2 = "456" 输出: "579" 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和num2 都不包含任何前导零。 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。 解题思路: 方法一:逐个相加 直观想法 从最后一位开始逐个相加,注意判断是否有进位。 C++: # include <iostream> using namespace std ; class Solution { public : string addStrings ( string num1 , string num2 ) { int len1 = num1 . size ( ) ; int len2 = num2 . size ( ) ;

Leetcode 43 字符串相等 C++,Java,Python

喜夏-厌秋 提交于 2019-12-08 07:24:05
Leetcode43 字符串相乘 来源:力扣(LeetCode) 链接: https://leetcode-cn.com/problems/multiply-strings 博主Github : https://github.com/GDUT-Rp/LeetCode 题目: 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。 示例 1: 输入: num1 = "2", num2 = "3" 输出: "6" 示例 2: 输入: num1 = "123", num2 = "456" 输出: "56088" 说明: num1 和 num2 的长度小于110。 num1 和 num2 只包含数字 0-9。 num1 和 num2 均不以零开头,除非是数字 0 本身。 不能使用任何标准库的大数类型(比如 BigInteger)或直接将输入转换为整数来处理 解题思路: 方法一:逐个相乘 直观想法 逐个相乘,注意进位。 C++: # include <iostream> using namespace std ; class Solution { public : string multiply ( string num1 , string num2 ) { string ans ; int len1 = num1 .

leetcode-8-字符串转整数

跟風遠走 提交于 2019-12-08 06:27:53
title: leetcode-8-字符串转整数 date: 2019-08-30 14:49:28 categories: leetcode tags: leetcode 字符串转整数 你来实现一个 atoi 函数,使其能将字符串转换成整数。 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。 该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。 注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。 在任何情况下,若函数不能进行有效的转换时,请返回 0。 说明: 假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231, 231 − 1]。如果数值超过这个范围,请返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。 示例 1: 输入: “42” 输出: 42 示例 2: 输入: " -42" 输出: -42 解释: 第一个非空白字符为 ‘-’, 它是一个负号。

【Leetcode】4. Median of Two Sorted Arrays

送分小仙女□ 提交于 2019-12-07 17:33:18
1.英文题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assume nums1 and nums2 cannot be both empty. Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5 2.解题 package com.example.leetcode.hard; public class FindMedianSortedArrays { public static double findMedianSortedArrays(int[] num1, int[] num2) { int m = num1.length; int n = num2.length; int size = m + n; int[] result = new int[(m + n)];

ARTS 打卡第一周

删除回忆录丶 提交于 2019-12-07 11:40:45
Algorithm 初识leetcode网站,是参加了覃超的《算法训练营》的小课训练。视频中,覃老师在课堂上介绍了“五毒神掌”的算法学习方法: 1.先花5-10分钟读题,然后去思考答案。如果暂时没思路,不要紧,直接看题解。 2.自行题解。使用多种方法尝试,直到速度优化到最佳。 3.看leetcode中前三的解法。 4.一周后重新解题。 5.较长时间后(如面试前)重新解题。 本次Leetcode题目是:给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。 链接:https://leetcode-cn.com/problems/integer-to-roman 解题思路: 1.枚举法 将所有可能的情况列出:将4位整数拆分成 个十百千,然后分别找对应的数字即可。 public String intToRoman(int num) { String [] one = {"I","II","III","IV","V","VI","VII","VIII","IX","X"}; String [] two = {"X","XX","XXX","XL","L","LX","LXX","LXXX","XC","C"}; String [] three ={"C","CC","CCC","CD","D","DC","DCC","DCCC","CM","M"}; String []

【leetcode】1278. Palindrome Partitioning III

旧城冷巷雨未停 提交于 2019-12-07 08:29:25
题目如下: You are given a string s containing lowercase letters and an integer k . You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string. Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb"

LeetCode:Employees Earning More Than Their Manager

吃可爱长大的小学妹 提交于 2019-12-07 02:23:58
1、题目名称 Employees Earning More Than Their Managers(比领导工资还高的员工) 2、题目地址 https://leetcode.com/problems/employees-earning-more-than-their-managers/ 3、题目内容 Employee表的结构包含四列:Id、Name、Salary、ManagerId,找出其中工资高于直接上级的员工的Name。 例如,下表中Joe的工资大于他的直接上级Sam: +----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | NULL | | 4 | Max | 90000 | NULL | +----+-------+--------+-----------+ 4、初始化数据库脚本 在MySQL数据库中建立一个名为LEETCODE的数据库,用MySQL命令行中的source命令执行下面脚本: -- 执行脚本前必须建立名为LEETCODE的DATABASE USE LEETCODE;

LeetCode:Nth Highest Salary

99封情书 提交于 2019-12-07 02:22:52
1、题目名称 Nth Highest Salary(第N高的工资) 2、题目地址 https://leetcode.com/problems/nth-highest-salary/ 3、题目内容 与这道题目相比,上一道题目“ Second Highest Salary ”是本题在N=2时的特例。两道题目的表结构完全一样,Employee表的结构如下: +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ 4、初始化数据库脚本 在MySQL数据库中建立一个名为LEETCODE的数据库,用MySQL命令行中的source命令执行下面脚本: -- 执行脚本前必须建立名为LEETCODE的DATABASE USE LEETCODE; DROP TABLE IF EXISTS Employee; CREATE TABLE Employee ( Id INT NOT NULL PRIMARY KEY, Salary INT ); INSERT INTO Employee (Id, Salary) VALUES (1, 100); INSERT INTO Employee (Id, Salary) VALUES (2, 200); INSERT