Leetcode解题笔记,basic calculator 1&&basic calculator 2

Basic Calculator1

题目内容:

实现一个带有加减以及括号功能的小计算器,其中输入的没用负数,而且输入的内容也全部是合法表达式。

个人分析:

1.利用stack解题,将得出的临时结果放入stack中

2.遇到括号的时候将result放入stack中

心得:

1. 对于加减可以直接利用符号进行操作,专门设置一个sign的变量去存这个符号

2.转化字符串中数字固定方法:number = number*10+s.charAt(i)-‘0‘

3. 对于这类需要对中间过程进行记录的可以利用stack来实现,相似的题目有valid pathness(判断合法符号的)

4. 对于符号的控制可以使用pop或者push进行操作。

5. 讲result结果放入栈中应将result结果重新进行赋值result=0;

代码:

public int calculate(String s) {
Stack<Integer> stack = new Stack<Integer>();
int result =0;
int num = 0;
int sign =1;
for(int i=0; i<s.length();i++){
if(Character.isDigit(s.charAt(i))){
num = num*10+s.charAt(i)-‘0‘;
}else if(s.charAt(i)==‘+‘){
result += num*sign;
num =0;
sign =1;
}else if(s.charAt(i)==‘-‘){
result +=num*sign;
num =0;
sign = -1;
}else if(s.charAt(i)==‘(‘){
stack.push(result);
stack.push(sign);
result = 0;
sign =1;
}else if(s.charAt(i)==‘)‘){
result += sign*num;
num =0;
result *= stack.pop();
result += stack.pop();
}
}
if (num !=0 ) result +=sign*num;
return result;
}

Basic calculator2:

1. 与1中不同,将计算结果全部放入栈中最后再进行加和计算

时间: 2024-12-26 03:42:25

Leetcode解题笔记,basic calculator 1&&basic calculator 2的相关文章

122. Best Time to Buy and Sell Stock(二) leetcode解题笔记

122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell on

121. Best Time to Buy and Sell Stock (一) leetcode解题笔记

121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algor

110.Balanced Binary Tree Leetcode解题笔记

110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 很早以前做的了  准

Leetcode解题笔记-Contains Duplicate &amp;&amp; Contains Duplicate II&amp;&amp;Contain Duplicate III

Contain Duplicate 题目要求: 给定一个数组如果有重复就返回true,如果没有就返回false 个人分析: 这个题比剔除重复的要简单许多,只需要判断就好 代码如下: public static boolean containsDuplicate(int[] nums){ if (nums.length==0) return false; Arrays.sort(nums); for(int i =1; i < nums.length; i++){ if(nums[i-1]==nu

Leetcode解题笔记-Merge sorted Array

题目要求: 将两个排序好的nums1和nums2数组合并成为一个数组,nums1中有足够的空间,有m+n的空间,在nums1中有m个元素,在nums2中有n个元素 解题思路: 将nums2中的元素归并到nums1中,对于nums1以及nums2从后往前进行遍历,归并到nums1中, while(i>-1&&j>-1)nums1[k--]= (nums1[i--]>nums2[j--])?nums1[i--]:nums2[j--];

LeetCode解题笔记 - 20. Valid Parentheses

这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶段性查看,一定能对自己有帮助. 这是我做的第一题,所以记录也从这题开始,之后尽力以简短的说明,描述出思路,方便以后能回顾到简介明了的记录. 20. Valid Parentheses Given a string containing just the characters '(', ')', '{

LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

leetcode解题笔记-Remove Duplicates from Sorted Array

题目要求: 去除数组中相重复的的元素,并且返回新数组的长度.(要求不能再申请额外空间,只能在原来数组上进行操作) 个人理解: 1. 数组定义之后就是定长的不能改变,只能在原来数组上进行操作,不能像链表一样根据指针进行操作 2. 与remove element相似,利用数组中后续的元素进行代替,将数组的一部分变为无重复且排序好的结果子数组: if(nums[i-1]!=nums[i]) nums[id++] = nums[i]; // 如果前一项的值不等于后一项,那么在id+1项的值就等于当前项,

Leetcode解题笔记-Plusone

题目内容: 给定一个int数组,代表一个非负数不同的位, 在这个数字中加上1之后将结果以int数组的形式返回. 个人解法1: 1.现将数组转化为int之后加上1之后再变成数组,这样就可能忽略溢出所以无法通过 个人解法2: 这个解法超级麻烦自己也做不下去了,感觉对于数组操作的部分还不是很熟悉: 1.先定义一个ArraList作为结果集(因为它不定长,不用考虑边界问题),之后对digits上面的每一位进行判断,如果加上1等于10的话digits[i]=0 and carry =1 2.将计算后的结果