首页 > 其他分享 >【114】

【114】

时间:2022-11-04 09:33:51浏览次数:55  
标签:begin target numMoves int ++ 114 place

754. 到达终点数字  

在一根无限长的数轴上,你站在0的位置。终点在target的位置。

你可以做一些数量的移动 numMoves :

  • 每次你可以选择向左或向右移动。
  • 第 i 次移动(从  i == 1 开始,到 i == numMoves ),在选择的方向上走 i 步。

给定整数 target ,返回 到达目标所需的 最小 移动次数(即最小 numMoves ) 

 

示例 1:

输入: target = 2
输出: 3
解释:
第一次移动,从 0 到 1 。
第二次移动,从 1 到 -1 。
第三次移动,从 -1 到 2 。

示例 2:

输入: target = 3
输出: 2
解释:
第一次移动,从 0 到 1 。
第二次移动,从 1 到 3 。
------------------------------------------------------------------------------------------------
仅仅考虑了单向移动,没有抓住问题的本质是一个求和问题
 1 class Solution {
 2     public int reachNumber(int target) {
 3         int numMoves = 0;
 4         int begin = 0;
 5         if(target > 0){
 6             for(int i = 1; begin != target; i++){
 7                 if(i == Math.abs(target-begin)){
 8                     numMoves++;
 9                     break;
10                 }else if(i < Math.abs(target-begin)){
11                     begin+=i;
12                     numMoves++;
13                 }else{
14                     begin-=i;
15                     numMoves++;
16                 }
17             }
18         }
19         if(target < 0){
20             for(int i = 1; begin != target; i++){
21                 if(i == Math.abs(target-begin)){
22                     numMoves++;
23                     break;
24                 }else if(i < Math.abs(target-begin)){
25                     begin-=i;
26                     numMoves++;
27                 }else{
28                     begin+=i;
29                     numMoves++;
30                 }
31             }
32         }
33         return numMoves;
34     }
35 }
------------------------------------------------------------------------------------------------
这道题的本质问题是一个求和问题,从1开始累加,给自然数加上符号表示向左或者向右移动,最后累加的结果就是 target 。
例如:1 = 1, 2 = 1+(-2)+3, 3 = 1+2, 4 = (-1)+2+3, 5 = 1+2+3+4+(-5), 6 = 1+2+3.
7 = 1+2+3+(-4)+5因此只用考虑单侧方向,到达1和-1过程的区别只有后边的符号不同。
 1 class Solution {
 2     public int reachNumber(int target) {
 3         int numMoves = 0;
 4         int place = 0;
 5         int step = 1;
 6         target = Math.abs(target);
 7         while(place < target){
 8             place+=step;
 9             step++;
10             numMoves++;
11         }
12         if((place-target) % 2 == 0){
13             return numMoves;
14         }else{
15             while((place-target) % 2 != 0){
16                 place+=step;
17                 step++;
18                 numMoves++;
19             }
20         }
21         return numMoves;
22         
23     }
24 }

又简化了下代码
 1 class Solution {
 2     public int reachNumber(int target) {
 3         int numMoves = 0;
 4         int place = 0;
 5         int step = 1;
 6         target = Math.abs(target);
 7         while(place < target || (place-target) % 2 != 0){
 8             place+=step;
 9             step++;
10             numMoves++;
11         }
12         return numMoves;
13         
14     }
15 }

 

 
------------------------------------------------------------------------------------------------
答案的题解,思路大致一致,只不过代码更简单
 1 class Solution {
 2     public int reachNumber(int target) {
 3         target = Math.abs(target);
 4         int k = 0;
 5         while (target > 0) {
 6             k++;
 7             target -= k;
 8         }
 9         return target % 2 == 0 ? k : k + 1 + k % 2;
10     }
11 }
12 
13 作者:力扣官方题解
14 链接:https://leetcode.cn/problems/reach-a-number/solutions/1946020/dao-da-zhong-dian-shu-zi-by-leetcode-sol-ak90/
15 来源:力扣(LeetCode)
16 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 



标签:begin,target,numMoves,int,++,114,place
From: https://www.cnblogs.com/wzxxhlyl/p/16856628.html

相关文章

  • CF1141F2 Same Sum Blocks (Hard)
    题目传送门思路简单题。不妨先预处理出每一个区间的\(\sum\),然后离散化\(\sum\),对于每个\(\sum\)开一个\(\mathcalvector\)记录所有区间的左右端点。然后枚举每......
  • 洛谷P1144
    最短路计数题目描述给出一个\(N\)个顶点\(M\)条边的无向无权图,顶点编号为\(1\simN\)。问从顶点\(1\)开始,到其他每个点的最短路有几条。输入格式第一行包含\(2......
  • 【XSY3892】【hihocoder1147】时空阵(分层图dp)
    设\(dp(i,t,l)\)表示已经定好前\(i\)层,共有\(t\)个节点,其中第\(i\)层有\(l\)个节点。直接转移即可,注意一些细节:第\(1\)层只有\(1\)号节点。同层之间......
  • uva11404
    删去字符串S中的一些字符,使剩下的字符组成最长的回文串(顺序连接) 区间dpf[i][j] f[i][j]=f[i+1][j-1]  i==j =min(f[i+1][j],f[i][j-1]) #include......
  • 2022-2023 20211404《信息安全专业导论》 第九周学习总结
    **作业信息**作业模板:https://www.cnblogs.com/rocedu/p/9577842.html#JXJC作业要求:https://www.cnblogs.com/rocedu/p/9577842.html#WEEK09**教材学习内容总结**《计算机......
  • HDU 1114 Piggy-Bank
    题目链接:​​传送门​​Piggy-BankProblemDescriptionBeforeACMcandoanything,abudgetmustbepreparedandthenecessaryfinancialsupportobtained.Them......
  • 力扣 114. 二叉树展开为链表-原地算法(O(1) 额外空间)详解
    114.二叉树展开为链表给你二叉树的根结点 root ,请你将它展开为一个单链表:展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而......
  • ProgrammingError at /admin/mes_app/book/ (1146, "Table 'mes.mes_app_book' doesn'
     发生上述这个问题的时候,可以把CSRF直接在settings中直接屏蔽掉。 ......
  • UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 3114: inval
    movies=pd.read_table(file_path,header=None,sep="::",names=["movieID","title","genres"],engine="python",)添加encoding='ISO-8859-1'......
  • CodeForces-1143#C 题解
    正文♦时间复杂度:\(\mathcal{O}(n)\)思维题,不需要建树。设数组\(a\)记录每一个节点是否尊重它的父节点,数组\(b\)记录是否有节点尊重它,特别的,叶子节点必然不被尊重。......