当前位置:网站首页>LeetCode 01: T1. 两数之和 ; T1108. IP 地址无效化 ; T344. 反转字符串
LeetCode 01: T1. 两数之和 ; T1108. IP 地址无效化 ; T344. 反转字符串
2022-07-27 10:58:00 【无知小九】
目录
T1: 1. 两数之和(简单)
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1]
示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]
示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]
提示:
2 <= nums.length <= 103
-109 <= nums[i] <= 109
-109 <= target <= 109
只会存在一个有效答案
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
直接寻找求和能达到target的数字, 脑海中能想到两个指针, 注意第二个循环里右指针=左指针+1 而不是等于 0
或者使用Hash表更快找到相应数值
解法 1: 暴力
class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i = 0; i<nums.length-1;++i){
for(int j = i+1; j<nums.length;++j){
if(nums[j]==target-nums[i]){
return new int[]{i,j};
}
}
}
return new int[0];
}
}
执行用时:0 ms, 在所有 Java 提交中击败了**100.00%**的用户
内存消耗:38.6 MB, 在所有 Java 提交中击败了**68.15%**的用户
时间复杂度:O(N^2),其中 N 是数组中的元素数量。最坏情况下数组中任意两个数都要被匹配一次。
空间复杂度:O(1)。
class Solution {
public int[] twoSum(int[] nums, int target) {
int left =0;
while(left<nums.length-1){
for(int i = left+1; i < nums.length; ++i){
if(nums[left]+nums[i]==target){
return new int[]{
left, i};
}
}
++left;
}
return new int[0];
}
}
解法 2: 哈希表
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hashtable = new HashMap<Integer,Integer>();
for(int i = 0;i<nums.length;++i){
if(hashtable.containsKey(target-nums[i])){
return new int[]{hashtable.get(target-nums[i]),i};
}
hashtable.put(nums[i],i);
}
return new int[0];
}
}
执行用时:0 ms, 在所有 Java 提交中击败了**100.00%**的用户
内存消耗:38.5 MB, 在所有 Java 提交中击败了**80.48%**的用户
时间复杂度:O(N),其中 N 是数组中的元素数量。对于每一个元素 x,我们可以 O(1) 地寻找 target - x。
空间复杂度:O(N)),其中 N 是数组中的元素数量。主要为哈希表的开销。
T2: 1108. IP 地址无效化(简单)
给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本。
所谓无效化 IP 地址,其实就是用 “[.]” 代替了每个 “.”。
示例 1:
输入:address = "1.1.1.1"
输出:"1[.]1[.]1[.]1"
示例 2:
输入:address = "255.100.50.0"
输出:"255[.]100[.]50[.]0"
提示:
给出的 address 是一个有效的 IPv4 地址
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/defanging-an-ip-address
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
直接替换…
或者用 StringBuilder
解法 1: replace
class Solution {
public String defangIPaddr(String address) {
return address.replace(".","[.]");
}
}
执行用时:0 ms, 在所有 Java 提交中击败了**100.00%**的用户
内存消耗:36 MB, 在所有 Java 提交中击败了**99.78%**的用户
解法 2: StringBuider
class Solution {
public String defangIPaddr(String address) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < address.length(); ++i){
if(address.charAt(i)=='.'){
sb.append("[.]");
}else{
sb.append(address.charAt(i));
}
}
return sb.toString();
}
}
执行用时:0 ms, 在所有 Java 提交中击败了**100.00%**的用户
内存消耗:36.3 MB, 在所有 Java 提交中击败了**87.23%**的用户
T3: 344. 反转字符串(简单)
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。
不要给另外的数组分配额外的空间,你必须**原地修改输入数组**、使用 O(1) 的额外空间解决这一问题。
你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。
示例 1:
输入:["h","e","l","l","o"]
输出:["o","l","l","e","h"]
示例 2:
输入:["H","a","n","n","a","h"]
输出:["h","a","n","n","a","H"]
https://leetcode-cn.com/problems/reverse-string/
思路
可以用temp交换, 也可以用异或交换, 还可以用 a = a+b, b = a-b, a = a-b
解法 1: 双指针
class Solution {
public void reverseString(char[] s) {
int left = 0, right = s.length-1;
while(left<=right){
char temp = s[left];
s[left] = s[right];
s[right]= temp;
left++;
right--;
}
}
}
执行用时:1 ms, 在所有 Java 提交中击败了**100.00%**的用户
内存消耗:44.9 MB, 在所有 Java 提交中击败了**83.80%**的用户
时间复杂度:O(N),其中 N 为字符数组的长度。一共执行了 N/2 次的交换。
空间复杂度:O(1)。只使用了常数空间来存放若干变量。
解法 2: 异或
class Solution {
public void reverseString(char[] s) {
int left = 0, right = s.length-1;
while(left<right){
s[left] ^= s[right];
s[right] ^= s[left];
s[left] ^= s[right];
left++;
right--;
}
}
}
执行用时:1 ms, 在所有 Java 提交中击败了**100.00%**的用户
内存消耗:45.2 MB, 在所有 Java 提交中击败了**34.69%**的用户
边栏推荐
- [shader realizes shake random shaking effect _shader effect Chapter 10]
- Local virtual machine initialization script
- (3) Pass parameters
- Everything cannot be searched for startup_ Lpc11x.s file
- The longest ascending subsequence model acwing 1016. The sum of the largest ascending subsequence
- Error while unzipping file in win10: unable to create symbolic link. You may need to run WinRAR with administrator privileges. The client does not have the required privileges
- 树形DP AcWing 285. 没有上司的舞会
- C programming language (2nd Edition) -- Reading Notes -- 1.5.1
- Maker Hongmeng application development training notes 02
- Maker harmony OS application development training notes 01
猜你喜欢

高斯消元 AcWing 883. 高斯消元解线性方程组

Error encountered in adding quick open option to right-click menu:

Moveit2 - 4. robot model and robot state

Interval problem acwing 906. Interval grouping

最长上升子序列模型 AcWing 1012. 友好城市

The longest ascending subsequence model acwing 1017. The glider wing of the strange thief Kidd

第13章 IO流

Digital triangle model acwing 1018. Minimum toll

Codeforces round #664C

Properties file
随机推荐
The C programming language-2nd-- notes -- 4.11.3
数字三角形模型 AcWing 1027. 方格取数
(8) Shell function
Digital triangle model acwing 1018. Minimum toll
Interval problem acwing 906. Interval grouping
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
The longest ascending subsequence model acwing 1016. The sum of the largest ascending subsequence
(9) Shell I / O redirection
Win10 vscode code code format setting and remote breakpoint debugging
Longest ascending subsequence model acwing 1010. Interceptor missile
Instructions for mock platform
SQL statement learning and the use of pymysql
Maker Hongmeng application development training notes 03
Remember an experience of using canvas to make the banner streamer effect of Tencent cloud homepage
LAN SDN technology hard core insider 11 the key of cloud convergence CP -- hierarchical port binding
The C programming language (2nd) -- Notes -- 1.7
本地虚拟机初始化脚本
栈 AcWing 3302. 表达式求值
(10) File contains
你真的会写二分查找吗——变种二分查找