# https://www.facebook.com/prayash.mohapatra.376/posts/391656181833378 # Subscribed by Code House # Program to check palindrome string import java.util.Stack; import java.util.Scanner; class PalindromeTest { public static void main(String[] args) { System.out.print("Enter any string:"); Scanner in=new Scanner(System.in); String inputString = in.nextLine(); Stack stack = new Stack(); for (int i = 0; i < inputString.length(); i++) { stack.push(inputString.charAt(i)); } String reverseString = ""; while (!stack.isEmpty()) { reverseString = reverseString+stack.pop(); } if (inputString.equals(reverseString)) System.out.println("The input String is a palindrome."); else System.out.println("The input String is not a palindrome."); } }
Input: " 3+5 / 2 " Output: 5 Note: You may assume that the given expression is always valid. Do not use the eval built-in library function. 先保存数字,再保存运算符 import java.util.Stack; class Solution { public static int calculate(String s) { char[] chars = s.replace(" ", "").toCharArray(); long res = 0, num = 0; char op = '+'; Stack<Long> st = new Stack(); for (int i = 0; i < chars.length; i++) { if (chars[i] >= '0') num = num * 10 + chars[i] - '0'; if (chars[i] < '0' || i == chars.length - 1) { //switch case if (op == '+') st.push(num); else if (op == '-') st.push(-num); else if (op == '*' || op == '/') { Long tmp = (op == '*') ? st.pop() * num : st.pop() / num; st.push(tmp); } op = chars[i]; num = 0;
# -*- coding: utf-8 -*- from __future__ import unicode_literals from java.util import Stack a = Stack() for k in range(10): a.push(k) print(a.search(7)) print(a.peek()) print(a.pop()) print(type(a)) print(a)